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:
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.
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:
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.
Joined: Apr 23, 2011
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???
Joined: Jul 06, 2005
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:
Joined: Apr 23, 2011
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).