Java Web Experiment Five servlet filter

Source: Internet
Author: User

Loginform.html

<title> Change request encoding using filters </title>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<body>
<center>
<form method= "POST" action= "Servlet/checkparamservlet" >
<table>
<tr>
<td> User name:</td>
<td><input name= "name" type= "text" ></td>
</tr>
<tr>
<td> Password:</td>
<td><input name= "pass" type= "password" ></td>
</tr>
<tr>
<td></td>
<td>
<input name= "OK" type= "submit" value= "Submission" >
<input name= "Cancel" type= "reset" value= "reset" >
</td>
</tr>
</table>
</form>
</center>
</body>

Checkparamservlet. Java in the servlet

Package servlet;

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;

public class Checkparamservlet extends httpservlet{
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {

String name = Request.getparameter ("name");
String pass = request.getparameter ("Pass");
Response.setcontenttype ("text/html;charset=gb2312");
PrintWriter out = Response.getwriter ();

Out.println ("Out.println ("

Out.println ("

Out.println ("</body>}

public void DoPost (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {

Doget (Request,response);
}
}

Filter inside the Encodingfilter

Package filter;

Import java.io.IOException;

Import javax.servlet.*;

public class Encodingfilter implements Filter {

protected String encoding = NULL;

Protected Filterconfig config;

public void init (Filterconfig filterconfig) throws Servletexception {

This.config = Filterconfig;

get the encoding configured in Web. xml

this.encoding = Filterconfig.getinitparameter ("encoding");

}

public void DoFilter (

ServletRequest request,

Servletresponse response,

Filterchain chain)

Throws IOException, Servletexception {

if (request.getcharacterencoding () = = null) {

gets the specified encoding

String encode = GetEncoding ();

if (encode! = null) {

set The encoding of the request

Request.setcharacterencoding (encode);

Response.setcharacterencoding (encode);

}

}

Chain.dofilter (request, response);

}

Protected String getencoding () {

return encoding;

}

public void Destroy () {

}

}

Xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
Id= "webapp_id" version= "3.1" >
<display-name>meng</display-name>
<servlet>
<servlet-name>CheckParamServlet</servlet-name>
<display-name>this is the display name of my EE component</display-name>
<description>this is the description of my EE component</description>
<servlet-class>servlet. Checkparamservlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CheckParamServlet</servlet-name>
<url-pattern>/servlet/CheckParamServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>CheckParamServlet</servlet-name>
<servlet-class>servlet. Checkparamservlet</servlet-class>
</servlet>
<servlet>
<servlet-name>EncodingFilter</servlet-name>
<display-name>this is the display name of my EE component</display-name>
<description>this is the description of my EE component</description>
<servlet-class>filter. Encodingfilter</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CheckParamServlet</servlet-name>
<url-pattern>/servlet/check</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EncodingFilter</servlet-name>
<url-pattern>/servlet/EncodingFilter</url-pattern>
</servlet-mapping>

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter. Encodingfilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Java Web Experiment Five servlet filter

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.