• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending parameters with a link

 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shhhhhh! (I need help with my homework!)
Marilyn has given me a nitpick that requires me to find a way to send a parameter along with a get request via an href link from a jsp to a servlet. I was sending the request directly from one jsp to another, but I guess that is offensive to MVC2, so I shouldn't do it that way. My mission, then, is to find a way to send a parameter along with the get request that is automatically part of an href link. I don't want the answer. (Well I do, but it would be cheating.) But a big, fat, clue would be appreciated.
I've already tried simply appending something that looked like
?name=action&value=add_a_video
on to the url in the link itself, but that didn't work at all. The getParameter( action ) method in my servlet still produced a null value. Please point me in the right direction. I know the answer is out there somewhere!
[ June 23, 2006: Message edited by: Carol Murphy ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carol Murphy:

I've already tried simply appending something that looked like
?name=action&value=add_a_video
on to the url in the link itself,


That should work. Are you sure that the URL is formed exactly that way and that the special characters aren't getting encoded?

What does the full URL look like in the address bar?
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what shows in the address bar of the browser:
http://localhost/videos/servlet/VideoServlet?name=action&value=add

This is the hyperlink from the jsp:

<a href='<%=PathFinder.getPathFromJSPToUnMappedServlet( request ) + "VideoServlet" +"?name=action&value=add"%>'>

I just now realised I could do this with one set of double quotes, but it won't change the result. My servlet doesn't see the parameter "action". When I call servletData.getParameter( "action" ), the result is a null String. This same process works when I send the parameters in a form using post or get. In fact, I use it in the same servlet and it works there. I'm confused as to why the parameter can't be read this way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I call servletData.getParameter( "action" ),


What is servletData?
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletData is a class in the com.javaranch.common package that handles a bunch of stuff with the HttpServletRequest and HttpServletResponse objects. servletData is the ServletData object that gets passed into the doAction() method of ActionServlet, which is another class in the same package.
Here's a snippet from ServletData.java:

/** Same as getRequest.getParameter().
*/
public String getParameter( String parameterName )
{
return request.getParameter( parameterName );
}
I added a servletData.getRequest().getQueryString(); line to my servlet along with a System.out.print() and the query string is being printed in the dos window as name=action&value=list (I changed the value from add to list.) However, the same servletData.getParameter( "action" ) ; line still returns a null value. So the query string is being sent along, but the container isn't parsing it into the name/value pair I guess. Wierd, huh?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I was wrong. It isn't a problem in your URL. So I'm going to move this over to the Servlets forum for further discussion.

Keep your arms inside the car and hold on to your personal belongings...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have direct access to the request instance? If so, I'd cut out the middle-man as a possible source of error and call getParameter() directly and see what we get there.

Originally posted by Carol Murphy:
So the query string is being sent along, but the container isn't parsing it into the name/value pair I guess. Wierd, huh?


Usually this happens when a form that sends a non-standard enctype is submitted. But since you're typing this into the address bar, that can't be the case...
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No direct access.

This will not compile because request cannot be resolved by compiler:
String param = request.getParameter( "action" ) ;

The way ActionServlet is coded, doGet() and doPost() are both final methods. They are called from init() which is an abstract method in ActionServlet. There is an ActionHandlerInterface that figures out which of the two methods to call, and you put that code inside init() in your servlet. This is a code snippet from ActionServlet:

public abstract void init() throws ServletException;

/**
* Override init and call this method for every action to be implemented.
*
* @param actionKey The text that will be sent in the servlet parameter to kick off this action.
*/

Since VideoServlet extends ActionServlet, I can't just call doGet() directly. Hmmm. I'm going to try extracting the value from the query string and see if it works as I want it to. Then I'll worry about why the parameter isn't being read.
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, well, well. I took the value string out of the query using
substring() and then I got a null pointer exception in doGet() (!) so I went back and put the offending code inside an if statement, so it would be bypassed if it was null, and voila! My code now works like I want it too. But I'm still a little confused as to what is going on with the parameters in doGet().
I'm going to submit this and see what Marilyn thinks of my solution!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With query strings the name appears to the left of the equals sign ('=') and the value appears on the right.

You had:


All you need is
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. I've never encountered that explanation in any books I've read on this subject. I'm at work so I can't check it out, but I will do so when I get home. So my parameters would have been name and value, and the values for my 2 parameters would be action and list, right? Interesting. Thanks for the hot tip!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
With query strings the name appears to the left of the equals sign ('=') and the value appears on the right.


Holy forest-for-the-trees, Batman!

Great catch Ben! I was so busy looking at the syntax and delimiters that I never noticed that!

Yes Carol, getParameter will fetch the value for the passed name.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic