• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get parameter from one servlet to another servlet

 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have two web application in java
webapp1(jservlet1)---webapp2(jservlet2) i have to send some data from servlet1 to servlet2 how should get data from servlet1 to servlet2
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data gets to a servlet via an HTTP request. You can use the java.net.* classes for that, or use the Apache HttpClient library.
 
mohammed R farook
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

use any collection classes like hashtable or set the parameters in request object or use HTTP session .
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting data from jsp1 page to servlet1 with getparameter but how this data i should get in servlet2
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session is not working
 
mohammed R farook
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply forward that request , then you can access the parameters in servlet2
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is servlet1

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<h2>Servlet ClientServlet at " + request.getContextPath () + "</h2>");
;
java.lang.String uname = request.getParameter("uname");
java.lang.String pass = request.getParameter("pass");
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", "http://localhost:8080/ServiceClient/Servlet2");


} finally {
out.close();
}
}

i want to know how to get data (uname, pass)in servlet2
 
mohammed R farook
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<h2>Servlet ClientServlet at " + request.getContextPath () + "</h2>");
;
java.lang.String uname = request.getParameter("uname");
java.lang.String pass = request.getParameter("pass");
request .getRequestDispatcher(" your servlet2 Name ").forward(request,response);
} finally {
out.close();
}
}

chage your servlet1 code as mentioned above
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sessions won't work, and neither will a forward because they're two different web apps.

A redirect would work if that falls under "send some data from servlet1 to servlet2".
 
mohammed R farook
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply use URLConnection class and call servlet and form a query string with the required data and post the data to the url
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not working servlet2 is in web app2,
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it will work if you do it right. Have you already written the code that makes an HTTP connection from one servlet to the other? What exactyl is it that you're trying to do? Are you certain that a redirect won't solve the issue?
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
connect web app1 to web app2
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what that means; it's not standard terminology. Can you describe in entire sentences what the problem is that you're trying to solve?
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In web1 user send the data with html in get this data in servlet1 n i want to get this data from servlet1 to my web2 throug servlet2
should it clear??
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort of. Making an HTTP call -as has been suggested before- is the way to go in this case.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic