File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How do 2 servlets communicate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do 2 servlets communicate?" Watch "How do 2 servlets communicate?" New topic
Author

How do 2 servlets communicate?

Jess Ishasder
Greenhorn

Joined: Feb 19, 2002
Posts: 21
Suppose I have 2 servlets, say, A and B
Servlet A displays a textfield and an image.If the user entered his name in the field and pressed the image, servlet B will be called. I would like to know how the value in the textfield can be passed to servlet B for performing another action.
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Simple. When servlet B is called, the HttpServletRequest object will contain any form parameters submitted from the browser via HTTP. On the servlet, you can access the name field value using the following code (assuming the name field on your HTML form was defined with the parameter name "username"...
String name = request.getParameter("username");
The "name" variable will contain the data that was entered by the user from the browser.
SAF
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
There are 2 ways I know of:
>> Get a direct reference to another servlet by using the "getServlet()" method. It's deprecated beyond 2.1 though and probably not recommended
>> The newer ( and much safer way | 2.1+ ) is to share the servlet context using "getServletContext()". You can then getAttribute/setAttribute/removeAttribute just like any other session value.
In your case, I think it might be better to just pass it by posting from one servlet to another as mentioned in the previous post.
[ March 05, 2002: Message edited by: Brian Glodde ]
Jess Ishasder
Greenhorn

Joined: Feb 19, 2002
Posts: 21
I can't get the value of parameter, it returns null!!!
The following are the codes of the 2 servlets:
============================
For servlet A:--Display a textfield and an image
.....
.....
out.println("<input type=\"text\" name=\"qty\" value=1 size=2>");
out.println("<a href = \"http://localhost:8080/slt/Add_Products?prdID=" + rs.getString(1) + "\">" + "<img src = \"http://localhost:8080/slt/myimages/add.gif\" border=0></a>");
......
.....
**********************
For Servlet B----Get the value of "qty"
=======================================
public void init(ServletConfig config) throws ServletException{
super.init(config);
ss=(String) config.getServletContext().getAttribute("qty");
System.out.println("******ss = " + ss);
........
........
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Ok, firstly I think (judging by the code in the post) that you are attempting to obtain a value that's on the form, but will not be sent by clicking on the button as you have it coded. That code will cause a "GET" and you will not see the qty value. The easiest way to solve this is to output form tags to cause a post to your servlet B:
<form name=\"frmWhatever\" method=\"POST\" action=\"http://localhost:8080/slt/Add_Products\">
Then I would put your prdID in a hidden field:
<input type=\"hidden\" name=\"prdID\" value=\"" + rs.getString(1) + "\">
Change your button to something like:
<input type=\"submit\">
or
<input type=\"image\">
Now you will be able to retrieve both qty and prdID via HttpRequest (as mentioned by Safrole):
String qty = ( String )request.getParameter( "qty" );
if( qty == null ) qty = "";
Then you should be able to test/manipulate qty with no exceptions, do the same for the other incoming values.
Hope that helps!
[ March 05, 2002: Message edited by: Brian Glodde ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do 2 servlets communicate?