1. Encoding Conversion
Public void DoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception { // TODO auto-generated method stub // Place Your code here if (! charencoding.equals (request.getcharacterencoding ())) { request.setcharacterencoding (charencoding); } Response.setcharacterencoding (charencoding); // Pass the request along the filter chain Chain.dofilter (request, response); }
2. Permission Check Filter
Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception { //TODO auto-generated Method Stub//Place your code hereHttpServletRequest req=(httpservletrequest) request; HttpServletResponse Res=(httpservletresponse) response; String Servletpath=Req.getservletpath (); System. out. println ("Servletpath"+Servletpath); HttpSession Session=req.getsession (); String Flag= (String) Session.getattribute ("Flag"); if(Servletpath! =NULL&&(Servletpath.equals ("/21/login.jsp") ||(Servletpath.equals ("/21/index.jsp")) ||//servletpath.equals ("/21/hello.jsp") | |(Servletpath.equals ("/21/loginserverlet21"))) {chain.dofilter (request, response); }Else { if(Flag! =NULL&& Flag.equals ("login_success") {chain.dofilter (request, response); } Else if(Flag! =NULL&& Flag.equals ("Login_error") {Req.setattribute ("msg","Login failed, please re-login!!!! <br/>"); Req.setattribute ("Return_uri", Servletpath); RequestDispatcher Rd= Req.getrequestdispatcher ("/21/login.jsp"); Rd.forward (req, res); } Else{Req.setattribute ("msg","you haven't landed yet ."); Req.setattribute ("Return_uri", Servletpath); RequestDispatcher Rd= Req.getrequestdispatcher ("/21/login.jsp"); Rd.forward (req, res); } }
3. Source code
Https://git.oschina.net/zkzk945/JavaEE.git
Servlet Filter Filters