doPost needs to call doGet or vice versa

-----Original Message-----
From: Bo Xu [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 27, 2001 1:50 PM
To: [EMAIL PROTECTED]
Subject: Re: Problems in POST


Colin Morris wrote:

> Hey BO!
>
>         Thanks! I thought that might be one of the possibilities.
> I've only overrident the doGet() method. However, overriding
> the doPost() method... I'm not clear on how to write it to
> accept values from my form? Or do I just write a blank method?
> (I'm new to java as well...)
>
>          I'll try it. Hopefully it's nothing to do with my
> Tomcat settings? I'll try overriding as u suggested.
>
> On 27 Mar 2001, at 15:53, Bo Xu wrote:
> >
> > Hi :-)  I guess it is because:
> > did you "override" both doGet/doPost method in your Servlt class?
> > if you enter a URL in browser directly, this is a "GET request", so I
> > guess
> > perhaps you forget the doPost.
> >
> > you also can only override "service" method, now both GET/POST
> > request will work.
> >
> >
> > Bo
> > Mar.27, 2001
> [...]

Hi :-)  the following is a simple demo, as a reference, there is a book
(Java Servlet Programming, Jason Hunter ISBN=1-56592-391-X)
which includes good details. :-)

and the "doGet/doPost/service method" canot be "empty", we need to
"response" something to the browser, please see the following sample
code.

*****************************html****************************
<HTML><BODY>
  <FORM
ACTION=http://host:8080/myapp/servlet/MyServlet?tailPrecv0=tailPrecv0V
METHOD=GET>
     <INPUT TYPE=hidden NAME=bodyPrecv0 VALUE=bodyPrecv0V>
     <INPUT TYPE=submit VALUE=GETtest>
  </FORM>

  <FORM
ACTION=http://host:8080/myapp/servlet/MyServlet?tailPrecv0=tailPrecv0V
METHOD=POST>
     <INPUT TYPE=hidden NAME=bodyPrecv0 VALUE=bodyPrecv0V>
     <INPUT TYPE=submit VALUE=POSTtest>
  </FORM>
</BODY></HTML>

*****************************Servlet****************************
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;

public class MyServlet extends HttpServlet{
   public void init(ServletConfig config) throws ServletException{
      super.init(config);
   }

   public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
      try{
   System.out.println( "in 0, getMethod()="+request.getMethod()+",
getQueryString(): "+request.getQueryString() );
   Enumeration enum=request.getParameterNames();
   int i=0;
   while( enum.hasMoreElements() ){

System.out.println("request.getParameterNames("+i+")="+enum.nextElement());

   i++;
   }

         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         out.println("<HTML><BODY>");
         out.println("ok");
         out.println("</BODY></HTML>");
         out.close();
}catch(Exception e){System.out.println("e="+e);}
}

}



Reply via email to