servlet中Filter的使用
这是我自己编写JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>我的JSP页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <style type="text/css"> body{ padding:0px;margin:auto; } .form{ margin:auto;width:300px;height:auto; } h1{ color:green;text-align:center; } .color{ color:red;font-size:24px; } </style> <body> <h1>my form</h1> <div class="form"> <form action="Servlet_1" method="post"> <label class="color">${sessionScope.warning }</label> <p><label>name: </label><input type="text" name="name" required placeholder="name"> <p><label>sex: </label><input type="radio" name="sex" value="man"><label>man</label> <input type="radio" name="sex" value="woman"><label>woman</label> <p><label>comment:</label> <br><textarea rows="10" cols="30" required placeholder="comments" name="comment"></textarea> <p><input type="submit" name="submit" value="submit"> <input type="reset" name="reset" value="reset"> </form> </div> </body> </html>
代码说明:
${sessionScope.warning }是利用jstl标签获取session的值 warning是从Filter中传过来的session的key值
详细的知识可以百度,这里不多说
接下来要写的是Filter 过滤器要实现Filter接口
package com.servlet.Filter; 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; import javax.servlet.http.HttpSession; public class Myfilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; String sex = request.getParameter("sex"); if(sex == "" || sex == null){ HttpSession session = request.getSession(); session.setAttribute("warning", "sex is not allowed null !"); response.sendRedirect("index.jsp"); }else{ chain.doFilter(req, res); } } @Override public void init(FilterConfig arg0) throws ServletException { } }
当sex选中时,request.getParameter("sex") != null ;这样就可以执行chain.doFilter(req,res);接下来如果还有其他过滤器的话就执行其他过滤器,如果没有就从服务器上获取相关资源,我们这儿没有过滤器了,接下来我们编写一个servlet,由servlet获取数据
package com.servlet.main; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet_1 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet_1() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String comment = request.getParameter("comment"); out.print("<html><head><title></title></head><body>"); out.print("name:<br>"+name+"<P>sex:<br>"+sex+"<P>comment:<br>"+comment); out.print("</body></html>"); } public void init() throws ServletException { } }
servlet的相关问题就不多叙述了
最后我们看看运行结果:
sex不选:
sex选择man: