Mahol Dot Org

Icon

Weblog For All Your Need

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 :2025

Related posts:

  1. A Hello,World Servlet Servlet Tutorial Servlet Examples Run servlet on tomcat
  2. Context Params How to get context parameters in servlets Retrieving context params in servlets
  3. Servlet Init Params How to get init parametrs in servlets Retreiving init params in servlets
  4. How To Redirect From One Servlet To Another Servlet Basics Servlet Redirect
  5. how to forward from one servlet to another Servlet Essentials Servlet Forward
Receive Updates By Email

Enter your email address:

Delivered by FeedBurner

Category: Java

Tagged:

2 Responses

  1. Arun says:
    Thanks.. Gud Example

Leave a Reply

Lost !! Use Advanced Search

Categories

Content Protected Using Blog Protector By: PcDrome.

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