Giosafat Loviglio
Giosafat Loviglio

Reputation: 1

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)?

Upvotes: 0

Views: 4618

Answers (2)

NIKET BHANDARY
NIKET BHANDARY

Reputation: 304

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)

Upvotes: 0

Rahul Yadav
Rahul Yadav

Reputation: 1513

You can set the attributes in request

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

Upvotes: 2

Related Questions