aspose file tools*
The moose likes Servlets and the fly likes Reading parameters in Servlet passed from Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading parameters in Servlet passed from Client" Watch "Reading parameters in Servlet passed from Client" New topic
Author

Reading parameters in Servlet passed from Client

Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi;
I have a question regarding passage of parameters from the command prompt to my servlet which reads in HTTP parameters.I am reading in the parameters from a client program and then calling the servlet(from the command prompt using URL class).The servlet inturn takes in the paramaters,does the database work and gives out the result in the console.The servlet works pretty well if I hard code the servlet parameters in the servlet itself,but
my question here is:-
How to read the parameters passed from the client program in the servlet code?Is there any way I got retrieve those parameters.
Details:-
My client(client program):-Command prompt(MS DOS)
My server(servlet):-Visual Age 3.5(Web Sphere Test Environment)
Thanking You
Ashwin
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Assuming that I understand your question correctly (which is doubtful since I haven't used servlets used via command line but...)
CMD> /servlet/MyServlet?param1=A&param2=B&param3=C


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi Tony,
This is how I invoke my servlet.
URL url = new URL("http://localhost:8080/servlet/MyServlet");
Not directly on the command prompt nut by using a URL class.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think a combination of the approaches you both mention should do the trick:

You may need to URL-encode the parameters you pass in, if they are likely to contain non alphanumeric characters.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi Tony,
Thanks for letting me know.I am also working on similar lines,it is just that I did not properly encode the URL i.e with the input paramters as you have told me.I will try that and let you know of the result.
Thanking You
Ashwin
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi Tony,
You were right,it has worked for me.
You have to invoke it this way as you have said:-
/servlet/MyServlet?param1=A¶m2=B¶m3=C
Thank you very much Tony.
Ashwin
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi,
Okay I have been now successfully been able to read the parameters given from a client program invoked on the command prompt in a servlet and give out the result in the console.I can do that by using a URL class and invoking the servlet in this way from the client program:-
URL url = new URL("http://localhost:8080/servlet/MyServlet?param1=A¶m2=B¶m3=C");
In servlet code I use(to read those parameters):-
String param1 = new String();
param1 = req.getParameter("param1");
..
***Now the Problem is :-***
I am able to run the program successfly the first time but if I run the program a second time or change the parameters in the client(recompile & then run),I don't get the result.Only if I stop & then re-start my servlet engine(which is on the server side),I am able to run this correctly.
Can anyone please let me know what is the problem?
Thanking you,
Ashwin
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Ashwin,
I used Frank's sample code and able to get the different name and age params given from command prompt everytime I run the java appln from command prompt without restarting the Tomcat server.
I use Tomcat 3.1 on windows. What do you use? I also post the code which I used. Please check. Don'e bother about the extra unnecessary import packages I used. I just cut and pasted from another servlet file and lazy to edit the unwanted imports.

But I added the following line to Frank's Java appln to invoke the servlet.


regds
maha anna
[This message has been edited by maha anna (edited February 09, 2001).]
Ashwin Tadepalli
Greenhorn

Joined: Feb 01, 2001
Posts: 27
Hi Anna,
Thanks for your feedback.Yep it has worked perfectly this time.
You were right about the passsing of the URL string in that fashion.Also why it would run the first time and not the second time was that there was a close statement(for my JDBC code) at the end of the processing in the servlet.I had removed that statement beacause there was already a close statement in the destroy method of my servlet which should be the necessary case.
Anyhow thanks to all of you.
Ashwin
 
Consider Paul's rocket mass heater.
 
subject: Reading parameters in Servlet passed from Client