There are four methods to pass parameters between servlets,.
First method is using session object depending on the application environment, for example
HttpSession ses = req.getSession(true);
ses.putValue("key","value");
you can get back this data in the second servlet by :
HttpSession ses = req.getSession(true);
String val1 = (String)ses.getValue("key");
Second method is using the,
request.getParameter ("key"); and
request.setParameter("key","value");
methods. These methods are used to retrieve parameters from forms or through querystring.
Third method is using the,
request.getAttribute("key"); and
request.setAttribute("key","value");
methods. These methods are used with RequestDispatcher class's object.
Ex. <% @ page import="java.util.*" %>
<%
request.setAttribute("username","mahesh");
RequestDispatcher dispatcher = request.getDispatcher("abc.jsp");
if(dispatcher != null) {
dispatcher.forward(request,response);
}
%>
abc.jsp
<% out.println(request.getAttribute("username"));
%>
Fourth method is using Java Beans,
getXXX() and
setXXX() methods.
No comments:
Post a Comment