1 / 24

Dynamic Content

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

shae
Download Presentation

Dynamic Content

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Content February 16, 2004

  2. Assignments • Due – Message of the Day Part 1 • Due – Reading and Warmup • Work on Message of the Day

  3. What is Dynamic Content? • Example? • Instead of fixed content, generate the page dynamically • Run a program and return the result

  4. Technologies • Servlets • CGI • PHP • ASP • JSP

  5. How it Works 1. request

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

  7. How it Works 3. Servlet contacts DB

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

  9. How it Works 5. Response returned to client

  10. How it Works mysql Tomcat running on napa Client running web browser

  11. 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

  12. 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(); } }

  13. Servlet Methods • init • Called when a servlet is first loaded • Initialize objects (such as DB handle) • doGet, doPost • Handle corresponding HTTP request

  14. 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. Request Methods • String getParameter(String name) • Parameter for URL or form data • HttpSession getSession()

  16. Session Methods • Object getAttribute(String name) • void setAttribute(String name, Object value) • void invalidate()

  17. Response Methods • ServletOutputStream getOutputStream() • void setContentType(String type) • String encodeURL(String URL)

  18. Example • Welcome.java

  19. 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. 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. 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. 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. JDBC • Connection • createStatement • Statement • executeQuery – select • executeUpdate – insert/delete • ResultSet • next – very important • getString – DB column name (STUID)

  24. Form Input/DB Interaction • Login.java

More Related