jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes want to pass a string to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "want to pass a string to a servlet" Watch "want to pass a string to a servlet" New topic
Author

want to pass a string to a servlet

HARSHAD bhawsar
Ranch Hand

Joined: Feb 22, 2001
Posts: 48
Frds
I am using following code to pass a string to servlet,
<a href=**\myservlet?param=xyz>xyz</a>
It's working;
But what if i want to pass an string like "hello xyz how r u?"
It is passing only "hello", and not rest string , is there athor solution to pass it
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
Did yout try URL encoding
for example in your case your parameter value will be "hello%20xyz%20how%20r%20u?" where %20 is for space
OR
<a href=**\myservlet?param=URLEncoder.encode(hello xyz how r u)</a>
if you are using jsp. Remember to decode it while retreiving the value
I'm not sure if this is what you are looking for but worth giving a chance
AJan

[This message has been edited by Ajan Balakrishnan (edited March 10, 2001).]
Daniel Olson
Ranch Hand

Joined: Jan 05, 2001
Posts: 81
I think Ajan is right. I just wanted to add that in my experience it depended on what browser I was using. With Netscape I had to URLEncode, but not with IE. IE doesn't have trouble if you do, but it doesn't seem to care whether you do or not.
HARSHAD bhawsar
Ranch Hand

Joined: Feb 22, 2001
Posts: 48
Thanks Ajan,Danial,
I am not using Jsp.
Do you think this will work on plane servlet and tell me how to collect string in another servlet using url rewriting
Harshad
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Harshad,
In the calling Servlet just wrap the address with URLEncoder.encode(...). In the called servlet you need not decode anything. Just request.getParameter(..) will do.
I tested your need with the following code. Please check.


[This message has been edited by maha anna (edited March 13, 2001).]
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Maha,
When TestServlet receives the dispatcher from MyServlet,
which method takes care of the action, doGet(*,*) or
DoPost(*,*)? I know that in your program, you call doGet()
from doPost(), so it doesn't really matter there. But,
which method is called excatly? Can I specified GET
or POST from MyServlet to direct the method call in
TestServlet?
Thanks!
Sam
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sam,
It looks like POST method is used for the dispatcher when forwarded to TestServlet. Initially I wrote only doGet(...) in TestServlet and did not get "msg" printed out at DOS command prompt. So, in order to make sure which method (GET/POST) is used, I wrote doPost(...) to call doGet(...) in TestServlet and immediately got the "msg" printed out at command prompt.
For the testing I used http://localhost/MISApp/servlet/MyServlet at the browser. Instead of "MISApp" you can use your webApplnContextName.
regds
maha anna

[This message has been edited by maha anna (edited March 15, 2001).]
Tom Caldwell
Ranch Hand

Joined: Oct 09, 2000
Posts: 39
Has anyone else had this problem? This is my 1st try at these but when I try to run this I get the message "No target servlet configured for uri: /app/TestServlet?msg=Hello "
This is the correct address as I can run both of these separately
by myURL/app/MyServlet or myURL/app/TestServlet. I've tried every combination of address I can think of is there something else?
I'm running websphere.
Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: want to pass a string to a servlet
 
Similar Threads
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException: 8
HTML input to JSP output
passing string to a servlet
Problem with link label
Velocity template - populating with form values