![Page 1: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/1.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Java Servlets
![Page 2: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/2.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 1:Introduction
to Java Servlets
![Page 3: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/3.jpg)
Objectives
• Describe the differences between servlets and other Web application technologies
• Explain the difference between the GET and POST methods of making an HTTP request
• Create a simple servlet using GET• Create a simple servlet using POST• Define a simple deployment descriptor
![Page 4: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/4.jpg)
Introductionto Web Applications
• Web application technologies– Common Gateway Interface (CGI)– Server extensions– Server-side scripting– JavaServer Pages– Java servlets
![Page 5: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/5.jpg)
HypertextTransfer Protocol
• The GET method• The POST method• Additional methods
![Page 6: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/6.jpg)
Writing aSimple Servlet
javax.servlet.Servlet
GenericServlet HttpServlet
![Page 7: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/7.jpg)
Respondingto Form Data
• FormServlet• getParameter method• Using the POST method
![Page 8: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/8.jpg)
DeploymentDescriptors
• XML files conforming to Sun Microsystems DTD– Describes servlets contained within a Web
application
![Page 9: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/9.jpg)
Summary
Describe the differences between servlets and other Web application technologies
Explain the difference between the GET and POST methods of making an HTTP request
Create a simple servlet using GET Create a simple servlet using POST Define a simple deployment descriptor
![Page 10: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/10.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 2:The Servlet Life Cycle
![Page 11: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/11.jpg)
Objectives
• Describe the servlet life cycle• Create init and destroy methods• Retrieve servlet initialization parameters• Use the SingleThreadModel interface• Retrieve CGI environment variables• Retrieve and use the ServletContext object• Use temporary files
![Page 12: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/12.jpg)
TheMultithreaded Model
• Servlets typically operate in a multithreaded environment– The Web server usually instantiates only
one instance of a servlet to serve all clients• Deployment descriptors and the multithreaded
model
![Page 13: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/13.jpg)
TheSingle Thread Model
• The Web server guarantees that no two threads will ever operate concurrently on the same servlet instance
• To designate servlets to use the single thread model, implement the following interface:– javax.servlet.SingleThreadModel
![Page 14: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/14.jpg)
The initand destroy Methods
• The init method– Initialization parameters and the
deployment descriptor• The destroy method
![Page 15: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/15.jpg)
CGIEnvironment Variables
• AUTH_TYPE• CONTENT_LENGTH• CONTENT_TYPE• HTTP_ACCEPT• HTTP_REFERER• HTTP_USER_AGENT• PATH_INFO• PATH_TRANSLATED• QUERY_STRING
• REMOTE_ADDR• REMOTE_HOST• REMOTE_USER• REQUEST_METHOD• SCRIPT_NAME• SERVER_NAME• SERVER_PROTOCOL• SERVER_PORT
![Page 16: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/16.jpg)
The ServletContext
• Methods for obtaining server information• Using temporary files
![Page 17: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/17.jpg)
Summary
Describe the servlet life cycle Create init and destroy methods Retrieve servlet initialization parameters Use the SingleThreadModel interface Retrieve CGI environment variables Retrieve and use the ServletContext object Use temporary files
![Page 18: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/18.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 3:Responding to a Request
![Page 19: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/19.jpg)
Objectives
• Use client-side caching• Use client pull to update a client• Redirect the client to another URL• Use persistent connections• Use response status codes• Return a file to a client• Dynamically generate images
![Page 20: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/20.jpg)
Controllingthe Client
• Using client-side caching• Using client pull• Redirecting the client
![Page 21: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/21.jpg)
PersistentConnections
public class PersistentConnection extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException
{resp.setBufferSize(32 * 1024);resp.setContentType("text/html");PrintWriter out = resp.getWriter();
// Generate a response}
}
![Page 22: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/22.jpg)
Status Codes
• Status code constants• sendError method used to set status code
![Page 23: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/23.jpg)
Multimedia Content
• Returning a file• Dynamically generating images
![Page 24: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/24.jpg)
Summary
Use client-side caching Use client pull to update a client Redirect the client to another URL Use persistent connections Use response status codes Return a file to a client Dynamically generate images
![Page 25: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/25.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 4:Servlet Sessions
![Page 26: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/26.jpg)
Objectives
• Track a session using hidden form fields• Track a session using URL rewriting• Track a session using cookies
![Page 27: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/27.jpg)
HiddenForm Fields
<INPUT TYPE="HIDDEN" NAME="SID" VALUE="1234567890">
Hidden form field named "SID" with an assigned value of
"1234567890"
![Page 28: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/28.jpg)
URLRewriting
• Servlets can build URLS that add information in the form of additional path information
![Page 29: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/29.jpg)
Cookies
• Small pieces of information transmitted from a Web server to a Web browser
• Represented in Java using the Cookie class
![Page 30: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/30.jpg)
Summary
Track a session using hidden form fields Track a session using URL rewriting Track a session using cookies
![Page 31: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/31.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 5:Authentication
and Security
![Page 32: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/32.jpg)
Objectives
• Authenticate a user using HTTP-based authentication
• Authenticate a user using a form• Use Secure Sockets Layer to improve security
![Page 33: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/33.jpg)
HTTP-BasedAuthentication
• Users• The deployment descriptor• Servlets and authentication
![Page 34: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/34.jpg)
FormAuthentication
• Requires modification of the deployment descriptor– The login-config element must be
modified to indicate that form authentication is to be used and to provide the URL for a login page and login error page
![Page 35: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/35.jpg)
Summary
Authenticate a user using HTTP-based authentication
Authenticate a user using a form Use Secure Sockets Layer to improve security
![Page 36: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/36.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 6:Inter-Servlet
Communication
![Page 37: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/37.jpg)
Objectives
• Share data with another servlet• Handle a single request using multiple
servlets
![Page 38: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/38.jpg)
Sharing Data
• Data-sharing methods of the ServletContext interface
• Sharing data with another ServletContext
![Page 39: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/39.jpg)
Dispatchingto Another Servlet
• The forward method• The include method
![Page 40: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/40.jpg)
Summary
Share data with another servlet Handle a single request using multiple
servlets
![Page 41: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/41.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 7:Building EnterpriseWeb Applications
![Page 42: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/42.jpg)
Objectives
• Use JNDI to look up EJBs, resource factories and environment entries
• Write servlets for use in a distributed environment
• Use JavaMail to send e-mail
![Page 43: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/43.jpg)
Java Servlets and JNDI
• Referencing EJBs• Referencing resource factories• Referencing environment entries
![Page 44: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/44.jpg)
Clusteringand Java Servlets
• Clustering styles• Developing distributable servlets
![Page 45: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/45.jpg)
JavaMail
• Add-on API that creates a full-fledged POP/iMAP client– Session class– getDefaultInstance static method
![Page 46: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/46.jpg)
Summary
Use JNDI to look up EJBs, resource factories and environment entries
Write servlets for use in a distributed environment
Use JavaMail to send e-mail
![Page 47: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/47.jpg)
Copyright © 2002 ProsoftTraining. All rights reserved.
Lesson 8:Internationalization
![Page 48: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/48.jpg)
Objectives
• Use the Unicode escape sequence to specify special Latin characters
• Use alternative character sets to generate a non-Latin character response
![Page 49: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/49.jpg)
The LatinCharacter Set
• Non-English Latin characters• Languages and language codes
![Page 50: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/50.jpg)
Non-LatinCharacter Sets
• Arabic• Chinese• Japanese• Korean• Russian
![Page 51: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/51.jpg)
Summary
Use the Unicode escape sequence to specify special Latin characters
Use alternative character sets to generate a non-Latin character response
![Page 52: Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets](https://reader035.vdocument.in/reader035/viewer/2022062804/5697bf841a28abf838c87058/html5/thumbnails/52.jpg)
Java Servlets
Introduction to Java Servlets The Servlet Life Cycle Responding to a Request Servlet Sessions Authentication and Security Inter-Servlet Communication Building Enterprise Web Applications Internationalization