File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes sending parameters with a link Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending parameters with a link" Watch "sending parameters with a link" New topic
Author

sending parameters with a link

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

when I call servletData.getParameter( "action" ),


What is servletData?
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

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

Joined: Jan 10, 2002
Posts: 61084
    
  66

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

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending parameters with a link