How To Get The Parameters From One Servlet To Another Using Request

Access Parameters From One Servlet To Another

Parameters can only be set from the web.xml file or can be input from a user input through a view. It cannot be set from the servlet code. Once we have accessed the required parameter/parameters in one servlet,we can set a particular attribute and set that very attribute with the accessed value of parameter and it can be accessed through out various pages depending upon the type and nature and scope of the attributes .Here it must be noted that attributes and only attributes can be set with a value from code and not the parameters. So if we want to access a parameter from one one servlet to another,we must first access it in one servlet ,set an attribute with its value and then we can access it in other servlet.If the scope of attribute is request it will exist till request exists,if session then till session and if servletcontext then once set it can be accessed through the application. But the parameters can be read only once when taken from user input. The following code demonstrates this :

<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>myjobs.FirstServlet</servlet-class>

<init-param>

<param-name>username</param-name>

<param-value>john</param-value>

</init-param>

</servlet>

<servlet>

<servlet-name>SecondServlet</servlet-name>

<servlet-class>myjobs.SecondServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>NewServlet</servlet-name>

<url-pattern>/NewServlet</url-pattern>

</servlet-mapping>

FirstServlet :

public class FirstServlet extends HttpServlet {

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

try {

String uname=getServletConfig().getInitParameter(“username”);

request.setAttribute(“usrname”,uname);

request.getRequestDispatcher(“SecondServlet”).forward(request, response);

} finally {

out.close();

}

}

}

Second Servlet :

public class SecondServlet extends HttpServlet {

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

try {

/* TODO output your page here*/

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>Servlet SecondServlet</title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“<h1>Parameter from first servlet is ” + request.getAttribute(“usrname”) + “</h1>”);

out.println(“</body>”);

out.println(“</html>”);

} finally {

out.close();

}

}

}

Here we are getting the paramter in FirstServlet setting an attribute and accessing in another servlet called SecondServlet.

Number of View :5020

Tags:

  1. #1 written by Arun December 1st, 2010 at 05:10

    Thanks.. Gud Example

    RE Q
    • #2 written by admin December 13th, 2010 at 04:20

      thanks for the feedback.

      RE Q
      • #3 written by Yenny September 4th, 2014 at 20:55

        I simply want to say I am nebwie to blogging and site-building and definitely loved this website. Very likely I’m going to bookmark your site . You really come with really good writings. Thank you for sharing your website.

        RE Q

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache