39 Slide
39 Slide
39 Slide
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To explain how a servlet works (39.2). To create/develop/run servlets (39.3). To deploy servlets on application servers such as Tomcat (39.3). To describe the servlets API (39.4). To create simple servlets (39.5). To create and process HTML forms (39.6). To develop servlets to access databases (39.7). To use hidden fields, cookies, and HttpSession to track sessions (39.8). To send images from servlets (39.9).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Web Browser
Web Server
/htdocs/index.html
HTML Page
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
CGI
The Common Gateway Interface, or CGI, was proposed to generate dynamic Web contents. The interface provides a standard framework for Web servers to interact with external programs, known as the CGI programs.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Web Browser
Web Server
/htdocs/index.html /cgi-bin/getBalance.cgi
Generate Response
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Query String
The URL query string consists of the location of the CGI program, parameters and their values. http://www.webserverhost.com/cgi-bin/ getBalance.cgi?accountId=scott+smith&password=tiger The ? symbol separates the program from the parameters. The parameter name and value are associated using the = symbol. The parameter pairs are separated using the & symbol. The + symbol denotes a space character.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
HTML Forms
HTML forms enable you to submit data to the Web server in a convenient form. The form can contain text fields, text area, check boxes, combo boxes, lists, radio buttons, and buttons.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
To run Java servlets, you need a servlet container. Many servlet containers are available. Tomcat, developed by Apache (www.apache.org), is a standard reference implementation for Java servlet 2.2 and Java Server Pages 1.1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
Servlet
GenericServlet
HttpServlet
ServletRequest
HttpServletRequest
ServletResponse javax.servlet.*
HttpServletResponse javax.servlet.http.*
12
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
/**Invoked to respond to incoming requests*/ public void service(ServletRequest p0, ServletResponse p1) throws ServletException, IOException; /**Invoked to release resource by the servlet*/ public void destroy();
/**Return information about the servlet*/ public String getServletInfo(); /**Return configuration objects of the servlet*/ public ServletConfig getServletConfig();
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
Servlet Life-Cycle
1. The init method is called when the servlet is first created, and is not called again as long as the servlet is not destroyed. This resembles the applets init method, which is invoked when the applet is created, and is not invoked again as long as applet is not destroyed.
2. The service method is invoked each time the server receives a request for the servlet. The server spawns a new thread and invokes service.
3. The destroy method is invoked once all threads within the servlet's service method have exited or after a timeout period has passed. This method releases resources for the servlet.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
14
15
16
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
Creating Servlets
Servlets are opposites of the Java applets. Java applets run from a Web browser on the client side. To write Java programs, you define classes. To write a Java applet, you define a class that extends the Applet class. The Web browser runs and controls the execution of the applet through the methods defined in the Applet class. Similarly, to write a Java servlet, you define a class that extends the HttpServlet class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
18
19
TimeForm
Run
20
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
21
SimpleRegistration
Run
22
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Session Tracking
Web servers use Hyper-Text Transport Protocol (HTTP). HTTP is a stateless protocol. The HTTP Web server cannot associate requests from a client together. Each request is treated independently by the Web server. This protocol works fine for simple Web browsing, where each request typically results in an HTML file or a text file being sent back to the client. Such simple requests are isolated. However, the requests in interactive Web applications are often related.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
23
What is a Session ?
A session can be defined as a series of related interactions between a single client and the Web server over a period of time. To track data among requests in a session is known as session tracking.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
24
25
26
Registration
Run
27
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
28
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29
This obtains the session or creates a new session if the client does not have a session on the server. The HttpSession class provides the methods for reading and storing data to the session, and for manipulating the session.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
30
31
ImageContent
Run
32
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
ImageContentWithDrawing
Run
33
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
MixedContent
Run
34
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807