Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jagdish Gangolly State University of New York at Albany

Similar presentations


Presentation on theme: "Jagdish Gangolly State University of New York at Albany"— Presentation transcript:

1 Jagdish Gangolly State University of New York at Albany
Java Servlets Jagdish Gangolly State University of New York at Albany Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

2 Java Servlets Types of web containers Structure of web applications
An Example Servlet Life cycle Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

3 Types of web containers
Web containers built-in to a J2EE application server: Weblogic, Inprise, iPlanet, WebSphere Web containers built into a webserver: Sun’s Java Webserver, Jakarta Tomcat Web container in a separate runtime: Servlet engines as plug-ins to web servers such as IIS, Apache, etc. (Jrun, ServletExec, …) Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

4 Structure of web applications
Public directory: where all html files are kept WEB-INF/web.xml file: Deployment descriptor WEB-INF/classes directory: compiled classes of servlets and their utility classes WEB-INF/lib directory: packaged JAR files Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

5 An Example What the application does: Steps:
Prompts the user for a name and an address Prints a welcome greeting based on the time of the day Steps: First Contact html page (requesting info) Servlet to process request and generate html page Deployment descriptor Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

6 1. Contact html page <HTML> <HEAD>
<TITLE>ProJava Registration</TITLE> </HEAD> <BODY> <H1>Welcome</H1> <FORM ACTION="/greeting/servlet/GreetingServlet" METHOD="POST"> <P>Your Name <INPUT TYPE="text" SIZE="40" NAME="name"></P> <P>Your <INPUT TYPE="text" SIZE="40" NAME=" "> <INPUT TYPE="submit" VALUE="Submit"></P> </FORM> </BODY> </HTML> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

7 The Servlet I Importing packages // Import Servlet packages
import javax.servlet.*; import javax.servlet.http.*; // Import other Java packages import java.io.*; import java.util.*; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

8 The Servlet II The Servlet class
public class GreetingServlet extends HttpServlet { } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

9 The Servlet II 3. Implement the doPost method
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

10 The Servlet III Implement the doPost method: Details
1. Get parameters from request 2. Compute greeting message 3. Set MIMI type for response & obtain printwriter object for generating response 4. Write response html content Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

11 The Servlet IV 1. Get parameters from request
String name = request.getParameter("name"); String = request.getParameter(" "); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

12 The Servlet V 2. Compute greeting message String message = null;
GregorianCalendar calendar = new GregorianCalendar(); if(calendar.get(Calendar.AM_PM) == Calendar.AM) { message = "Good Morning"; } else { message = "Good Afternoon"; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

13 The Servlet VI 3. Set MIMI type for response & obtain printwriter object for generating response response.setContentType("text/html"); PrintWriter out = response.getWriter(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

14 The Servlet VI 4. Write response html content
out.println("<HTML>"); out.println("<BODY>"); out.println("<P>" + message + ", " + name + "</P>"); out.println("<P> Thanks for registering your (" + + ") with us.</P>"); out.println("<P> - The Pro Java Team. </P>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

15 The Servlet VII: The Whole Program
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class GreetingServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String = request.getParameter(" "); String message = null; GregorianCalendar calendar = new GregorianCalendar(); if(calendar.get(Calendar.AM_PM) == Calendar.AM) { message = "Good Morning"; } else {message = "Good Afternoon"; } response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY>"); out.println("<P>" + message + ", " + name + "</P>"); out.println("<P> Thanks for registering your (" + + ") with us.</P>"); out.println("<P> - The Pro Java Team. </P>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

16 Deployment descriptor
<?xml version="1.0" encoding="ISO "?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" " <web-app> <servlet> <!-- Servlet alias --> <servlet-name>Registration</servlet-name> <!-- Fully qualified Servlet class --> <servlet-class>GreetingServlet</servlet-class> </servlet> </web-app> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

17 Servlet Lifecycle (Fig on p.356)
Creation of thread pool Instantiation of servlet Call to init() method (initialisation) Allocation of requests to threads Call to service() method Terminate threadpool Call to destroy() method Termination of servlet (garbage collection) Container shutdown Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

18 Servlet Lifecycle: An example I
Freak servlet: Life cycle Steps: First unavailable html page (UnavailableException()) Servlet to process request and generate response Deployment descriptor Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

19 Servlet Lifecycle: An example II
Unavailable html page: <HTML> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <HEAD> <TITLE>FreakServlet Unavailable</TITLE> </HEAD> <BODY> <H1>FreakServlet Unavailable</H1> <P>FreakServlet is temporarily unavailable. Please try <A HREF="/lifeCycle/servlet/freak">again</A>.</P> </BODY> </HTML> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

20 Servlet request and generate response I
2. The FreakServlet Importing packages The Servlet class Instantiation of servlet (constructor) createState() method Init() method to initialise (ready for requests) doGet() method to build response to queries (service) Destroy() method Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

21 Importing packages // Import servlet packages import javax.servlet.*;
import javax.servlet.http.*; // Import other Java packages import java.io.*; import java.util.*; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

22 The Servlet class public class FreakServlet extends HttpServlet {
java.util.Vector states; java.util.Random random; int waitInterval; public static final int DEFAULT_WAIT_INTERVAL = 10; } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

23 Instantiation of servlet (constructor)
public FreakServlet() { states = new java.util.Vector(); random = new java.util.Random(); waitInterval = DEFAULT_WAIT_INTERVAL; states.add(createState("Instantiation")); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

24 createState() method private String createState(String message) {
return "[" + (new java.util.Date()).toString() + "] " + message; } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

25 Init() method to initialise (ready for requests)
public void init() throws ServletException { states.add(createState("Initialization")); String waitIntervalString = getServletConfig().getInitParameter("waitInterval"); if (waitIntervalString != null) { waitInterval = new Integer(waitIntervalString).intValue(); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

26 doGet() method to build response to queries (service)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(random.nextBoolean()) { // Not available for waitInterval seconds. states.add(createState("Unavailable from doGet")); throw new UnavailableException("Unavailable from doGet", waitInterval); } states.add(createState("Service")); //see HTML Generation on next slide Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

27 HTML Page generation response.setContentType("text/html");
PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"); out.println("<HEAD><TITLE>"); out.println("FreakServlet: State History"); out.println("</TITLE></HEAD>"); out.println("<BODY>"); out.println("<H1>FreakServlet: State History</H1>"); out.println("<a href=\"/lifeCycle/servlet/freak\">Reload</a></p>"); for(int i = 0; i < states.size(); i++) { out.println("<p> " + states.elementAt(i) + "</p>"); } out.println("</BODY></HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

28 Destroy() method public void destroy() {
states.add(createState("Destroy")); log("Flushing state history of LifeCycleTest servlet."); for(int i = 0; i < states.size(); i++) { log(states.elementAt(i).toString()); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

29 Deployment descriptor
<?xml version="1.0" encoding="ISO "?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" " <web-app> <servlet> <servlet-name>freak</servlet-name> <servlet-class>FreakServlet</servlet-class> <init-param> <param-name>waitInterval</param-name> <param-value>5</param-value> </init-param> </servlet> <error-page> <exception-type>javax.servlet.UnavailableException</exception-type> <location>/unavailable.html</location> </error-page> </web-app> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018


Download ppt "Jagdish Gangolly State University of New York at Albany"

Similar presentations


Ads by Google