Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing variables to a servlet...

 
Stone Golem
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stone Golem
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic