SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Servlet Session, Error handling, Including another servlet, Forwarding to another servlet, Threading, Filter

 Introduction

Here, you will get the hints for implementing Servlet Session, Servlet Error handling, Including another servlet, Forwarding to another servlet, Servlet Threading, Servlet Filter, Calling Public Methods of Other Servlets.

This hints will be very uselful for everyone to do projects for security also.


 Session Management

The techniques that you can use to maintain the session information are:
  • Hidden form field (inside a servlet we use HTML coding, there use <input type=“hidden” name= “---“ value=“---“/>)
  • URL rewriting (use <a href=“http://---/anotherServlet/”>click here </a>)
  • Cookies
  • Servlet session API (use HttpSession session=req.getSession(true), then use this session and access all other session’s like setAttribute, getAttribute, etc)
 Handling Errors And Exceptions In Servlets

The two Servlet Exceptions defined in Servlet API are:
  • javax.servlet.ServletException: Defines a servlet exception that a servlet throws while processing the client request.
  • javax.servlet.UnavailableException: Defines a servlet exception that is thrown by a servlet, when a servlet is temporarily or permanently unavailable.

Note:
  • Do not try to write to the response buffer after invoking the sendError() method
  • You can write to the response after invoking setStatus() method
 Including Contents of Another Servlet

Here, we can create 2 or more servlets and they can be included according to our needs. We can also use conditions(like if condition) also to display the required servlet. Inside the main servlet and/or in other servlet we need to use the coding as follows.

RequestDispatcher dispatch = getServletContext().getRequestDispatcher(“/required-servlets-alias-name)
dispatch.include(
request, response)
(or to use request object)
ServletContext contx=getServletConfig().getServletContext();
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(
“/required-servlets-alias-name)
dispatch.include(
request, response)

Each servlet need to me complied and deployed as explained in the Map the Error Page to the Web Application.

 Forwarding Request to Other Servlets

Here, we can create 2 or more servlets and they can be included according to our needs. We can also use conditions(like if condition) also to display the required servlet. Inside the main servlet and/or in other servlet we need to use the coding as follows.

RequestDispatcher dispatch = getServletContext().getRequestDispatcher(“/required-servlets-alias-name)
dispatch.forward(
request, response)
(or to use request object)
ServletContext contx=getServletConfig().getServletContext();
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(
“/required-servlets-alias-name)
dispatch.forward(
request, response)

Each servlet need to me complied and deployed as explained in the Map the Error Page to the Web Application.

 Servlet Threading Model

There are two types of threading models
  • Multi-Threading Model
  • Single-Threading Model

In Multi-Threading Model, the Web container starts a new thread each time a client sends a request to the servlet. This means there can be multiple threads accessing your servlet, simultaneously.

In Single-Threading Model, the Web container creates a pool of servlet instances and assigns one instance per request. If the number of requests exceeds the number of instances in the pool, the requests are queued. You can specify that your servlet needs to be executed in the single-thread model by implementing the SingleThreadModel interface in your servlet. Implementing the SingleThreadModel interface of the javax.servlet package ensures that only a single thread will execute inside the service() method of your servlet.

Note: Implementing the SingleThreadModel interface in your servlet results in degradation of server performance. This is because the server needs to create separate instances for each client requests. In addition, implementing SingleThreadModel does not ensure synchronization of access to shared resources, such as class variables inside a servlet.

By default, Multi-threading Model is used and also it is best.
To use Single-Threading Model, use synchronized. Also can synchronize service() and doXXX() methods

public synchronized void setCount()
{
count++;
}
 Servlet Filters

The servlet API provides the Filter interface, FilterConfig interface, and FilterChain interface of the javax.servlet package that you can use to develop filters. To develop a filter, you need to implement the Filter interface in your filter class.

 Calling Public Methods of Other Servlets

A servlet can call the public methods of other servlets, provided both the servlets are running within the same application. You need to know the name of the servlet whose public method you want to call. Next, you need to retrieve the context in which the servlet is running, using the getServletContext() method of the ServletContext interface. Now, you can call the getServlet(String name) method on the context object by passing the name of the servlet as an argument. You can use the following code snippet to call the public method of another servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletContext sc = getServletContext();
SimpleServlet simple_ref = (SimpleServlet)sc.getServlet("simple");
/* "simple" is the alias name of the SimpleServlet servlet that you specify in the alias name field while deploying the SimpleServlet servlet. */
out.println (simple_ref.getDetails()); /* getDetails() is an arbitrary method of SimpleServlet servlet */
}



 Click for Next Topic
<- PREVIOUSNEXT ->