Updated Servlet Notes
Updated Servlet Notes
Updated Servlet Notes
There are many interfaces and classes in the servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse etc.
What is a Servlet?
CGI technology enables the web server to call an external program and
pass HTTP request information to the external program to process the
request. For each request, it starts a new process.
Disadvantages of CGI
There are many advantages of servlet over CGI. The web container
creates threads for handling the multiple requests to the servlet. Threads
have a lot of benefits over the Processes such as they share a common
memory area, lightweight, cost of communication between the threads
are low. The basic benefits of servlet are as follows:
5) Get request is more efficient and Post request is less efficient and
used more than Post. used less than get.
Servlet Vs CGI:
Servlets can link directly to the CGI cannot directly link to Web
Web server server.
Servlets can read and Set HTTP CGI cannot read and Set HTTP
Headers Headers
Servlet Container
It provides the runtime environment for JavaEE (j2ee) applications. The
client/user can request only a static WebPages from the server. If the
user wants to read the web pages as per input then the servlet container
is used in java.
The servlet container is used in java for dynamically generate the web
pages on the server side. Therefore the servlet container is the part of a
web server that interacts with the servlet for handling the dynamic web
pages from the client.
1. Servlet API
2. Interfaces in javax.servlet package
3. Classes in javax.servlet package
4. Interfaces in javax.servlet.http package
5. Classes in javax.servlet.http package
The javax.servlet package contains many interfaces and classes that are
used by the servlet or web container. These are not specific to any
protocol.
1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletRequestWrapper
1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
4. HttpSessionListener
Classes in javax.servlet.http package
1. HttpServlet
2. Cookie
3. HttpServletRequestWrapper
4. HttpServletResponseWrapper
5. HttpSessionEvent
Servlet Interface
1. Servlet Interface
2. Methods of Servlet interface
There are 5 methods in Servlet interface. The init, service and destroy
are the life cycle methods of servlet. These are invoked by the web
container.
Method Description
TYPES OF SERVLETS:
A) Generic Servlet
B) Http Servlet
A) Generic Servlet class
1. GenericServlet class
2. Methods of GenericServlet class
3. Example of GenericServlet class
import java.io.*;
1. import javax.servlet.*;
2.
3. public class First extends GenericServlet
4. {
5. public void service(ServletRequest req,ServletResponse res)
6. throws IOException,ServletException
7. {
8.
9. res.setContentType("text/html");
10.
11. PrintWriter out=res.getWriter();
12. out.print("<html><body>");
13. out.print("<b>hello generic servlet</b>");
14. out.print("</body></html>");
15.
16. }
17. }
B) HttpServlet class
1. HttpServlet class
2. Methods of HttpServlet class
The web container maintains the life cycle of a servlet instance. Let's see
the life cycle of the servlet:
6.
1) Servlet class is loaded
The web container creates the instance of a servlet after loading the
servlet class. The servlet instance is created only once in the servlet life
cycle.
The web container calls the service method each time when request for
the servlet is received. If servlet is not initialized, it follows the first
three steps as described above then calls the service method. If servlet is
initialized, it calls the service method. Notice that servlet is initialized
only once. The syntax of the service method of the Servlet interface is
given below:
1. public void service(ServletRequest request, ServletResponse respo
nse)
2. throws ServletException, IOException
The web container calls the destroy method before removing the servlet
instance from the service. It gives the servlet an opportunity to clean up
any resource for example memory, thread etc. The syntax of the destroy
method of the Servlet interface is given below:
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
1) Cookies in Servlet:
Types of Cookie
1. Non-persistent cookie
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user
closes the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user
closes the browser. It is removed only if user logout or signout.
Advantage of Cookies
Disadvantage of Cookies
Method Description
Let's see the simple code to delete cookie. It is mainly used to logout or
signout the user.
1. Cookie ck[]=request.getCookies();
2. for(int i=0;i<ck.length;i++){
3. out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing
name and value of cookie
4. }
5.
In such case, we store the information in the hidden field and get it from
another servlet. This approach is better if we have to submit form in all
the pages and we don't want to depend on the browser.
Here, uname is the hidden field name and Vimal Jaiswal is the hidden
field value.
url?name1=value1&name2=value2&??
4) HttpSession interface:
In such case, container creates a session id for each user.The container
uses this id to identify the particular user.An object of HttpSession can
be used to perform two tasks:
1. bind objects
2. view and manipulate information about a session, such as the
session identifier, creation time, and last accessed time.
3.