wood burning stoves 2.0*
The moose likes JSP and the fly likes newbie form submit question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "newbie form submit question" Watch "newbie form submit question" New topic
Author

newbie form submit question

Mark Kelsall
Greenhorn

Joined: Apr 23, 2011
Posts: 19
hi guys,

i appreciate that this is probably going to be a very straight forward answer or point in the right direction but i'm struggling to find the right words to search in google.

I want a form & i want to submit to my controller serviet (straight forward enough) but the complication is that my form is in a folder & obviously the servlet is at the application root. Let me give you an example:

folder structure:
root:
web-inf/
meta-inf/
css/
register/
register.jsp

i have a form on the register.jsp & i want to submit it to my servlet which obviously lives at the root. What do I need to put in the action of the form to get it to submit to the root?

Thanks in advance

Mark
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your servlet can't be at the root of your application's folder tree. It has to be in the /WEB-INF/classes folder. That's WEB-INF, not web-inf by the way. And it has to be in a directory structure which matches its package declaration as well. And finally you'll need a servlet mapping in your web.xml deployment description which maps the URL you plan to use to the servlet.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
The obvious (and incorrect) answer would be <form action="/myServlet">

The gotcha here of course being that you need to allow for the web context that you are in.

So the answer becomes:
<form action="${pageContext.request.contextPath}/myServlet">

And of course there has to be a mapping for "/myServlet" in your web.xml file.

This is one thing I like about using a framework, is that they will abstract away annoying stuff like this, and just let you specify a web application relative path to your form handler.


Mark Kelsall
Greenhorn

Joined: Apr 23, 2011
Posts: 19
Hi Paul,

Thanks for your reply. Yeah I understand that it actually isn't at the root of the application folder tree & i only typed it like that just because I was being lazy :P I've got it all set up architecturally OK & working, I was just lacking this missing piece, which Stefan has been able to help with!

Stefan, that's brilliant, I never knew about that! Yeah I know that a framework is the way forward, but i'm still learning so I want to understand all the intricate bits "under the hood", i'm a geek like that, but aren't we all!

Edit: I have tried the code now but it unfortunately doesn't work out of the box. If for example my controller servlet is defined as follows



in my web.xml. How would I be able to submit to that from a jsp page in the root WebContent folder & also one in register which is a sub-folder of WebContent???

Thanks
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I think the only thing you are missing is a / character in your servlet mapping:



And then the following form tag should work from whichever jsp you submit from:


Mark Kelsall
Greenhorn

Joined: Apr 23, 2011
Posts: 19
Again you're right Stefan! I revisited it & stepped through it with a bit more patience instead of wanting it to work out of the box (like you always do when you get so frustrated that you post it on a forum).

Anyway thanks for your help, much appreciated :)

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbie form submit question