Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/16/2004 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.

Similar presentations


Presentation on theme: "2/16/2004 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."— Presentation transcript:

1 2/16/2004 Dynamic Content February 16, 2004

2 2/16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day

3 2/16/2004 What is Dynamic Content? Example? Instead of fixed content, generate the page dynamically Run a program and return the result

4 2/16/2004 Technologies Servlets CGI PHP ASP JSP

5 2/16/2004 How it Works 1. request

6 2/16/2004 How it Works 2. “Web container” activates servlet

7 2/16/2004 How it Works 3. Servlet contacts DB

8 2/16/2004 How it Works 4. Servlet constructs response and passes to “Web container”

9 2/16/2004 How it Works 5. Response returned to client

10 2/16/2004 How it Works mysql Tomcat running on napa Client running web browser

11 2/16/2004 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/ /public_html/WEB- INF/classes Make sure WEB-INF has a web.xml 3.Invoke by going to: https://napa.mtholyoke.edu:8443/~ /servlet/HelloServlet Note, Tomcat using SSL

12 2/16/2004 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(" Hello "); out.println(" "); out.println(" Hello Servlet "); out.println(" This is my cool Hello Servlet! "); out.println(" "); out.close(); }

13 2/16/2004 Servlet Methods init –Called when a servlet is first loaded –Initialize objects (such as DB handle) doGet, doPost –Handle corresponding HTTP request

14 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

15 2/16/2004 Request Methods String getParameter(String name) –Parameter for URL or form data HttpSession getSession()

16 2/16/2004 Session Methods Object getAttribute(String name) void setAttribute(String name, Object value) void invalidate()

17 2/16/2004 Response Methods ServletOutputStream getOutputStream() void setContentType(String type) String encodeURL(String URL)

18 2/16/2004 Example Welcome.java

19 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.forName("com.mysql.jdbc.Driver").newInstance(); Connection = DriverManager.getConnection("jdbc:mysql://localhost/srollins?user= srollins&password=srollins");

20 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));

21 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:cs341: Computer Science:101:Intro to C:cs101 reg_entry.txt 12345:cs341: 12345:cs101

22 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='cs101' and STUID=12345; insert into reg_entry (STUID, COURSEID) values (12345, 'cs101');

23 2/16/2004 JDBC Connection –createStatement Statement –executeQuery – select –executeUpdate – insert/delete ResultSet –next – very important –getString – DB column name (STUID)

24 2/16/2004 Form Input/DB Interaction Login.java


Download ppt "2/16/2004 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."

Similar presentations


Ads by Google