Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   calling a Servlet from a jsp (http://www.velocityreviews.com/forums/t647905-calling-a-servlet-from-a-jsp.html)

vaneric 12-09-2008 02:23 PM

calling a Servlet from a jsp
 
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

Lew 12-09-2008 03:10 PM

Re: calling a Servlet from a jsp
 
vaneric wrote:
> 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">


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.

--
Lew

vaneric 12-09-2008 04:41 PM

Re: calling a Servlet from a jsp
 
On Dec 9, 8:10*pm, Lew <no...@lewscanon.com> wrote:

> Have you tried the action without the leading slash?
>



Thank you for the help..

eric


All times are GMT. The time now is 04:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.