简体   繁体   中英

how to get response from one servlet to another?

I have one Servlet A in which I put my result (eg 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM