Skip to main content

Quick Reference to JAVA Servlets


I am writing these quick reference blogs for those who wants to brushup the ideas of each topic. This one will lead you through the basic concepts of JAVA Servlets.

How this quick reference guideline is different from the numerous other docs available ?  ,  please read on to get an idea :

Servlet Lifecycle :
  1. The container will identify the servlet based on the URL
  2. Servlet class loading
  3. Servlet instantaition
  4. Call init method
  5. Create a servlet thread for the current request.
  6. Before calling service(), will create the request and response objects
  7. Call service method
  8. Service method will identify whether to call doGet or doPost and call it
  9. Call destroy method

Different servlet objects :
  1. A sinlge servlet instance per JVM ( except for SingleTheadModel)
  2. A sinlge HttpSession per web application ( session activation and passivation)
  3. A sinlge ServletContext per JVM
  4. A sinlge ServletConfig per servlet
  5. A sinlge ServletRequest per servlet request

Important Servlet rq and rs methods :

javax.servlet.Servlet<<Interface>>
      <-- javax.servlet.GenericServlet
                <-- javax.servlet.HttpServlet

javax.servlet.ServletRequest<<Interface>>
        <-- javax.servlet.HttpServletRequest<<Interface>>

getParameter      getCookies        getRequestDispatcher
getParameterValues         getHeader             getSession           getMethod
getParameterNames               getIntHeader           getAttribute
getRemotePort --> remote to servlet is client. So this is client port.
getServerPort --> port to which request send ( eg:8080)
getLocalPort --> port for each thread to handle request

javax.servlet.ServletResponse<<Interface>>
      <-- javax.servlet.HttpServletResponse<<Interface>

setContentType            sendRedirect             setHeader
addCookie      encodeURL         addHeader
setIntHeader
getWriter      -->   PrintWriter  --> .print method
getOutputStream --> ServletOutputStream --> .write method


Simple Servlet class to get one parameter and a colection of parameters :

public class ServletOne extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response)
                                                                       throws IOException, ServletException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String userName = request.getParameter("userName");
            String[] paramValues = request.getParameterValues("userNames");
            Enumeration parameterNames = request.getParameterNames();
            while(parameterNames.hasMoreElements()){
                    out.println("<br>request.parameterNames="+parameterNames.nextElement());
            }
            out.print("<br>request.getRemotePort()="+request.getRemotePort());
     }
}


General purpose of Servlet methods and objects :

public void init() --> usually to handle the ServletConfig.
public void destroy () --> commonly used for activities like close DB connections opened by the servlet.
ServletContext.InitParameter --> like database lookup name, EJB bean JNDI names
ServletContext.attribute --> like databaseConnection, concurrentUsers
HttpSession.attribute --> like shopping cart

Sample web.xml :

<web-app>

   <context-param>
       <param-name>bankURL</param-name>
       <param-value>www.myBank.com</param-value>
   </context-param>

   <servlet>
        <servlet-name>SampleServlet</servlet-name>
        <servlet-class>jspAndServlet.SampleServlet</servlet-class>
        <init-param>
                 <param-name>bankName</param-name>
                 <param-value>MyBank</param-value>
         </init-param>
   </servlet>

   <servlet-mapping>
         <servlet-name>SampleServlet</servlet-name>
         <url-pattern>/Serv1</url-pattern> --> possible to use wildcards
   </servlet-mapping>

   <session-config>
          <session-timeout>5</session-timeout>
    </session-config>

    <listener>
          <listener-class>listeners.ContextListener</listener-class>
    </listener>

    <welcome-file-list>
           <welcome-file>TestLogIn.html</welcome-file>
           <welcome-file>index.html</welcome-file>
     </welcome-file-list>

</web-app>


Servlet and RequestDispatcher :

RequestDispatcher rd = request.getRequestDispatcher("FirstJSP.jsp");   --> Relative or absolute path
rd.forward(request,response);

or

RequestDispatcher rd = getServletContext().getRequestDispatcher("FirstJSP.jsp");  --> Absolute path
rd.forward(request,response);

  • When we use "rd.include", control is retained by the calling resource.
  • When we use "rd.include", not full HttpServletResponse functionality is passed to the called resource.
  • Remember that you cannot forward the request if you have already committed a reponse like "out.print" or "os.flush"
  • To pass some data to JSP you can use any of HttpServletRequest or HttpSession or ServletContext objects

Servlet and HttpSession

Http is a stateless protocol. To get session we can use javax.servlet.http.HttpSession
The HttpSession interface is used with cookie and URL rewriting technology.

header send in response --> Set-Cookie:JSESSIONID=0AABB2345ED2
header send in request --> Cookie:JSESSIONID=0AABB2345ED2
  •    HttpSession session = request.getSesion();
  •    session.isNew()
  •    request.getSession(false) --> only already created session
  •    session.getId();
  •    session.getCreationTime();
  •    session.getLastAccessedTime();
  •    String userName = (String)session.getAttribute("userName");
  •    session.setAttribute("userName",request.getParameter("userName")); --> Cannot store primitive data types
  •    session.setMaxInactiveInterval(int interval);
  •    int interval = session.getMaxInactiveInterval();
  •    session.invalidate();
<session-config>
    <session-timeout>-1</session-timeout> --> will never expire
</session-config>

response.encodeURL("/BeerTest.do"); --> URL rewriting -->possible only with dynamically generated urls
response.encodeRedirectURL("/BeerTest.do");

Cookie cookie = new Cookie(userName,password);
cookie.setMaxAge(10*60);
response.addCookie(cookie);

Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
     String cookieName = cookie.getName();
     String cookieValue = cookie.getValue();
}

Servlet and Thread safety

Web components share information with other web components using objects that are maintained as attributes of four types of scope object
  • web context --> ServletContext
  • session --> HttpSession
  • request --> ServletRequest
  • page --> PageContext

1. You can ensure that each request works independently and doesn't access the servlet instance concurrently by implementing SingleThreadModel

      public class myServlet extends HttpServlet implements SingleThreadModel
      <%@ page isThreadSafe="true" %> --> The default value of the isThreadSafe attribute is "true".
                                                               --> Will not implement SingleThreadModel.

2. You can allow concurrent access while protecting the shared data in the same way as you do in other multithreaded Java applications. For high-traffic servlets, you should use explicit synchronized blocks rather than implementing the SingleThreadModel interface like
      synchronized(getServletContext()){ ... } or
      synchronized(session){ ... }

Only local variables and request parameters are thread safe.


Servlet and Filter

Filters are commonly used for :
  • authentication
  • localization
  • encryption
  • data compression
  • caching
  • logging --> given below
javax.servlet.Filter ---> init(), doFilter(), destroy()
javax.servlet.FilterChain

      public void init(FilterConfig config)
      public void doFilter(ServletRequest request, --> Please note that this is not HttpServletRequest
                                    ServletResponse response,
                                    FilterChain chain) throws IOException,ServletException

<filter>
     <filter-name>myFilter</filter-name>
     <filter-class>jspAndServlet.filter.CompressionFilter</filter-class>
      <init-param>
          <param-name>enableCompression</param-name>
          <param-value>true</param-value>
       </init-param>
</filter>

<filter-mapping>
     <filter-name>myFilter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>

or

<filter-mapping>
      <filter-name>myFilter</filter-name>
      <servlet-name>SampleServlet</servlet-name>
</filter-mapping>

or

<filter-mapping>
       <filter-name>myFilter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>REQUEST</dispatcher> or INCLUDE or FORWARD or ERROR
</filter-mapping>
                     --> from 2.4 version onwards   --> REQUEST is the default value --> All/any of thesre can be included

Container's rule for ordering the filters:
        1. All filters with "<url-pattern>" is taken in the order in which they are declared in DD.
        2. All filters with "<servlet-name>" is taken in the order in which they are declared in DD.

FilterChain.doFilter() --> Will invoke the next filter.
String filterName = filterConfig.getFilterName();

Logging using the filter:

     ServletContext context = filterConfig.getServletContext();
     context.log(new Date() + ": " + "Access to: " + req.getRequestURL()
                       + " requested by: " + req.getRemoteHost()
                       + ". (Reported by " + filterConfig.getFilterName() + ".)");

Compression using the filter:
   
     It can be used after the FilterChain.doFilter() call
Eg:
        public void doFilter(request,response,chain) {
                     //request handling
                     chain.doFilter()
                     //response handling --> compression.
        }

     Issue --> After the call "chain.doFilter()", response will already be flushed to client from servlet.
     Solution --> Create a new instance of "response" and copy all values to it and pass it.
             Issue --> The "response" has n values to copy --> need to code all ?
             Solution --> use Wrappers

                              ServletRequestWrapper               ServletResponseWrapper
                              HttpServletRequestWrapper        HttpServletResponseWrappers

Servlet and Listener

Use of Listener:   If you wanted to keep a count of the total number of users on a system, you could update a global userNumber variable upon receiving each session creation event(sessionCreated). Similarly contextInitialized can be used to create a connection common to all the servlets.

        <listener>
               <listener-class>listeners.ContextListener</listener-class>
        </listener>

  Changes that trigger events -->
          1. Initialization and destruction of servletContext.
          2. Initialization and destruction of ServletRequest.
          3. Initialization and destruction of session objects.
          4. Addition/Alteration/Removal of attributes associated with servletContext, ServletRequest or session objects.

java.util.EventListener                                         ---> java.util.EventObject

javax.servlet.ServletContextListener                    ---> javax.servlet.ServletContextEvent
contextInitialized getServletContext
contextDestroyed

javax.servlet.ServletContextAttributeListener      ---> javax.servlet.ServletContextAttributeEvent
attributeAdded                                                           getName
attributeRemoved                                                       getValue --> OLD value
attributeReplaced                                                       getServletContext

