• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading parameters in Servlet passed from Client

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ashwin Tadepalli
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ashwin Tadepalli
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic