Thursday, June 24, 2010

javax.servlet.forward AND javax.servlet.include

from servlet spec

requestURI = contextPath + servletPath + pathInfo

Include

When you include a servlet the following attributes will be set by the calling servlet
the attributes will be derived from the string you passed to include the servlet

javax.servlet.include.request_uri
javax.servlet.include.context_path
javax.servlet.include.servlet_path
javax.servlet.include.path_info
javax.servlet.include.query_string

ex:
first.jsp code
=========
rd = request.getRequestDispatcher("/myapp/second.jsp?pname=pvalue");
rd.include(request,response);
assume the context for your webapp is "myapp"

javax.servlet.include.request_uri : /myapp/second.jsp
javax.servlet.include.context_path : /myapp
javax.servlet.include.servlet_path : /second.jsp
javax.servlet.include.path_info : null
javax.servlet.include.query_string : pname=pvalue


Forward

when you forward to a servlet the following attributes will be set by calling servlet

javax.servlet.forward.request_uri = request.getRequestURI() value in the calling servlet
javax.servlet.forward.context_path = request.getContextpath() value in the calling servlet
javax.servlet.forward.servlet_path = request.getServletpath() value in the calling servlet
javax.servlet.forward.path_info = request.getPathInfo() value in the calling servlet
javax.servlet.forward.query_string = request.getQueryString() value in the calling servlet

ex:
first.jsp code
=========
rd = request.getRequestDispatcher("/myapp/second.jsp?pname=pvalue");
rd.forward(request,response);
assume the context for your webapp is "myapp"

Access URL is: http://localhost:8080/myapp/first.jsp?first=first

javax.servlet.forward.request_uri : /myapp/first.jsp
javax.servlet.forward.context_path : /myapp
javax.servlet.forward.servlet_path : /first.jsp
javax.servlet.forward.path_info : null
javax.servlet.forward.query_string : first=first


Is path_info is null always?

No.
This will come into picture when your servlet is matched by /* pattern in web.xml

ex:
your appication context is myapp and have 1 entries in web.xml for  a servlet with following mapping

Servlet Mapping Pattern: /lawn/*

and the request from client is

http://localhost:8080/myapp/lawn/grretings/d.html

here path info : /greetings/d.html
Servlet path   : /lawn

3 comments:

  1. Why do I need these attributes when I can derive the same from the servlet api in the included resource. For example why should I look for the attribute javax.servlet.include.query_string when I can have it by calling request.getQueryString().

    The attributes are internal implementations and is not guaranteed by the spec. The api is.

    Regards,
    Ram.

    ReplyDelete
  2. 1) If I include and print the requestURI in both servlets it gives me the same values.But how do you know the name of the included page?

    If I forward and print the requestURI it will print the name of the forwared page. But how do you that from which pahe it has been forwared?

    In the both cases the attributes help.


    2) These attributes are part of servlet specification not internal implementaion details.

    ReplyDelete
  3. So what.
    Keep up the discussions anyway.

    ReplyDelete