<% - Web site: <A href = "http://www.crazyit.org"> crazy league Java </a> author yeeku.H.lee kongyeeku @ 163 .com which version of the 1.0 for Amsterdam Copyright (the C), 20012016 , yeekuHLee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <Title> Filter Test </ title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <H2> Filter page </ h2> </body> </html>
<% - Web site: <A href = "http://www.crazyit.org"> crazy league Java </a> author yeeku.H.lee kongyeeku @ 163 .com which version of the 1.0 for Amsterdam Copyright (the C), 20012016 , yeekuHLee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <Title> login page </ title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <H2> login page </ h2> <% if(request.getAttribute("tip") != null) { out.println("<font color='red'>" + request.getAttribute("tip") + "</font>"); } %> <form method="post" action="proLogin.jsp"> 用户名:<input type="text" name="name"/><br/> <input type="submit" value="登录"/> </form> </body> </html>
<% - Web site: <A href = "http://www.crazyit.org"> crazy league Java </a> author yeeku.H.lee kongyeeku @ 163 .com which version of the 1.0 for Amsterdam Copyright (the C), 20012016 , yeekuHLee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <Title> login page </ title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <H2> login page </ h2> <% session.setAttribute("user" , request.getParameter("name")); %> Successful login, you can access other pages of the application </body> </html>
<?xml version="1.0" encoding="GBK"?> <-! Project root element definition file is generated, the default target is empty -> <project name="web" basedir="." default=""> <! - definition of three simple attributes -> <property name="src" value="src"/> <property name="classes" value="classes"/> <! - Defines a set of files and directories -> <path id="classpath"> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement path="${classes}"/> </path> <! - defined compile target, used to compile Java source files -> <Target name = "compile" description = "compile Java source files"> <! - delete classes properties represent folder -> <delete dir="${classes}"/> <! - create classes properties represent folder -> <mkdir dir="${classes}"/> <copy todir="${classes}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <! - compiled Java files, class files compiled into classes properties represent folder -> <javac destdir="${classes}" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <! - Specify the location to compile the Java files are located -> <src path="${src}"/> <! - Specifies the location of files compiled Java class libraries to third parties where required -> <classpath refid="classpath"/> </javac> </target> </project>
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="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"> <! - defined Filter -> <filter> <-! The Filter name is equivalent to specifying @WebFilter The filterName Properties -> <filter-name>log</filter-name> <-! Filter implementation class -> <filter-class>lee.LogFilter</filter-class> </filter> <-! URL address defined Filter blocked -> <filter-mapping> <-! Filter name -> <filter-name>log</filter-name> <-! The Filter responsible for intercepting the URL, is equivalent to specifying @WebFilter The urlPatterns Properties -> <url-pattern>/*</url-pattern> </filter-mapping> <! - defined Filter -> <filter> <-! Filter name -> <filter-name>authority</filter-name> <-! Filter implementation class -> <filter-class>lee.AuthorityFilter</filter-class> <! - the following three init-param element arranged three parameters -> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>loginPage</param-name> <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>proLogin</param-name> <param-value>/proLogin.jsp</param-value> </init-param> </filter> <-! URL address defined Filter blocked -> <filter-mapping> <-! Filter name -> <filter-name>authority</filter-name> <-! Filter responsible for intercepting the URL -> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package lee; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io. * ; / ** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ @WebFilter(filterName="authority" , urlPatterns={"/*"} , initParams={ @WebInitParam(name="encoding", value="GBK"), @WebInitParam(name="loginPage", value="/login.jsp"), @WebInitParam(name="proLogin", value="/proLogin.jsp")}) public class AuthorityFilter implements Filter { // the FilterConfig Filter can be used to access the configuration information Private the FilterConfig config; // implemented method of initialization public void the init (the FilterConfig config) { this.config = config; } // achieve destruction methods public void the destroy () { this.config = null; } // execution core filtration public void doFilter (ServletRequest Request, ServletResponse response, FilterChain chain) throws IOException,ServletException { // Get the configuration parameters of Filter String encoding = config.getInitParameter ( "encoding" ); String loginPage = config.getInitParameter("loginPage"); ProLogin String = config.getInitParameter ( "proLogin" ); // set the character set used for encoding request Request.setCharacterEncoding (encoding); // ① the HttpServletRequest requ = (the HttpServletRequest) request; Session the HttpSession = requ.getSession ( to true ); // get the customer requested page String requestPath = requ.getServletPath (); // If the session-wide user is null, signifies not logged // and neither requested by the user login page nor is the processing of the page to log IF (session.getAttribute ( "the User") == null &&! requestPath.endsWith (the LoginPage) &&! requestPath.endsWith (proLogin)) { // Forward to the login page request.setAttribute ( "tip", "You are not logged in." ); request.getRequestDispatcher(loginPage) .forward(request, response); } // "release" request else { chain.doFilter(request, response); } } }
package lee; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io. * ; /** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ @WebFilter(filterName="log" ,urlPatterns={"/*"}) public class LogFilter implements Filter { // the FilterConfig Filter can be used to access the configuration information Private the FilterConfig config; // implemented method of initialization public void the init (the FilterConfig config) { this.config = config; } // achieve destruction methods public void the destroy () { this.config = null; } // execution core filtration public void doFilter (ServletRequest Request, ServletResponse response, FilterChain chain) throws IOException,ServletException { // --------- The following code is used to perform pre-processing user requests --------- // Get ServletContext object, for logging ServletContext context = the this .config.getServletContext () ; Long before = System.currentTimeMillis (); System.out.println ( "Start Filter ..." ); // the request into the request HttpServletRequest HttpServletRequest hrequest = (HttpServletRequest) Request; // output message System.out.println ( "the Filter has intercepted the request of the user address: "+ hrequest.getServletPath()); // the Filter just chaining, to the destination address release request is still the chain.doFilter (Request, Response); // --------- following code to the server response process after execution ------ --- Long the After = System.currentTimeMillis (); // output message System.out.println ( "filter end" ); // output message System.out.println ( "request is positioned to the" + hrequest.getRequestURI () + "The time spent is:" + (the After - the before)); } }