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)
?
Asked
Active
Viewed 4,298 times
0

Mihir
- 572
- 1
- 6
- 24

Giosafat Loviglio
- 1
- 1
- 2
-
`request.setAttribute` – Suresh Atta Aug 13 '15 at 11:42
-
Stop thinking in "passing". Start thinking in "scopes" (request scope, session scope and application scope). Simply put data in the desired scope and elswehere check/grab data there. Food for read: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading – BalusC Aug 13 '15 at 12:33
2 Answers
2
You can set the attributes in request
request.setAttribute("attr",val);
RequestDispatcher rd = request.getRequestDispatcher("servlet_path");
rd.forward(request,response);

Rahul Yadav
- 1,503
- 8
- 11
-
-
Suppose if we have servlet S1 & S2 and we want to pass value from S1 to S2 then in S1 we would write the above code and in S2 we would fetch the value using 'request.getAttribute("attr")' – Rahul Yadav Aug 13 '15 at 11:45
-
Thanks first, but I have one thing clear. In servlets s1 I have the variable "finals" I want to go to the servlet s2, then I do: request.setAttribute ("attr", finals); RequestDispatcher rd = request.getRequestDispatcher ("C: / Users / thesis / workspace / W7TurismoServer / src / packserver / S2"); rd.forward (request, response); S2 as I can recall "finals"? Thanks again. – Giosafat Loviglio Aug 13 '15 at 12:30
-
In RequestDispatcher you have to provide the path of the servlet using the context root of your web application and not the complete file path. And also you can name attr attribute to the one that best suits to you. – Rahul Yadav Aug 13 '15 at 12:49
0
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)

NIKET BHANDARY
- 195
- 13