JSP filter encoding character encoding filter

Source: Internet
Author: User

First, create two JSP pages: A.jsp and b.jsp.

1, a.jsp

1 <!--Landing Form -2 <formAction= "Checkloginservlet.do"Method= "POST">3     <inputtype= "text"name= "username">4     <inputtype= "Password"name= "Password" />5     <inputtype= "Submit"value= "Submit">6 </form>

2, b.jsp

1 <!--  --2 ${requestscope.username}    Welcome ...

Second, create the Servlet:CheckLoginServlet.js for the login judgment

1 Importjava.io.IOException;2 Importjavax.servlet.ServletException;3 ImportJavax.servlet.annotation.WebServlet;4 ImportJavax.servlet.http.HttpServlet;5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletResponse;7 8@WebServlet ("/checkloginservlet.do")9  Public classCheckloginservletextendsHttpServlet {Ten     Private Static Final LongSerialversionuid = 1L; One      A     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -         /** - * Login Verification: If the user name and password are the same, jump to the b.jsp page. Otherwise jump back to the a.jsp page and log back in.  the          */ -String name = Request.getparameter ("username"); -String Password = request.getparameter ("Password"); -         if(name.equals (password) && Name.trim ()! = "" && password.trim ()! = ""){ +Request.setattribute ("username", name); -Request.getrequestdispatcher ("b.jsp"). Forward (request, response); +             return; A}Else{ atResponse.sendredirect ("a.jsp"); -         } -     } -}

Third, configure the initialization parameter encoding in the Web. xml file with a value of utf-8.

< Context-param >    < Param-name >encoding</param-name>    <Param-value >utf-8</param-value></Context-param  >

Four, write character encoding filter: Encodingfilter.java, filter all the files in the root directory.

1 Importjava.io.IOException;2 ImportJavax.servlet.Filter;3 ImportJavax.servlet.FilterChain;4 ImportJavax.servlet.FilterConfig;5 Importjavax.servlet.ServletException;6 Importjavax.servlet.ServletRequest;7 ImportJavax.servlet.ServletResponse;8 ImportJavax.servlet.annotation.WebFilter;9 Ten@WebFilter ("/*") One  Public classEncodingfilterImplementsFilter { A      -     PrivateString encoding; -      the      PublicEncodingfilter () {} -  -      Public voidInit (Filterconfig fconfig)throwsservletexception { -encoding = Fconfig.getservletcontext (). Getinitparameter ("Encoding"); +     } -  +      Public voidDoFilter (ServletRequest request, servletresponse Response,filterchain chain)throwsIOException, servletexception { A request.setcharacterencoding (encoding); at Chain.dofilter (request, response); -     } -  -      Public voiddestroy () {} -  -}

V. Finish ...

JSP filter encoding character encoding 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.