File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes retrieving a parameter from a link Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "retrieving a parameter from a link" Watch "retrieving a parameter from a link" New topic

retrieving a parameter from a link

subba rai

Joined: Jan 03, 2005
Posts: 5
hai friends, i have a problem with retrieving a parameter from a link using HttpServletRequest object. Following is the code

String action = request.getParameter("action");
PrintWriter out = response.getWriter();
if(action != null && action.equals("invalidate"))
HttpSession session = request.getSession();
out.println("<a href=\"http://localhost:8080/servlets-examples/Session1 \""+"?action=invalidate>");

Assume that the previous out.println statement is in a single line.

But action is always assigned null value. Why request(HttpSrevletRequest object)is not able to read "action" parameter from the link i created in the servlet. Is there any thing wrong with the syntax i used in out.println statement?
thank u in advance
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

How do you know that it's always null?
Have you tried printing it or looking at the headers?

Try printing it right at the top of your service method:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Varun Dikshit

Joined: Feb 15, 2005
Posts: 18
Give a name to your hyper-link in the out.println statement for eg concatenate the string in the out.println stmnt with "Click here!" so that it displays "Click here!" on the html page. Click on that and check what is displayed in the address bar.
Also, give an SOP in the pgm to check the value of the string 'action'
subba rai

Joined: Jan 03, 2005
Posts: 5
hello friends,
sorry for not making myselef very clear. I have tried to print the string object "action" and also i have given a name to my link. After clicking the link action is assigned null value but not "invalidate".
sorry for not telling these things in my code these things in my previous query. Now u can think about what may be the problem
thank u in advance
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Did you try what I suggested -- print the value at the top of your doPost or doGet method?

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Your link seems to turn out to be

<a href="http://localhost:8080/servlets-examples/Session1"?action=invalidate>

this will not send the action parameter in.

the link should be <a href="http://localhost:8080/servlets-examples/Session1?action=invalidate"/> instead.

And as Ben said, there's no place in your posted code that lets you find out whether or not action is null.
[ April 12, 2005: Message edited by: Yuriy Zilbergleyt ]
subba rai

Joined: Jan 03, 2005
Posts: 5
thank u friends...i have found out the mistake
correct usage:
out.println("<a href=\"http://localhost:8080/servlets-examples/Session1?action=invalidate\">");
wrong usage:
out.println("<a href=\"http://localhost:8080/servlets-examples/Session1\""+"?action=invalidate>");
once again thanks.....bye for now
I agree. Here's the link:
subject: retrieving a parameter from a link
It's not a secret anymore!