how to get response from one servlet to another? - java

I have one Servlet A in which I put my result (e.g. URL ) in JSONObject (org.codehaus.jettison.json.JSONObject).I am trying to get url from Servlet A to Servlet B with passing some parameters (like ID).How to do this?
Is it possible?
Thanks.

You can use request dispatcher and in request object you can set the attribute using request.setAttribute() in Servlet A and in Servlet B you can access it using request.getAttribute()
RequestDispatcher dispatcher = request.getRequestDispatcher(URL_PATTERN_OF_ANOTHERSERVLET);
dispatcher.forward(request,response);

Try using sessions.
Servlet A:
HttpSession session = request.getSession();
session.setAttribute("id", yourValue);
Servlet B:
String str = (String)session.getAttribute("id");

You can get the url by the following code:
request.getAttribute("javax.servlet.forward.request_uri")
And then you can navigate to another servlet using
response.sendRedirect("/ServletB")
EDIT
In Servlet B:
request.setAttribute("attributeName",StringParameter);
RequestDispatcher rd = request.getRequestDispatcher("/ServletA");
rd.forward(request,response);
In Servlet A:
String r = (String)request.getAttribute("attributeName");

Related

Refreshing session of other servlet

I have a problem with handling sessions between Java servlet, jsp page and Struts Action. Java servlet adds some param to session when get request and sendRedirect to some page1.jsp. On page1 I have an url to Struts Action like strutsAction.do. When StrutsAction recevies request, the session doesn't contain attributes I added in Java servlet. Clicking on page returned by this action doesnt refresh session of Java Servlet, but they have the same SessionId. So, after session.getMaxInactiveInterval() of servlet session pass I'm getting sessionDestroyed() event, even when i taking actions on page returned by StrutsAction. How to fix this issue?
In case of SendRedirect call old request and response object is lost because it’s treated as new request,
You should try the following code
RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.forward(request, response);
becuase when forward is called on requestdispather object we pass request and response object so our old request object is present on new resource which is going to process our request.

Communication between servlets and/or jsp

How can you sent a request from one servlet to another or one servlet to any jsp file?
Actually i want to send a request form one servlet named Demo to another jsp file abc.jsp
Using RequestDispatcher
programatically...
public class Demo extends HttpServlet{
public void doGet(HttpServletRequest req , HttpServletRespaonse res)
throws ServletException, IOException {
res.setContentType(text/html);
PrintWritter pr = res.getWriter();
pr.println("i am in servlet");
RequestDispatcher rd = req.getRequestDispatcher("abc.jsp");
rd.forward();
}
}
abc.jsp
<body>
<i am abc in abc.jsp>
</body>
You can either forward it or redirect it.
To forward, you can use RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("abc.jsp");
rd.forward(request, response);
To redirect,
response.sendRedirect("abc.jsp");
FYI, Difference between the two,
In Forwarding, the same request object is forwarded to the next resource (Servlet or JSP) and in Redirecting client (browser) is asked to send a new request to the server for the next resource (servlet or JSP).

Forward parameters to jsp from servlet

Is there a way to send parameters from servlet to jsp without redirecting the browser to that page?
RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp");
disp.forward(request, response);
There can be one way as below:
RequestDispatcher disp = request.getRequestDispatcher("shoppingCart.jsp"+"?myParam=myValue");
disp.forward(request, response);
If you are fine with "GET" method then you can solve this problem with appended parameters.
Well you can either set the attributes(used in case of internal communication with servlets or servlet to jsp or vice-versa) to the response object and forward the request you can achieve this as :
request.setAttribute("someKey","someValue");
You can also use the session scope to share the attributes between servlet and jsp like this:
Http session = request.getSession();
session.setAttribute("someKey","someValue");

call servlet from controller with parameters to servlet

I am writing a jsp file to call a controller with some parameters. And from this controller i want to call a servlet by passing the values from the controller. And with in the servlet i should get access to the parameters. Is it possible to forward values from jsp to servlet via controller?
Yes it's possible in Spring controller,
Try this in Spring controller:
public void requestedURL(HttpServletRequest req, HttpServletResponse res){
String jspParameter = request.getParameter("param_name");
req.getRequestDispatcher("your servlet url pattern?param1="+jspParameter)
.forward(req, res);
}

Setting parameters in jsp and in servlets

In my web application , I get a request from the client. I process the request in my jsp page or the servlet(which ever is called) and want to forward to other jsp's or servlets accordingly. But before forwarding I want to set a few parameters and then forward it with these new parameters. The old parameters should not be present when forwarding. Only the new parameters should be present. How can this be done?
Can I forward from a servlet to jsp and vice-versa?
Please tell how the above can be accomplished?
If you have no use for the request parameters and your jsp/servlet has not written anything to the response, then I suppose it would be fine to use redirect instead of forward, since redirecting will discard the request along with the parameters.
When you do redirect, you can create dynamically and set the querystring like so:
response.sendRedirect("url?a=" + var1 +"&b=" + var2);
Take note that this will be a GET method to the url. If url will be resolved to a servlet, you can implement the doGet method to just call the doPost.
Please note that a redirect will be ignored if the jsp/servlet has written something already on the response...
You can use request dispatcher and redirect as per your need and requirement.
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("url");
rd.forward(request,response);
or
response.sendRedirect("url");
sendRedirect() sends the header back to the browser , which contains the name of the resource to be redirected to. So this will be a new request to the resource from the browser .
forward() action takes place within the server without the knowledge of the browser .
yes you can forward the parameter servlet to jsp and jsp to servlet.
when you can set the attribute in request then it will lost on destination.means you can not access that on third resource.
request.setAttribute(attribute name,attribute value)
you can do same thing in session also.
You have to forward to JSP/Servlet using RequestDisptcher. Set the request attribute on the request to set parameters using
request.setAttribute(name, value)
The Forwarded JSP can read the parameter using
request.getAttribute(name)
But, You cannot hide the attribute existing before forward by default. You can achieve this using Request Wrapper. Wrap the request before forwarding override the set and get attribute methods.
Below code explains
RequestDisptcher dispatcher = req.getRequestDispatcher("url");
HideExistingRequestWrapper requestWrapper =
new HideExistingRequestWrapper(request);
requestWrapper.setAtribute("forwarded", "forwarded value");
dispatcher.forward(requestWrapper, response);
Here is the code of wrapper implementation:
class HideExistingRequestWrapper extends HttpServletRequestWrapper {
private Map localattributes = new HashMap();
public HideExistingRequestWrapper (HttpServletRequest orignialRequest) {
super(orignialRequest);
}
public Object getAttribute(java.lang.String name) {
return localattributes.get(name);
}
public Object setAttribute(java.lang.String name, java.lang.String value) {
return localattributes.put(name, value);
}
}
use
ServletRequest.removeAttribute(name of your old attribute)
ServletRequest.setAttribute(name , value)
After setting the attributes, get the RequestDispatcher using
getRequestDispatcher(url)
and then use forward() method

Categories

Resources