Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D. Objectives To introduce the basic concepts of Java Servlets To discuss FORMs and Java Servlets Review more complex servlets Reference: DDEA Ch.7, Sharma p.110-122 and EJP (Ch.4) p.48-63 2002 Daniel L. Silver 2 The Problems with Applets and CGI Java Applets have three major drawbacks: – Take time to load onto client – May not work as planned (depends on JVM) – Security risk for client Server-side code is preferred for business logic CGI allows an application to run on server but creates server performance problems Most notably each time a separate process must be spawned (for Java a separate JVM is run) 2002 Daniel L. Silver 3 Enter Servlets Servlets overcome this problem Servlets relie on a Servlet Engine (Application Server) to manage multiple requests for the same application Java servlets have the advantages of Java applets but run on the server side The Jserv engine from SUN was the first Java servlet engine 2002 Daniel L. Silver 4 Java Servlet Technology Applications run on the server. Extend functionality of a web server and provide structure for a business environment. Servlets can be operating system and hardware platform independent. Servlets process HTTP/HTML requests with all the benefits of the mature Java language (portability, performance, reusability, and crash protection.) 2002 Daniel L. Silver 5 Applet vs. Servlet Applet Servlet – Client side. – Takes time to load. – JVM varies with browser. – Require compatible browser – Security issue if client side program needs to access sensitive data via browser. 2002 Daniel L. Silver – – – – Server side. Runs on request. Constant JVM No GUI required, can generate HTML, Javascript, Applet code – Server side programming and data access preferred for business applications. 6 CGIs vs. Servlets CGI programs Servlets – Separate process for each CGI program – Mod_perl and FastCGI improves performance of CGI but not to level of servlets – Have difficult time maintaining state across requests 2002 Daniel L. Silver – Run under single JVM (better performance) – Servlets loaded into memory with first call, and stay in memory – Have built in state preservation methods – Java's inherent security – Proprietary source code can be retained by only giving up *.class files to the server 7 Websphere Java Servlet Request Processing Client http://eagle.acadiau.ca/demo/servlet/HelloWorld Browser HTML Internet Tomcat App. Server JVM HTTP Server servlet/HelloWorld HelloWorld.class demo/servlet/ equates to …/demo/WEB-INF/classes/HelloWorld.class 2002 Daniel L. Silver 8 Servlet Life-Cycle The servlet is initialized and is loaded into the servers memory The servlet resides in memory and responds to requests from clients The servlet is destroyed (by the server engine) NOTE: When you update a servlet, Tomcat checks the modification date of the .class file and if it is more recent then the running version, the servlet is destroy and then re-initialize 2002 Daniel L. Silver 9 Fundamental parts of a Servlet 1. import javax.servlet.*; and import javax.servlet.http.*; - packages of servlet classes that implement the Java Servlet API 2. public class HelloWorld extends HttpServlet { - extends the HTTPServlet class 3. init() -intializes servlet after loading into memory - place to perform operations done only once at start-up - reading a current properties - clearing log files, notifying other services that the servlet is running 4. service(), doGet(), doPost() - this is where work is done - each time the servlet is called a new thread of execution begins - input is passed to the Java code via either HTTP GET or POST commands 5. destoy() - executed when server engine calls servlet to terminate - used to flush I/O, disconnect from database 2002 Daniel L. Silver 10 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld 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>"); }2002 Daniel L. Silver } Structure of a Servlet: HelloWorld 11 Some Basic Java Servlets Several examples … http://eagle.acadiau.ca/demo/servletexample.html http://eagle.acadiau.ca:8080/examples/servlets/index.html http://www.servlets.com/jservlet2/examples/index.html Java Developers Almanac: http://javaalmanac.com/egs/javax.servlet/pkg.html 2002 Daniel L. Silver 12 FORMS and Java Servlets Within HTML code on the client: A FORM tag similar to CGI is used to pass data from a browser to a Java servlet: … <FORM ACTION = 'http://eagle/store35/servlet/ShowFormVariables' METHOD=‘GET'> Enter your first name <INPUT TYPE=text NAME='firstName' VALUE=' '><br> … This will cause the web browser to generate a URL (if “Danny” is entered): http://eagle/demo/servlet/ShowFormVariables?firstName=Danny 2002 Daniel L. Silver 13 FORMS and Java Servlets Within Java code on the server: The doGet servlet method is used to obtain the two objects: – HttpServletRequest and HttpServletResponse HttpServletRequest is used to get the INPUT parameter names and values: – getParameterNames() – getParameterValues() HttpServletResponse used to return HTML response – printlin() 2002 Daniel L. Silver 14 An Example of a Java Servlet handling FORM input From page 117 of Sharma – servlet that returns browser input passed via a FORM tag http://eagle.acadiau.ca/danstech/ShowFormVariables.html Here is the java source code : http://eagle/danstech/ShowFormVariables.java 2002 Daniel L. Silver 15 A more complex Servlet Example AddToShopppingCart.java from your store provides a larger and more complex servlet example Also see ClearShoppingCart.java 2002 Daniel L. Silver 16 Problems with Servlets ? Do you see any problems with servlets from an E-Commerce perspective? 2002 Daniel L. Silver 17 Additional Web Info http://java.sun.com/products/servlet/ http://developer.java.sun.com/developer/onlineTra ining/Servlets/Fundamentals/ http://hotwired.lycos.com/webmonkey/99/07/inde x3a.html?tw=programming http://www.servlets.com/jservlet2/index.html http://www.servlets.com/jservlet2/examples/index. html 2002 Daniel L. Silver 18 THE END danny.silver@acadiau.ca