Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do 2 servlets communicate?

 
Jess Ishasder
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic