Solved

How to send a string from one sevlet to another servlet?

Posted on 2002-06-17
12
240 Views
Last Modified: 2012-05-04
Say if I have a string in a servlet called sendServlet.java and I want to pass it another servlet called receiveServlet.java, How would I do that?
Thanks.
0
Comment
Question by:tomboman
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 7084931
If the two servlets are in the same JVM then you can simply call the necessary class directly.
0
 

Author Comment

by:tomboman
ID: 7084971
Hello objects, could you be more specific? I'm kind of new in writing servlets. thanks.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7086566
What does recieveServlet need to do when the string is passed to it?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:tomboman
ID: 7088501
just need to display it on the web page at the mean time. But now the question just got more complicated. There are now a series of string, which I now kept them in a vector(of the send sendServlet). I need to display them on the web page after the receiveServlet received the series of strings from sendServlet. Help.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7089086
there are various approaches for this.

1.  Store the vector in Session and retrieve it from session in the second servlet
2.  Attach the vector to the request object and forward the request from the first servlet to the second servlet.
3.  Store the string array in a cookie.

if one servlet isn't forwarding to the other then use approach one otherwise approach two.  Approach three is a last resort and not recommended.

CJ
0
 
LVL 3

Expert Comment

by:mjzalewski
ID: 7090269
I am assuming here that SendServlet writes out the page that will be serviced with ReceiveServlet.

If you are doing a POST from the first servlet to the second, the easiest thing to do might be to include the following in SendServlet

   out.write( "<input type=\"hidden\" name=\"mystring\" value=\" + myValue + "\">");

Then in ReceiveServlet, you can pick up the value of th string with

  String myString = request.getParameterValue( "mystring");

To do a series of strings, simply have one more hidden field for each string.

   out.write( "<input type=\"hidden\" name=\"mystring1\" value=\" + myValue1 + "\">");
   out.write( "<input type=\"hidden\" name=\"mystring2\" value=\" + myValue2 + "\">");

and retrieve with

  String myString1 = request.getParameterValue( "mystring1");
  String myString2 = request.getParameterValue( "mystring2");

------

If you are doing a GET, you will have to encode the string and write a properly coded href, something like this:

  out.write( "<a href=\"http://myhost/myWebApp/servlet/ReceiveServlet"
    + "?mystring1=" + URLEncoder.encode( myValue1)
    + "&mystring2=" + URLEncoder.encode( myValue2)
    + "\">Receive</a>");

The URLEncoder (java.net.URLEncoder) is needed to encode a String into a URL parameter. If you know for sure that the string will be only letters and numbers, you can just use

    ... ?mystring=" + myValue + "\"> ...

------
Comments about the other solutions:

objects proposes calling a public method in ReceiveServlet directly. This will work if the receiving method can be made static. It will not work well if there will be a different string passed on every request.

cheekycj proposes to store the string as a vector in the session. That will only work if you actually create a session first. And you can store the String directly, you don't need to create a vector.
0
 
LVL 92

Expert Comment

by:objects
ID: 7091211
Need to clarify exactly what you want to do:
So when sendServlet get's called you want to store a String which can be displayed by recieveServlet the *next* time it is called?
If this is the case then store it in a session as described by CJ.

0
 
LVL 6

Expert Comment

by:kotan
ID: 7091836
I give you CJ's method 2 example:

sendServlet.java
------------------


Vector v = ...
request.setAttribute("strings", v);
RequestDispatcher rd = request.getRequestDispatcher("receiveServlet");
rd.forward(request, response);


receiveServlet.java
--------------------
Vector v = (Vector) request.getAttribute("strings");
v.get() ....
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7092686
since kotan gave sample code for the second approach.. just in case you are not forwarding to a second servlet here is sample code for the first approach.

firstServlet.java

Vector strVector = new Vector();
// add to Strings to vector here
HttpSession session = request.getSession(true);
session.setAttribute("StringVector", strVector);


Now to retrieve it in the second servlet:
secondServlet.java
// we pass false here b/c we assume session has been
// created by first servlet already
HttpSession session = request.getSession(false);
Vector vectorFromSession = null;
if (session != null) {
  vectorFromSession = request.getAttribute("StringVector");
  if (vectorFromSession != null) {
     // add code here to do whatever you want with the
     // vector of strings

   }
   else {
     // vector was not in session for some reason handle appropriately
   }
}
else {
  // print out a warning in your error logs that session is not working right
}

also add this to your web.xml file:
  <session-config>
    <!-- setting session timeout to 30 minutes -->
    <session-timeout>30</session-timeout>
    <!-- setting session id cookie domain -->
    <cookie-domain>.yoursite.com</cookie-domain>
  </session-config>

some feedback would help us.

HTH,
CJ
0
 
LVL 35

Expert Comment

by:girionis
ID: 9919431
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between objects and cheekycj

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
 

Author Comment

by:tomboman
ID: 9925400
Thanks object and cj. and everyone else who contributed.
0
 
LVL 92

Expert Comment

by:objects
ID: 9925411
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
gZip compression filter 2 148
oracle fusion vs MQ 2 114
maven project jars 7 53
struts spring hibernate example 12 122
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question