Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > servlets question about dopost and doget

Reply
Thread Tools

servlets question about dopost and doget

 
 
Ryan Gaffuri
Guest
Posts: n/a
 
      09-29-2004
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[i]);
}
}
}
}
 
Reply With Quote
 
 
 
 
Madhur Ahuja
Guest
Posts: n/a
 
      09-29-2004
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






 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen [TeamB]
Guest
Posts: n/a
 
      09-29-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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=".")
 
Reply With Quote
 
Ankur Bulsara
Guest
Posts: n/a
 
      09-29-2004
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.

You can see an example of this technique here:
http://www.koders.com/java/fid8EE564...-mSHBNqCwFvA__


 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      09-29-2004
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 ) {
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd problem with doGet/doPost in same servlet canoe414@telecom-digest.zzn.com Java 2 11-13-2007 12:35 PM
doget dopost handler Jeff Kish Java 0 02-13-2007 09:21 PM
Calling protected doGet() method of Tomcat DefaultServlet F. H. Java 1 08-23-2004 10:49 AM
Fill doGet, doPost in servlet bigbinc Java 1 02-26-2004 06:08 PM
parameter in HttpServlet.doGet Raoul Markus Java 0 08-21-2003 10:35 PM



Advertisments