当请求来的时候,首先经过拦截器,在经过一系列拦截器处理后,再由再根据URL找到Servlet.执行servlet中的代码.
首先建一个dynamic web project
然后在WebContent或webroot文件夹下面的WEB-INF文件夹下面再建两个文件夹classes和lib.
classes文件夹用来存放加载的字节码文件,lib文件夹用于存放jar包
接下来在WEB-INF目录下新建一个web.xml文件,用于配置拦截器信息和servlet.
然后处理请求页面,可以建一个文件夹专门存放.jsp文件或者.html文件
下面看登录信息字符串解码的程序:
建一个Filter的类,该类实现了Filter接口:
1 package test.filter; 2 import java.io.IOException; 3 import javax.servlet.Filter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.FilterConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 public class CharFilter implements Filter { 10 /** 11 * 加载配置的方法 12 */ 13 public void init(FilterConfig config) throws ServletException { 14 System.out.println("CharFilter.init()"); 15 } 16 /** 17 * 执行拦截的方法 18 */ 19 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 20 throws IOException, ServletException { 21 // 将请求到的数据转化为UTF-8 将服务器输入到jsp的汉字进行转码 22 response.setContentType("text/html; charset=utf-8"); 23 // 将form表单中的内容提交给服务器时进行解码 24 request.setCharacterEncoding("utf-8"); 25 // 将拦截后处理过的内容转发给下一个filter 26 chain.doFilter(request, response); 27 } 28 /** 29 * 销毁拦截器 30 */ 31 public void destroy() { 32 } 33 }
下来在进行servlet中处理业务逻辑:
代码如下:
package test.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/hello") public class CharServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String parameter = request.getParameter("username");//获取name参数 String pas = request.getParameter("pas");//获取password参数 System.out.println(parameter+"密码:"+pas); if("huike".equals(parameter)&&"111".equals(pas)){ request.getRequestDispatcher("/JSP/login.jsp").forward(request, response); }else{ PrintWriter writer = response.getWriter(); writer.write("账户或密码错误"); writer.close(); } } }
登录界面的.jsp文件
<%@ page import="java.util.UUID" %> <!-- 导入UUID的jar包 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>hello world!!你好</h1> <form action="/Charset/hello" method="POST"> 账户:<input type="text" name = "username" placeholder ="hello world" required/><br/><br/> 密码:<input type="password" name = "pas" /><br/><br/> <input type="submit" /> </form> </body> </html>
跳转页面的.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>login</title> </head> <body> <h1>login</h1> ${name} <!-- 通过servlet中设置的键获取name的值 ,进行参数传递--> <a>你好</a> </body> </html>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Charset</display-name> <filter> <filter-name>CharFilter</filter-name> <filter-class>test.filter.CharFilter</filter-class> //filter类的权限定名称 </filter> <filter-mapping> <filter-name>CharFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
访问URL:http://localhost:8080/工程名/放.jsp文件的文件夹名/XX.jsp,另:在WEB-INF下面的jsp文件不能直接访问到
时间: 2024-10-04 15:31:57