Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.

Similar presentations


Presentation on theme: "Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D."— Presentation transcript:

1 Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.

2 2002Daniel L. Silver2 Objectives To introduce the basic concepts of Java Servlets To introduce the basic concepts of Java Servlets To discuss FORMs and Java Servlets To discuss FORMs and Java Servlets Review more complex servlets Review more complex servlets Reference: DDEA Ch.7, Sharma p.110-122 and EJP (Ch.4) p.48-63 Reference: DDEA Ch.7, Sharma p.110-122 and EJP (Ch.4) p.48-63

3 2002Daniel L. Silver3 The Problems with Applets and CGI Java Applets have three major drawbacks: 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 Server-side code is preferred for business logic CGI allows an application to run on server but creates server performance problems 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) Most notably each time a separate process must be spawned (for Java a separate JVM is run)

4 2002Daniel L. Silver4 Enter Servlets Servlets overcome this problem Servlets overcome this problem Servlets relie on a Servlet Engine (Application Server) to manage multiple requests for the same application 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 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 The Jserv engine from SUN was the first Java servlet engine

5 2002Daniel L. Silver5 Java Servlet Technology Applications run on the server. Applications run on the server. Extend functionality of a web server and provide structure for a business environment. Extend functionality of a web server and provide structure for a business environment. Servlets can be operating system and hardware platform independent. 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.) Servlets process HTTP/HTML requests with all the benefits of the mature Java language (portability, performance, reusability, and crash protection.)

6 2002Daniel L. Silver6 Applet vs. Servlet Applet Applet –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. Servlet –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.

7 2002Daniel L. Silver7 CGIs vs. Servlets CGI programs CGI programs –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 Servlets –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

8 2002Daniel L. Silver8 Websphere Java Servlet Request Processing Internet Browser Client HTTP Server HelloWorld.class http://eagle.acadiau.ca/demo/servlet/HelloWorld Tomcat App. Server servlet/HelloWorld demo/servlet/ equates to …/demo/WEB-INF/classes/HelloWorld.class HTML JVM

9 2002Daniel L. Silver9 Servlet Life-Cycle The servlet is initialized and is loaded into the servers memory The servlet is initialized and is loaded into the servers memory The servlet resides in memory and responds to requests from clients The servlet resides in memory and responds to requests from clients The servlet is destroyed (by the server engine) 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

10 2002Daniel L. Silver10 Fundamental parts of a Servlet 1. import javax.servlet.*; and import javax.servlet.http.*; - packages of servlet classes that implement the Java Servlet API - 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

11 2002Daniel L. Silver11 Structure of a Servlet: HelloWorld import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException throws IOException, ServletException { response.setContentType("text/html"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter(); out.println(" "); out.println(" "); out.println(" Hello World! "); out.println(" Hello World! "); out.println(" "); out.println(" "); out.println(" Hello World! "); out.println(" Hello World! "); out.println(" "); out.println(" "); }}

12 2002Daniel L. Silver12 Some Basic Java Servlets Several examples … 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: Java Developers Almanac: http://javaalmanac.com/egs/javax.servlet/pkg.html

13 2002Daniel L. Silver13 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: Within HTML code on the client: A FORM tag similar to CGI is used to pass data from a browser to a Java servlet:… Enter your first name Enter your first name … This will cause the web browser to generate a URL (if “Danny” is entered) : This will cause the web browser to generate a URL (if “Danny” is entered) : http://eagle/demo/servlet/ShowFormVariables?firstName=Danny http://eagle/demo/servlet/ShowFormVariables?firstName=Danny

14 2002Daniel L. Silver14 FORMS and Java Servlets Within Java code on the server: The doGet servlet method is used to obtain the two objects: 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: HttpServletRequest is used to get the INPUT parameter names and values: –getParameterNames() –getParameterValues() HttpServletResponse used to return HTML response HttpServletResponse used to return HTML response –printlin()

15 2002Daniel L. Silver15 An Example of a Java Servlet handling FORM input From page 117 of Sharma – servlet that returns browser input passed via a FORM tag 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 : Here is the java source code : http://eagle/danstech/ShowFormVariables.java

16 2002Daniel L. Silver16 A more complex Servlet Example AddToShopppingCart.java from your store provides a larger and more complex servlet example AddToShopppingCart.java from your store provides a larger and more complex servlet example Also see ClearShoppingCart.java Also see ClearShoppingCart.java

17 2002Daniel L. Silver17 Problems with Servlets ? Do you see any problems with servlets from an E-Commerce perspective? Do you see any problems with servlets from an E-Commerce perspective?

18 2002Daniel L. Silver18 Additional Web Info http://java.sun.com/products/servlet/ http://java.sun.com/products/servlet/ http://java.sun.com/products/servlet/ http://developer.java.sun.com/developer/onlineTra ining/Servlets/Fundamentals/ http://developer.java.sun.com/developer/onlineTra ining/Servlets/Fundamentals/ http://developer.java.sun.com/developer/onlineTra ining/Servlets/Fundamentals/ http://developer.java.sun.com/developer/onlineTra ining/Servlets/Fundamentals/ http://hotwired.lycos.com/webmonkey/99/07/inde x3a.html?tw=programming http://hotwired.lycos.com/webmonkey/99/07/inde x3a.html?tw=programming http://hotwired.lycos.com/webmonkey/99/07/inde x3a.html?tw=programming http://hotwired.lycos.com/webmonkey/99/07/inde x3a.html?tw=programming http://www.servlets.com/jservlet2/index.html http://www.servlets.com/jservlet2/index.html http://www.servlets.com/jservlet2/index.html http://www.servlets.com/jservlet2/examples/index. html http://www.servlets.com/jservlet2/examples/index. html http://www.servlets.com/jservlet2/examples/index. html http://www.servlets.com/jservlet2/examples/index. html

19 THE END danny.silver@acadiau.ca


Download ppt "Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D."

Similar presentations


Ads by Google