login.html Login
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Fiter processing Import Java.io.ioexception;import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;//authentication of sensitive catalogs [classroom practice 1]public class FilterDemo7 implements Filter {public void init (Filterconfig filterconfig) throws servletexception {} public void DoFilter (ServletRequest req, servletresponse Res , Filterchain chain) throws IOException, servletexception {httpservletrequest request = (httpservletrequest) req; HttpServletResponse response = (httpservletresponse) res; Set the request Body encoding method request.setcharacterencoding ("UTF-8"); Get user request parameter String username = request.getparameter ("username"); String Password = request.getparameter ("password"); String role = Request.getparameter ("role"); Sentence if (username!=null && password!=null && role!=null && username.trim (). Length () >0 && Password.trim () . Length () >0 && Role.trim (). Length () >0) {if ("Normal user". Equals (role)) {Request.setattrib Ute ("message", "Welcome to ordinary user <font color= ' blue ' >" +username+ "</font> login"); Request.setattribute ("Flag", "user"); }else if ("Administrator". Equals (role)) {Request.setattribute ("message", "Welcome Administrator <font color= ' red ' >" +username+ "< /FONT> Login "); Request.setattribute ("Flag", "admin"); } chain.dofilter (Request,response); }} public void Destroy () {}}
Results show<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Xml <filter> <filter-name>FilterDemo7</filter-name> <filter-class>cn.itcast.web.filter.FilterDemo7</filter-class> </filter> <filter-mapping> <filter-name>FilterDemo7</filter-name> <url-pattern>/welcome.jsp</url-pattern> </filter-mapping>
filter--a URL-level role certification for a directory---learning notes