This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving a parameter from a link

 
subba rai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
response.setContentType("text/html");
if(action != null && action.equals("invalidate"))
{
HttpSession session = request.getSession();
session.invalidate();
}
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Varun Dikshit
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try what I suggested -- print the value at the top of your doPost or doGet method?

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic