Dynamic Content February 16 2004 2162004 Assignments Due
Dynamic Content February 16, 2004 2/16/2004
Assignments • Due – Message of the Day Part 1 • Due – Reading and Warmup • Work on Message of the Day 2/16/2004
What is Dynamic Content? • Example? • Instead of fixed content, generate the page dynamically • Run a program and return the result 2/16/2004
Technologies • • • Servlets CGI PHP ASP JSP 2/16/2004
How it Works 1. request 2/16/2004
How it Works 2. “Web container” activates servlet 2/16/2004
How it Works 3. Servlet contacts DB 2/16/2004
How it Works 4. Servlet constructs response and passes to “Web container” 2/16/2004
How it Works 5. Response returned to client 2/16/2004
How it Works mysql 2/16/2004 Tomcat running on napa Client running web browser
Developing Servlets 1. Write the Java code – this can happen anywhere, but to compile you need the appropriate jars (servlet. jar, mysql. jar) 2. Put your. class files in the appropriate location • • Tomcat will look in /home/<user>/public_html/WEBINF/classes Make sure WEB-INF has a web. xml 3. Invoke by going to: • • 2/16/2004 https: //napa. mtholyoke. edu: 8443/~<user>/servlet/Hello. Servlet Note, Tomcat using SSL
Hello. Servlet. java import java. io. *; import javax. servlet. http. *; public class Hello. Servlet extends Http. Servlet { public void do. Get (Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { // set content-type header before accessing the Writer response. set. Content. Type("text/html"); response. set. Buffer. Size(8192); Print. Writer out = response. get. Writer(); out. println("<html><head><title>Hello</title></head>"); out. println("<body>"); out. println("<center><h 3> Hello Servlet</h 3></center>"); out. println("<p>This is my cool Hello Servlet!</p>"); out. println("</body>"); out. println("</html>"); out. close(); } } 2/16/2004
Servlet Methods • init – Called when a servlet is first loaded – Initialize objects (such as DB handle) • do. Get, do. Post – Handle corresponding HTTP request 2/16/2004
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 2/16/2004
Request Methods • String get. Parameter(String name) – Parameter for URL or form data • Http. Session get. Session() 2/16/2004
Session Methods • Object get. Attribute(String name) • void set. Attribute(String name, Object value) • void invalidate() 2/16/2004
Response Methods • Servlet. Output. Stream get. Output. Stream() • void set. Content. Type(String type) • String encode. URL(String URL) 2/16/2004
Example • Welcome. java 2/16/2004
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. for. Name("com. mysql. jdbc. Driver"). new. Instance(); Connection = Driver. Manager. get. Connection("jdbc: mysql: //localhost/srollins? user= srollins&password=srollins"); 2/16/2004
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)); 2/16/2004
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: cs 341: Computer Science: 101: Intro to C: cs 101 reg_entry. txt 12345: cs 341: 12345: cs 101 2/16/2004
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='cs 101' and STUID=12345; insert into reg_entry (STUID, COURSEID) values (12345, 'cs 101'); 2/16/2004
JDBC • Connection – create. Statement • Statement – execute. Query – select – execute. Update – insert/delete • Result. Set – next – very important – get. String – DB column name (STUID) 2/16/2004
Form Input/DB Interaction • Login. java 2/16/2004
- Slides: 24