servlets question about dopost and doget

R

Ryan Gaffuri

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);
}
}
}
}
 
M

Madhur Ahuja

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.
 
T

Tor Iver Wilhelmsen [TeamB]

<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=".")
 
S

Sudsy

Ankur said:
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 ) {
}
}
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top