javax.servlet.ServletRequestListener                   ---> javax.servlet.ServletRequestEvent
requestInitialized                                                        getServletContext
requestDestroyed

javax.servlet.ServletRequestAttributeListener      ---> javax.servlet.ServletRequestAttributeEvent
attributeAdded                                                           getName
attributeRemoved                                                       getValue
attributeReplaced                                                       getServletContext

javax.servlet.HttpSessionListener                        ---> javax.servlet.HttpSessionEvent
sessionCreated                                                           getSession
sessionDestroyed

javax.servlet.HttpSessionAttributeListener          ---> javax.servlet.HttpSessionBindingEvent
attributeAdded                                                          getName
attributeRemoved                                                      getValue --> OLD value
attributeReplaced                                                      getSession

javax.servlet.HttpSessionBindingListener            ---> javax.servlet.HttpSessionBindingEvent
valueBound                                                               getName
valueUnbound                                                           getValue
(no need to configure this in DD)                                getSession

javax.servlet.HttpSessionActivationListener       --->  javax.servlet.HttpSessionEvent
sessionDidActivate                                                    getSession
sessionWillPassivate

HttpSessionBindingEvent extends HttpSessionEvent


Simpe Tips in servlet :
  1. WEB-INF --> The folder name should be in capital letter to work with Tomcat5.
  2. <form action="/urlOne" > --> If you have a folder like "Tomcat 5.5\webapps\test\urlOne", this will not work as this is an absolute path, this will search for "webapps/urlOne" and you have "webapps/test/urlOne". So need to use <form action="urlOne"> to get "webapps/test/urlOne".
  3. You can overrride the init() method and do the one time operations like create a DB connection.
  4. War files make it easy to transfer the web applications from one server to another.


Comments

Popular posts from this blog

ATG Search - search engine tuning settings

In this blog, I am going to list the best tuning settings for ATG Search engine. The AESoapConfig.xml, AESoapWaspConfig.xml  and AEConfig.xml are the xmls referred below and you can find it @  <ATG_DIR>\<Searchx.x>\SearchEngine\<operating_system>\bin\ folder. (1)  Make sure that the AESoapConfig.xml's rwTimeout is less than or equal to routing's readTimeoutMs. You could find the routing's readTimeoutMs @ atg\search\routing\SearchEngineService component.               rwTimeout is the  length of time in seconds to wait before a read or write operation times out on an active connection. The number can be decreased to improve performance. However, a value that is too low could cause slow connections to be prematurely closed. (2)  Adjust the number of engine threads to match the number of CPUs available to the engine. Note that the minimal value for maxThreads and maxSpar...

Google Chrome shortcut keys

If you are a Google Chromey guy, please find below the list of shortcut keys for some of the most used features  :-) Find more shortcut keys @  http://www.google.com/support/chrome/bin/static.py?page=guide.cs&guide=25799&topic=28650

ATG Search Indexing - overview of different steps in search indexing

Read more about the search indexing behind the scene steps @  http://tips4ufromsony.blogspot.in/2011/12/atg-search-indexing-behind-scene-steps.html ATG Search prepares searchable content by indexing the products specified in the XML definition file (/atg/commerce/search/ProductCatalogOutputConfig). Generally there are two types of indexing 1.  Full Indexing  --> all data taken for indexing 2.  Incremental Indexing --> only changed data will be taken for indexing When full indexing is triggered, following happens:    1. The out of box component BulkLoader will call IndexedItemsGroup.getGroupMembers() to load the products to the XHTL document. It prevents uncategorized products from getting indexed. The definition file format begins with a top-level item as a product and includes the properties of parent category and childskus. For each product, the set of Variant Producers configured in ProductCatalogOutputConfig is execute...

Good features of Eclipse 3.6 (Eclipse Helios) JDT

Read the Eclipse Galileo features @  http://tips4ufromsony.blogspot.com/2011/10/good-features-of-eclipse35-eclipse.html New options in Open Resource dialog : The Open Resource dialog supports three new features: • Path patterns: If the pattern contains a /, the part before the last / is used to match a path in the workspace: • Relative paths: For example, "./T" matches all files starting with T in the folder of the active editor or selection: • Closer items on top: If the pattern matches many files with the same name, the files that are closer to the currently edited or selected resource are shown on top of the matching items list. MarketPlace :  Searching and adding new plugins for Eclipse have always been a challenge. The Eclipse Marketplace makes this much easier – it allows you to not only search a central location of all Eclipse plugins, but also allows you to find the most recent and the most popular plugins. Fix multiple proble...

GC Log Analyzer from IBM

This blog is about the GC( garbage collection) log analyzer from IBM. If you have a GC log and you want to analyze the file,  this IBM tool will help you with some graphical analyzer and some recommendations. You can download it from the following URL : https://www.ibm.com/support/pages/ibm-pattern-modeling-and-analysis-tool-java-garbage-collector-pmat Please find below some screenshots and details that might help you. 1. Screenshot 1: 2. Screenshot 2 : 3. Screenshot 3 : 4. Screenshot 4: