简体   繁体   中英

How to pass value from one servlet to another servlet?

I've two Servlets namely S1 and S2 . S1 contains a variable x of String type, S2 contains a variable y of String type.I have a method m(x,y) implemented in class C .How can i pass x or y to Servlet ( S2 or S1 ) using method m(x,y) ?

您可以在请求中设置属性

request.setAttribute("attr",val); RequestDispatcher rd = request.getRequestDispatcher("servlet_path"); rd.forward(request,response);

Example from here :

    URL yahoo = new URL("http://localhost:portnumber/context/urlpattern/s?x="+x+"&y="+y);
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();

From your perspective, servlet is just an URL on some server. As for not waiting for a response - read about Java threads. Use the above code in the method m(x,y)

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