240 likes | 365 Views
Dynamic Content. February 16, 2004. Assignments. Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day. What is Dynamic Content?. Example? Instead of fixed content, generate the page dynamically Run a program and return the result. Technologies. Servlets
E N D
Dynamic Content February 16, 2004
Assignments • Due – Message of the Day Part 1 • Due – Reading and Warmup • Work on Message of the Day
What is Dynamic Content? • Example? • Instead of fixed content, generate the page dynamically • Run a program and return the result
Technologies • Servlets • CGI • PHP • ASP • JSP
How it Works 1. request
How it Works 2. “Web container” activates servlet
How it Works 3. Servlet contacts DB
How it Works 4. Servlet constructs response and passes to “Web container”
How it Works 5. Response returned to client
How it Works mysql Tomcat running on napa Client running web browser
Developing Servlets • Write the Java code – this can happen anywhere, but to compile you need the appropriate jars (servlet.jar, mysql.jar) • Put your .class files in the appropriate location • Tomcat will look in /home/<user>/public_html/WEB-INF/classes • Make sure WEB-INF has a web.xml • Invoke by going to: • https://napa.mtholyoke.edu:8443/~<user>/servlet/HelloServlet • Note, Tomcat using SSL
HelloServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content-type header before accessing the Writer response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); out.println("<html><head><title>Hello</title></head>"); out.println("<body>"); out.println("<center><h3> Hello Servlet</h3></center>"); out.println("<p>This is my cool Hello Servlet!</p>"); out.println("</body>"); out.println("</html>"); out.close(); } }
Servlet Methods • init • Called when a servlet is first loaded • Initialize objects (such as DB handle) • doGet, doPost • Handle corresponding HTTP request
Sessions • Keep track of multiple accesses by same user • Avoid login at every screen • Implementation: Cookies • Store ID mapping in Cookie and keep DS with ID to Object mappings • Implementation: URL rewriting • Append ID to all URLs • Session API
Request Methods • String getParameter(String name) • Parameter for URL or form data • HttpSession getSession()
Session Methods • Object getAttribute(String name) • void setAttribute(String name, Object value) • void invalidate()
Response Methods • ServletOutputStream getOutputStream() • void setContentType(String type) • String encodeURL(String URL)
Example • Welcome.java
Using JDBC • Allows you to connect to the mysql database from within a java program (servlet) • init method of first servlet (then store in session) Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection = DriverManager.getConnection("jdbc:mysql://localhost/srollins?user=srollins&password=srollins");
SQL use database username; create table student (firstname VARCHAR(20), lastname VARCHAR(20), password VARCHAR(20), STUID INT NOT NULL PRIMARY KEY); create table course (department VARCHAR(20), number INT, title VARCHAR(20), COURSEID VARCHAR(20)); create table reg_entry (STUID INT, COURSEID VARCHAR(20));
SQL LOAD DATA local INFILE "student.txt" into table student fields terminated by ':'; student.txt Sami:Rollins:samipw:12345: Mickey:Mouse:mickeypw:12346: course.txt Computer Science:341:Networked Systems:cs341: Computer Science:101:Intro to C:cs101 reg_entry.txt 12345:cs341: 12345:cs101
SQL SELECT * FROM student where STUID=12345; select course.* from course left join reg_entry on course.COURSEID=reg_entry.COURSEID where reg_entry.STUID=12345; delete from reg_entry where COURSEID='cs101' and STUID=12345; insert into reg_entry (STUID, COURSEID) values (12345, 'cs101');
JDBC • Connection • createStatement • Statement • executeQuery – select • executeUpdate – insert/delete • ResultSet • next – very important • getString – DB column name (STUID)
Form Input/DB Interaction • Login.java