HelloServlet class inherits HttpServlet exploits HttpServletResponse objects

Source: Internet
Author: User

HelloServlet class inherits HttpServlet exploits httpservletresponse Objects

The doget () method of the HelloServlet class first obtains the username request parameter, converts it into a Chinese character encoding, and then judges if the username is empty, returns the 403 Response status Code if it is empty , otherwise it passes the httpservletresponse object's getwriter () method to get a Printerwriter object, and then through the PrintWriter object to Output HTML Document

Helloservlet.java

PackageCom.demo.servlet;

ImportJavax.servlet.ServletException;
ImportJavax.servlet.http.HttpServlet;
ImportJavax.servlet.http.HttpServletRequest;
ImportJavax.servlet.http.HttpServletResponse;
ImportJava.io.IOException;
ImportJava.io.PrintWriter;
/* Created by Tan Xiajiao on 2017/4/5.
 */
Public classHelloServletextendsHttpServlet {
Public voidDoget (HttpServletRequest request, httpservletresponse response)
throwsservletexception,ioexception{
//Get Sername request Parameters
        String Username=request.getparameter ("username");
/ * character encoding conversion
* The default character encoding for HTTP requests is iso-8859-1, and if the request contains Chinese, it needs to be converted to GB2312 Chinese encoding. */
        if(username!=NULL){
Username=NewString (Username.getbytes ("Iso-8859-1"),"UTF-8");
if(username==NULL){
Response.senderror (response.Sc_forbidden);
return;
}
//Set the MIME type and character encoding of the body of the HTTP response
            Response.setcontenttype ("Text/html;charset=utf-8");
/ * Output HTML document * /
            PrintWriter Out=response.getwriter ();
Out.println (");
Out.println ("<body>");
Out.println ("Hello:"+username);
Out.println ("</body>);
System. out. println ("before Close ():"+response.iscommitted ());
Out.close ();
System. out. println ("before Close ():"+response.iscommitted ());
}
}
}

Xml

<?XML version= "1.0"encoding= "UTF-8"?>
<Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:XSI= "Http://www.w3.org/2001/XMLSchema-instance"
         XSI: schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version= "3.1">
<Context-param>
<Param-name>contextconfiglocation</Param-name>
<Param-value>/web-inf/applicationcontext.xml</Param-value>
</Context-param>
<Listener>
<Listener-class>org.springframework.web.context.contextloaderlistener</Listener-class>
</Listener>
    <servlet>
<Servlet-name>dispatcher</Servlet-name>
<Servlet-class>com.demo.servlet.requestinfoservlet</Servlet-class>

</servlet>
<servlet-mapping>
<Servlet-name>dispatcher</Servlet-name>
<Url-pattern>/info</Url-pattern>
</servlet-mapping>
<servlet>
<Servlet-name>dispatcher1</Servlet-name>
<Servlet-class>com.demo.servlet.helloservlet</Servlet-class>
</servlet>
<servlet-mapping>
<Servlet-name>dispatcher1</Servlet-name>
<Url-pattern>/hello</Url-pattern>
</servlet-mapping>
</Web-app>

Run results

Tomcat Location Console results

Browser results:

HelloServlet class inherits HttpServlet exploits HttpServletResponse objects

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.