Académique Documents
Professionnel Documents
Culture Documents
DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
This line begins the overridden method doGet to respond to the get requests. In this case, the HttpServletRequest object parameter represents the clients request and the HttpServletResponse object parameter represents the servers response to the client. If method doGet is unable to handle a clients request, it throws an exception of type javax.servlet.ServletException. If doGet encounters an error during stream processing (when reading from the client or writing to the client), it throws a java.io.IOException.
The first line above uses the response objects setContentType method to specify the content type of the document to be sent as the response to the client. This enables the client browser to understand and handle the content it receives from the server. The content type is also referred to as the MIME (Multipurpose Internet Mail Extension) type of the data. In this servlet, the content type is text/html to indicate to the browser that the response is an XHTML document. The second line above uses the response objects getWriter method to obtain a reference to the PrintWriter object that enables the servlet to send content to the client. If the response is binary data, like an image, method getOutputStream would be used to obtain a reference to a ServletOutputStream object.
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
The server finds the file and returns it to the browser. The browser then displays the file for the user.
Static information is stored in HTML files. The HTML files can be updated, but at any given time, every request for the same file returns exactly the same content.
CGI Programming
When a web server receives a request from a browser it passes it to the CGI program. The CGI program processes the request and generates a response at runtime. CGI programs can be written in any language, but Perl is the most popular choice.
Web Server Host
Web Browser
HTML page returned spawn CGI process
Web Server
generate response
Servlet Container
Database
The server that executes a servlet is referred to as the servlet container or servlet engine.
Client Browser
Request
Web Server
Respons e
Servlet
Internet
servlet/Hello World
HelloWorld.class
Advantages of Java ServletsThe following sections discuss some of the advantages offered by Servlet.
1. Efficiency A Servlets initialization code is executed only the first time the Web server loads it. Once the Servlet is loaded, it is only a matter of calling a service method to handle new requests. This is a much more efficient technique than loading a completely new executable with every request. 2. Persistency Servlets can maintain state between requests. Once a Servlet is loaded, it stays resident in memory while serving incoming requests
3.Portability Servlets are developed using Java; therefore, they are portable. This portability enables Servlets to be moved to a new operating system without changing the source. You can take code that was compiled on a Windows NT platform and move it to a Solaris box without making any changes.
4.Robustness Because Servlets are developed with access to the entire JDK, they are very powerful and robust solutions. Java provides a well-defined exception hierarchy for error handling. It has a garbage collector to prevent problems with memory leaks.
5.Extensibility Another-advantage Servlets gain by being developed with an objectoriented language such as Java is that they can be extended and polymorphed into new objects that better suit your needs. A good example of this is an online catalog.
6.Security Servlets run on the-server side, inheriting the security provided by the Web server. Servlets can also take advantage of the Java Security Manager.
It is important that you write your destroy() method to avoid closing any necessary resources until all service() calls have
Architecture
Servlet
Generic Servlet
HttpServlet
YourOwnServlet
Servlet
Abstract class
GenericServlet
Abstract class
HttpServlet
Concrete class
Your Servlet
When is it called
Do you override it
init()
The container calls the init() before the servlet can service any client requests.
When a new request for that servlet comes in.
Possibly
service()
doGet() or doPost()
The service() To handle the method invokes it business logic. based on the HTTP method from the request.
Always
Servlet Lifecycle Thread handling The Container runs multiple threads to process multiple requests to a single servlet.
Container
Client A Servlet
Client B
Thread A
Thread B
response
request
request
response
Servlet Interface
The servlet packages define two abstract classes that implement interface Servlet class GenericServlet (from the package javax.servlet) and class HttpServlet (from the package javax.servlet.http). These classes provide default implementations of some Servlet methods. Most servlets extend either GenericServlet or HttpServlet and override some or all of their methods. The GenericServlet is a protocol-indpendent servlet, while the HttpServlet uses the HTTP protocol to exchange information between the client and server.
Servlets typically extend class HttpServlet, which overrides method service to distinguish between the various requests received from a client web browser. The two most common HTTP request types (also known as request methods) are get and post.
A get request retrieves information from a server. Typically, an HTML document or image.
HTTPServlet Class
A post request sends data to a server. Typically, post requests are used to pass user input to a data-handling process, store or update data on a server, or post a message to a news group or discussion forum.
Class HttpServlet defines methods doGet and doPost to respond to get and post requests from a client.
HTTPServletRequest Interface
Every invocation of doGet or doPost for an HttpServlet receives an object that implements interface HttpServletRequest. The servlet container creates an HttpServletRequest object and passes it to the servlets service method, which in turn, passes it to doGet or doPost. This object contains the clients request and provides methods that enable the servlet to process the request.
HTTPServletRequest Methods
Cookie[] getCookies() returns an array of Cookie objects stored on the client by the server. Cookies are used to uniquely identify clients to the server. String getLocalName() gets the host name on which the request was received. String getLocalAddr() gets the IP address on which the request was received. int getLocalPort() gets the IP port number on which the request was received.
String getParameter( String name) gets the value of a parameter set to the servlet as part of a get or post request.
HTTPServletResponse Interface
Every invocation of doGet or doPost for an HttpServlet receives an object that implements interface HttpServletResponse. The servlet container creates an HttpServletResponse object and passes it to the servlets service method, which in turn, passes it to doGet or doPost. This object provides methods that enable the servlet to formulate the response to the client.
HTTPServletResponse Methods
void addCookie (Cookie cookie) adds a Cookie to the header of the response to the client. ServletOutputStream getOutputStream() gets a byte-based output stream for sending binary data to the client. PrintWriter getWriter() gets a character-based output stream for sending text data (typically HTML formatted text) to the client. void SetContentType (String type) specifies the content type of the response to the browser to assist in displaying the data. void getContentType() gets the content type of the response.
HTTPServletResponse Methods
void addCookie (Cookie cookie) adds a Cookie to the header of the response to the client. ServletOutputStream getOutputStream() gets a byte-based output stream for sending binary data to the client. PrintWriter getWriter() gets a character-based output stream for sending text data (typically HTML formatted text) to the client. void SetContentType (String type) specifies the content type of the response to the browser to assist in displaying the data. void getContentType() gets the content type of the response.
Creating a Servlet
Extend HTTPServlet
Implement doGet Implement doPost The methods should get an input (the HTTP request) Should create an output (the HTTP response)
Creating a Servlet
ServletRequest HTTPServletRequest
Implement doGet
Implement doPost
ServletResponse
HTTPServletResponse
Deployment Descriptor
A deployment descriptor (DD) refers to a configuration file
In the Java EE, a deployment descriptor describes how a component, module or application should be deployed.
It directs a deployment tool to deploy a module or application with specific container options, security settings and describes specific configuration requirements. XML is used for the syntax of these deployment descriptor files.
For web applications, the deployment descriptor must be called web.xml and must reside in the WEB-INF directory in the web application root.
<html> <head> <title>Student Form</title> </head> <body> <h2 align="center">TEST GET METHOD - STUDENT FORM</h2> <form action="http://rs-hp:8080/Webproject/servlet/gg1" method="get"> ROLL NO<input type="text" size="30" name="Roll_Number"> NAME<input type="text" size="30" name="Name"> AGE<input type="text" size="30" name="Age"> <input type="submit" value="SUBMIT YOUR MEMBERSHIP"> </form> </body> </html>