logging in or signing up servlets cpf748 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 139 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: January 06, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Servlets : Servlets Stoney Jackson jacksoni@cs.ucdavis.edu http://wwwcsif.cs.ucdavis.edu/~jacksoni Java on the Web: J2EE : Java on the Web: J2EE Thin clients (minimize download) Java all “server side” THIS IS WHAT YOU’LL BE DOING!! Client Server Outline : Outline Who made Servlets? What are Servlets? Why are Servlets? Where are Servlets? When are Servlets? How are Servlets? Sun What are Servlets? : What are Servlets? Units of Java code that run server-side. Run in containers (provide context) Helps with client-server communications Not necessarily over HTTP But usually over HTTP (we’ll focus here) Why are Servlets? : Why are Servlets? Web pages with dynamic content Easy coordination between Servlets to make Web applications Containers support many features Sessions, persistence, resource management (e.g., database connections), security, etc. Where are Servlets? : Where are Servlets? HTTP Web Server File system ServletServer Static Dynamic When are Servlets? : When are Servlets? Receive Request forServlet S Is S loaded? Is S current? (re)Load S Forward Requestto S Servlets die when Servlet Server dies Loaded when first used,or after modified no no yes yes Why Not CGIs? : Why Not CGIs? CGI = Common Gateway Interface Defines interface between Web servers and programs Environment variables and standard-in as input from client Standard-out for output to client Why Not CGIs (cont.) : Why Not CGIs (cont.) Only defines interface no supporting infrastructure (security, sessions, persistence, etc.) Inefficient: new process for each request!!! Recall: Servlets loaded only “once”! How are Servlets? : How are Servlets? import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hellox extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); // out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } // doGet } // Hellox Compiling : Compiling javac –classpath $LIB/servlet-api.jar Hellox.java Directory Structure : Directory Structure Directory Structure (cont.) : Directory Structure (cont.) Slide 14: <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>Examples</description> <display-name>Examples</display-name> <servlet> <servlet-name>Hellox</servlet-name> <servlet-class>Hellox</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hellox</servlet-name> <url-pattern>/Hellox</url-pattern> </servlet-mapping> </web-app> Maps servlet to URL (rooted at D) Declares servlet abbreviation fully qualified (e.g., java.lang.String) Slide 15: public class Helloy extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello, Tell me your name!</h1> <br>"); out.print("<form action=\""); out.println("NamedHello\" method=POST>"); out.println("<input type=text length=20 name=yourname><br>"); out.println("<input type=submit></form>"); out.println("</body>"); out.println("</html>"); }} Slide 16: public class NamedHello extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = request.getParameter("yourname"); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name again!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>Hello, " + name + "</h2> <br>"); out.print("<form action=\""); out.println("NamedHello\" method=POST>"); out.println("<input type=text length=20 name=yourname><br>"); out.println("<input type=submit></form>"); out.println("</body>"); out.println("</html>"); }} Slide 17: public class NamedSessionHello1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession hs = request.getSession(true); String sn = (String) hs.getAttribute("yourname"); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name again!</title>"); out.println("</head>"); out.println("<body>"); if(sn != null && ! sn.equals ("")) { out.println("<h1><blink> OH, NamedSessionHello1” + “already know your name: " + sn + "</blink></h1>"); } else { String sn2 = request.getParameter("yourname"); if (sn2 == null || sn2.equals("")) { out.println("<h2>Hello,noname " + "</h2> <br>"); } else { out.println("<h2>Hello, " + sn2 + "</h2> <br>"); hs.setAttribute("yourname", sn2); }} out.print("<form action=\""); out.println("NamedSessionHello2\" method=GET>"); ... } You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
servlets cpf748 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 139 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: January 06, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Servlets : Servlets Stoney Jackson jacksoni@cs.ucdavis.edu http://wwwcsif.cs.ucdavis.edu/~jacksoni Java on the Web: J2EE : Java on the Web: J2EE Thin clients (minimize download) Java all “server side” THIS IS WHAT YOU’LL BE DOING!! Client Server Outline : Outline Who made Servlets? What are Servlets? Why are Servlets? Where are Servlets? When are Servlets? How are Servlets? Sun What are Servlets? : What are Servlets? Units of Java code that run server-side. Run in containers (provide context) Helps with client-server communications Not necessarily over HTTP But usually over HTTP (we’ll focus here) Why are Servlets? : Why are Servlets? Web pages with dynamic content Easy coordination between Servlets to make Web applications Containers support many features Sessions, persistence, resource management (e.g., database connections), security, etc. Where are Servlets? : Where are Servlets? HTTP Web Server File system ServletServer Static Dynamic When are Servlets? : When are Servlets? Receive Request forServlet S Is S loaded? Is S current? (re)Load S Forward Requestto S Servlets die when Servlet Server dies Loaded when first used,or after modified no no yes yes Why Not CGIs? : Why Not CGIs? CGI = Common Gateway Interface Defines interface between Web servers and programs Environment variables and standard-in as input from client Standard-out for output to client Why Not CGIs (cont.) : Why Not CGIs (cont.) Only defines interface no supporting infrastructure (security, sessions, persistence, etc.) Inefficient: new process for each request!!! Recall: Servlets loaded only “once”! How are Servlets? : How are Servlets? import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hellox extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); // out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } // doGet } // Hellox Compiling : Compiling javac –classpath $LIB/servlet-api.jar Hellox.java Directory Structure : Directory Structure Directory Structure (cont.) : Directory Structure (cont.) Slide 14: <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>Examples</description> <display-name>Examples</display-name> <servlet> <servlet-name>Hellox</servlet-name> <servlet-class>Hellox</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hellox</servlet-name> <url-pattern>/Hellox</url-pattern> </servlet-mapping> </web-app> Maps servlet to URL (rooted at D) Declares servlet abbreviation fully qualified (e.g., java.lang.String) Slide 15: public class Helloy extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello, Tell me your name!</h1> <br>"); out.print("<form action=\""); out.println("NamedHello\" method=POST>"); out.println("<input type=text length=20 name=yourname><br>"); out.println("<input type=submit></form>"); out.println("</body>"); out.println("</html>"); }} Slide 16: public class NamedHello extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = request.getParameter("yourname"); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name again!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>Hello, " + name + "</h2> <br>"); out.print("<form action=\""); out.println("NamedHello\" method=POST>"); out.println("<input type=text length=20 name=yourname><br>"); out.println("<input type=submit></form>"); out.println("</body>"); out.println("</html>"); }} Slide 17: public class NamedSessionHello1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession hs = request.getSession(true); String sn = (String) hs.getAttribute("yourname"); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello, Tell me your name again!</title>"); out.println("</head>"); out.println("<body>"); if(sn != null && ! sn.equals ("")) { out.println("<h1><blink> OH, NamedSessionHello1” + “already know your name: " + sn + "</blink></h1>"); } else { String sn2 = request.getParameter("yourname"); if (sn2 == null || sn2.equals("")) { out.println("<h2>Hello,noname " + "</h2> <br>"); } else { out.println("<h2>Hello, " + sn2 + "</h2> <br>"); hs.setAttribute("yourname", sn2); }} out.print("<form action=\""); out.println("NamedSessionHello2\" method=GET>"); ... }