KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > servlet > http > HttpServlet

javax.servlet.http
Class HttpServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
See Also:
Top Examples, Source Code

protected void doDelete(HttpServletRequest req,
                        HttpServletResponse resp)
                 throws ServletException,
                        IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException
See Also:
ServletResponse.setContentType(java.lang.String), ServletResponse.setContentLength(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[142]Hello World servlet
By Anonymous on 2004/04/15 12:25:55  Rate
import java.io.*;  
 import javax.servlet.*; 
 import javax.servlet.http.*; 
  
  
 /** Very simplistic servlet that generates plain text. 
 * 
 * Taken from More Servlets and JavaServer Pages 
 * from Prentice Hall and Sun Microsystems Press, 
 * http://www.moreservlets.com/. 
 * ?? 2002 Marty Hall; may be freely used or adapted. 
 */
 
  
  
 public class HelloWorld extends HttpServlet  {  
 public void doGet ( HttpServletRequest request, HttpServletResponse response )  
 throws ServletException, IOException  {  
   PrintWriter out = response.getWriter (  ) ; 
   out.println ( "Hello World" ) ; 
  }  
  
  
  } 


protected void doHead(HttpServletRequest req,
                      HttpServletResponse resp)
               throws ServletException,
                      IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void doOptions(HttpServletRequest req,
                         HttpServletResponse resp)
                  throws ServletException,
                         IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void doPost(HttpServletRequest req,
                      HttpServletResponse resp)
               throws ServletException,
                      IOException
See Also:
ServletResponse.setContentType(java.lang.String), ServletOutputStream, ServletResponse.setContentLength(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void doPut(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void doTrace(HttpServletRequest req,
                       HttpServletResponse resp)
                throws ServletException,
                       IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected long getLastModified(HttpServletRequest req)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public HttpServlet()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[176]A servlet that prints out request attributes
By Edward Sumerfield on 2003/03/14 08:52:59  Rate
package mypackage; 
  
  
 import java.io.OutputStream; 
 import java.io.PrintStream; 
  
  
 import javax.servlet.ServletConfig; 
 import javax.servlet.ServletException; 
 import javax.servlet.http.HttpServlet; 
 import javax.servlet.http.HttpServletRequest; 
 import javax.servlet.http.HttpServletResponse; 
  
  
 import conceptdefgen.util.Url; 
  
  
 public class MyServlet extends HttpServlet 
  {  
     /** 
      * Returns a short description of the servlet. 
      * 
      * @return  The ServletInfo value 
      */
 
     public String getServletInfo (  )  
      {  
         return "MyServLet"; 
      }  
  
  
     /** 
      * Initializes the servlet. 
      * 
      * @param config Description of Parameter 
      * @exception ServletException Description of Exception 
      */
 
     public void init ( ServletConfig config )  throws ServletException 
      {  
         super.init ( config ) ; 
      }  
  
  
     /** 
      * Destroys the servlet. 
      */
 
     public void destroy (  )  
      {  
      }  
  
  
     /** 
      * Processes requests for both HTTP  < code > GET < /code >  and  < code > POST < /code >  
      * methods. 
      * 
      * @param request servlet request 
      * @param response servlet response 
      * @exception ServletException Description of Exception 
      * @exception java.io.IOException Description of Exception 
      */
 
     protected void processRequest (  
         HttpServletRequest request, 
         HttpServletResponse response )  
         throws ServletException, java.io.IOException 
      {  
         // Get the output stream to write the response to. 
         OutputStream l_out = response.getOutputStream (  ) ; 
   PrintStream l_print = new PrintStream ( l_out ) ; 
  
  
         // Default all responses to html. 
         response.setContentType ( "text/html" ) ; 
  
  
         l_println ( " < html > " ) ; 
  
  
         l_println ( " < p > This is MyServlet < /p > " ) ; 
  
  
         l_println ( "URL details: < br/ > " ) ; 
         l_println (  request.getProtocol (  )   ) ; 
         l_println (  request.getServerName (  )   ) ; 
         l_println (  request.getServerPort (  )   ) ; 
         l_println (  request.getRequestURI (  )   ) ; 
         l_println (  request.getQueryString (  )   ) ; 
          
         l_println ( " < /html > " ) ; 
          
         // Flush and close the output stream 
         l_print.flush (  ) ; 
         l_print.close (  ) ; 
      }  
  
  
     /** 
      * Handles the HTTP  < code > GET < /code >  method. 
      * 
      * @param request servlet request 
      * @param response servlet response 
      * @exception ServletException Description of Exception 
      * @exception java.io.IOException Description of Exception 
      */
 
     protected void doGet (  
         HttpServletRequest request, 
         HttpServletResponse response )  
         throws ServletException, java.io.IOException 
      {  
         processRequest ( request, response ) ; 
      }  
  
  
     /** 
      * Handles the HTTP  < code > POST < /code >  method. 
      * 
      * @param request servlet request 
      * @param response servlet response 
      * @exception ServletException Description of Exception 
      * @exception java.io.IOException Description of Exception 
      */
 
     protected void doPost (  
         HttpServletRequest request, 
         HttpServletResponse response )  
         throws ServletException, java.io.IOException 
      {  
         processRequest ( request, response ) ; 
      }  
  }  
 


protected void service(HttpServletRequest req,
                       HttpServletResponse resp)
                throws ServletException,
                       IOException
See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void service(ServletRequest req,
                    ServletResponse res)
             throws ServletException,
                    IOException
See Also:
HttpServletResponse, HttpServletRequest, GenericServlet, Servlet
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags