Javaweb Core Programming (three. 6) HttpServlet

Source: Internet
Author: User

Previously integrated servlets are really too cumbersome, the servlet interface provides an implementation class for HttpServlet as long as the doget and Dopost methods can be implemented

Still take a form as an example

Create a new Web project

1  Packagecom.xiaoan.test;2 3 ImportJavax.servlet.http.HttpServlet;4 Importjavax.servlet.http.HttpServletRequest;5 ImportJavax.servlet.http.HttpServletResponse;6 7  Public classTesthttpservletextendshttpservlet{8 9     /**Ten      *  One      */ A     Private Static Final LongSerialversionuid = 1L; -  -      the      Public voiddoget (httpservletrequest req, HttpServletResponse resp) { -System.out.println ("GET METHOD"); -     } -      +      Public voidDoPost (httpservletrequest req, HttpServletResponse resp) { -System.out.println ("POST METHOD")); +     } A}

Edit Web. xml file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">3   <Display-name>Testhttpservlet</Display-name>4   5   <servlet>6       <Servlet-name>Testhttpservlet</Servlet-name>7       <Servlet-class>Com.xiaoan.test.TestHttpServlet</Servlet-class>8   </servlet>9   <servlet-mapping>Ten       <Servlet-name>Testhttpservlet</Servlet-name> One       <Url-pattern>/testhttpservlet</Url-pattern> A   </servlet-mapping> - </Web-app>

Running the project access path to the Http://localhost:8080/testHttpServlet/testHttpServlet console outputs the Get METHOD

Write a form to submit its post to this path

Create a new testform.jsp inside the WebContent

1 <%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"2 pageencoding="iso-8859-1"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten     <formAction= "Testhttpservlet"Method= "POST"> One     <inputtype= "Submit"value= "Tijiao"> A     </form> - </Body> - </HTML>

To run this JSP page, click Submit Note the changes in the browser and the console output as post METHOD

HttpServletRequest HttpServletResponse The methods used here and Serlvet.service methods can be considered exactly the same

And in HttpServlet's parent class Genericservlet gets the servlet initialization in the Init method ServletConfig as a member property can be called directly

Servletresponse: Encapsulates the response information, if you want to give the user what response, you can use the method of the interface to implement

1> getwriter (): Returns the PrintWriter object. Call the object's Pring () method to print the parameters in the Pring () directly to the customer's browser
PrintWriter out = Response.getwriter ();
Out.println ("Neirong");

2> Setting Response content Type: Response.setcontenttype ("Application/msword");

3> woid sendredirect (String location): Redirect Requested (this method is defined in HttpServletResponse)

Practice
Set two Web initialization parameters in the. xml file user, password. Define a login.html, which defines two request fields user, password. This is consistent with the request parameters defined in the Web. xml file, and responds with hello:xxx, inconsistent response sorry:xxx

1GenericServlet:
1> is a servlet. is the implementation class for the Servlet interface and the ServletConfig interface. But it's an abstract class. Where the servlet method is an abstract method
2> If the new Servlet program inherits directly from Genericservlet it will make development more concise
3> Specific implementations:
1) A member variable of type ServletConfig is declared in Genericservlet and initialized in the init (ServletConfig) method.
2) The method of ServletConfig interface is realized by using the method of SERVLETCONFIG member variable.
3) also defines an init () method, which is called in the init (ServletConfig) method, and subclasses can directly override Init () in which the servlet is initialized
4) It is not recommended to overwrite Init (servletconfig) directly, because if you forget to write Super.init (config); Instead, the ServletConfig interface method is used, and a null pointer exception appears
5) The new init () {} is not a life-cycle method for the servlet. and init (ServletConfig) is a life-cycle related approach


2 HttpServlet:
1 is a servlet that inherits from Genericservlet. Customized for the HTTP protocol

Javaweb Core Programming (three. 6) HttpServlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.