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 Passing variables to a servlet... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing variables to a servlet..." Watch "Passing variables to a servlet..." New topic
Author

Passing variables to a servlet...

Stone Golem
Greenhorn

Joined: Oct 27, 2001
Posts: 8
Hi all,
Inside my servlet, I have the lines:
String action = request.getParameter("ACTION");
if (action.equals("admin")) {
//do some thing really, really cool here...
}
I want the servlet to grab a parameter from the URL line. In my JSP, I've got the line (which doesn't work!!):
<a href="../<path>/PortalServlet?ACTION=admin">Portal (admin)</a><br>
I can change the JSP any way I want, but I can't change the Servlet. Any ideas on how to do this?? Is this even possible??

<thought>
the servlet is able to take arguments from the JSP using the hidden tag field with a button (this is why I can't change the servlet)... <input type="hidden" name="ACTION" value="admin"> ...this is the only alternative I can think of.... Is there a method to accomodate both??
</thought>
I hope I've provided enough info....
Thanks in advance for your response.
Stone
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
First:
String action = request.getParameter("ACTION");
if (action.equals("admin")) {

If there is no parameter "ACTION" your servlet will throw a NullPointerException right there.
"I hope I've provided enough info...."
Not for me at least
What are the error messages?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Stone Golem
Greenhorn

Joined: Oct 27, 2001
Posts: 8
I get a page not found error.....
The following line appears in my address bar... http://localhost:8080/portal/classes/PortalServlet?ACTION=admin
In my if else statement, the else part is a...
response.sendRedirect("../jsp/anotherPage.jsp").
Stone
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
If your calling a servlet with that code it will never find it.......the url must be /servlet/servletName NOT classes! In your case it would be http://localhost:8080/portal/servlet/PortalServlet?ACTION=admin
[This message has been edited by DC Dalton (edited October 31, 2001).]
Stone Golem
Greenhorn

Joined: Oct 27, 2001
Posts: 8
Thanks for the response DC, but since it accepts arguments from other jsp's using the hidden fields, I assume that the directory stucture is okay...
For argument sake, I made a copy of my class, and placed it in the directory that you suggested. I also changed the path in my jsp. I got the same error.
Thanks though...
Stone
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by DC Dalton:
[...] the url must be /servlet/servletName NOT classes! [...]
Well, I don't know, DC. If you ask me, the url must be whatever you configured the web.xml to map the servlet to.
Regarding the original question, unless the servlet is coded to reject GET requests there is no reason whatsoever why the link won't work (barring servlet engine bugs). It isn't something silly like a space or control character slipped in somewhere, is it?
Remember, the "Page not found" response does not necessarily apply to the servlet - it may be that the servlet throws an exception and the server tries to go to an incorrectly set up error page, or that the page the servlet attempts to forward to doesn't exist and the error handling code sends back a 404 response.
- Peter
[This message has been edited by Peter den Haan (edited November 01, 2001).]
 
 
subject: Passing variables to a servlet...