calling a Servlet from a jsp

Discussion in 'Java' started by vaneric, Dec 9, 2008.

  1. vaneric

    vaneric Guest

    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
    vaneric, Dec 9, 2008
    #1
    1. Advertising

  2. vaneric

    Lew Guest

    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
    Lew, Dec 9, 2008
    #2
    1. Advertising

  3. vaneric

    vaneric Guest

    On Dec 9, 8:10 pm, Lew <> wrote:

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



    Thank you for the help..

    eric
    vaneric, Dec 9, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    1,981
    Jack Jia
    Apr 4, 2004
  2. Will
    Replies:
    1
    Views:
    2,063
    Raymond DeCampo
    Aug 21, 2005
  3. Jawahar
    Replies:
    1
    Views:
    528
    Simon Mieth
    Sep 23, 2003
  4. javadev
    Replies:
    5
    Views:
    12,860
    javadev
    Nov 16, 2006
  5. Replies:
    4
    Views:
    767
Loading...

Share This Page