servlets question about dopost and doget

Discussion in 'Java' started by Ryan Gaffuri, Sep 29, 2004.

  1. Ryan Gaffuri

    Ryan Gaffuri Guest

    When I go to a url that is a servlet:

    The following happens(please let me know if I am right)

    1. init()
    2. doGet()
    -- if i have my webpage in this function, that is executed.
    3. how do i get dopost to fire? I tried putting
    <FORM METHOD=POST> into my code, but my dopost code is not fired?
    I have example code that does and fires the dopost method which just
    writes some of the values entered in the form to the screen. Without
    posting tons of code, I am having trouble getting my 'doPost' to fire
    from my form which is executed by 'doGet'.

    when i click submit, all that happens is that the current screen is
    refreshed.
    my sample code is as follows. it does not have any 'action' in it.
    what in this code is actually getting the 'dopost' method to execute?

    this code is out of ;java for the web with servlets' book.

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class HttpRequestDemoServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse
    response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>Obtaining Multi-Value Parameters</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");

    out.println("<BR>");
    out.println("<BR>Select your favorite music:");
    out.println("<BR><FORM METHOD=POST>");
    out.println("<BR><INPUT TYPE=CHECKBOX " +
    "NAME=favoriteMusic VALUE=Rock>Rock");
    out.println("<BR><INPUT TYPE=CHECKBOX " +
    "NAME=favoriteMusic VALUE=Jazz>Jazz");
    out.println("<BR><INPUT TYPE=CHECKBOX " +
    "NAME=favoriteMusic VALUE=Classical>Classical");
    out.println("<BR><INPUT TYPE=CHECKBOX " +
    "NAME=favoriteMusic VALUE=Country>Country");
    out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
    out.println("</FORM>");
    out.println("</BODY>");
    out.println("</HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse
    response)
    throws ServletException, IOException {

    String[] values = request.getParameterValues("favoriteMusic");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    if (values != null ) {
    int length = values.length;
    out.println("You have selected: ");
    for (int i=0; i<length; i++) {
    out.println("<BR>" + values);
    }
    }
    }
    }
    Ryan Gaffuri, Sep 29, 2004
    #1
    1. Advertising

  2. Ryan Gaffuri

    Madhur Ahuja Guest

    It worked fine on my computer. Running :
    java version "1.4.1_01"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
    Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

    and Tomcat 5.0.28.

    I think your servlet might need reloading. Try stopping and restarting
    your server.

    --
    Madhur Ahuja [madhur<underscore>ahuja<at>yahoo<dot>com]

    Homepage
    http://madhur.netfirms.com
    Madhur Ahuja, Sep 29, 2004
    #2
    1. Advertising

  3. (Ryan Gaffuri) writes:

    > <FORM METHOD=POST> into my code,


    That should be enough, as long as you also put an ACTION with your
    servlet. That particular tag doesn't, it will just pass the form values
    to the web page. (Default ACTION=".")
    Tor Iver Wilhelmsen [TeamB], Sep 29, 2004
    #3
  4. Ankur Bulsara, Sep 29, 2004
    #4
  5. Ryan Gaffuri

    Sudsy Guest

    Ankur Bulsara wrote:
    > One very common strategy is to have a doService method that both doPost and
    > doGet forward to. Using this technique, it doesn't matter what your form
    > action is a GET or POST will get handled by doService.


    "Very common"? I'd have to disagree. If I've got a servlet designed for
    form processing then I only want to accept the POST method. My doGet in
    these circumstances looks like this:

    public void doGet( HttpServletRequest req,
    HttpServletResponse resp ) {
    try {
    resp.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED,
    "GET not supported" );
    }
    catch( Exception e ) {
    }
    }
    Sudsy, Sep 30, 2004
    #5
    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. Raoul Markus

    parameter in HttpServlet.doGet

    Raoul Markus, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    682
    Raoul Markus
    Aug 21, 2003
  2. bigbinc

    Fill doGet, doPost in servlet

    bigbinc, Feb 26, 2004, in forum: Java
    Replies:
    1
    Views:
    5,607
    Oscar kind
    Feb 26, 2004
  3. F. H.
    Replies:
    1
    Views:
    1,549
    Erwin Moller
    Aug 23, 2004
  4. Jeff Kish

    doget dopost handler

    Jeff Kish, Feb 13, 2007, in forum: Java
    Replies:
    0
    Views:
    334
    Jeff Kish
    Feb 13, 2007
  5. Replies:
    2
    Views:
    408
    Chris ( Val )
    Nov 13, 2007
Loading...

Share This Page