calling a Servlet from a jsp

V

vaneric

hi,
I have an index.jsp where i am collecting user input through form
fields.I want to send the data to doPost() method of a Servlet.I am
deploying my web app at a context, say 'myapp'.

index.jsp
--------
<body>
<h2>Entry Form</h2>
<form name="Name Input Form" method="Post" action="/posthandler">
Enter your Name:
<input type="text" name="name" value="" />
<input type="submit" value="OK" />
</form>
</body>

In my servlet
-------------

doPost()....{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );

String name = request.getParameter("name");
out.println("<html>");
out.println("<body>");
out.println("<h1>You Entered</h1>");
out.println(
"Your name is: " + ( (name == null || name.equals("")) ?
"Unknown" : name));

...
}

In web.xml ,I mapped the servlet
<servlet>
<servlet-name>PostHandler</servlet-name>
<servlet-class>org.mypackage.PostHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PostHandler</servlet-name>
<url-pattern>/posthandler</url-pattern>
</servlet-mapping>

But when I click the Submit button in the html page created by
index.jsp ,it tries to access
http://localhost:8080/posthandler
I suppose what needs to be invoked is
http://localhost:8080/myapp/posthandler

Can someone tell me how I can do that from index.jsp ? I got the
correct result when I put

<form name="Name Input Form" method="Post" action="/myapp/
posthandler">

But I am a little doubtful.The context name 'myapp' is something I
give in build.xml .Hardcoding that in index.jsp doesn't feel right

Please advise
thanks
eric
 
L

Lew

vaneric said:
hi,
I have an index.jsp where i am collecting user input through form
fields.I want to send the data to doPost() method of a Servlet.I am
deploying my web app at a context, say 'myapp'.

index.jsp

Have you tried the action without the leading slash?

The value for method should be "post", all lower case.

Names associated with HTML or XML elements shouldn't have spaces in them.
Enter your Name:
<input type="text" name="name" value="" />

Make yourself ready for the modern world by giving most elements, at least all
input elements, 'id' attributes.
<input type="submit" value="OK" />
</form>
</body>

In my servlet
-------------

doPost()....{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );

String name = request.getParameter("name");
out.println("<html>");

You shouldn't have view artifacts in a .java file. Use JSPs or the like to
handle the view layer of your model-view-controller (MVC) architecture.

Only the controller and model components should be written as .java source.
out.println("<body>");
out.println("<h1>You Entered</h1>");
out.println(
"Your name is: " + ( (name == null || name.equals("")) ?
"Unknown" : name));

..
}
....
But when I click the Submit button in the html page created by
index.jsp ,it tries to access
http://localhost:8080/posthandler

Because you specified an absolute path, not a relative one, in your 'action'
attribute of the 'form' element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top