zoukankan      html  css  js  c++  java
  • JSP网站开发基础总结《十二》

      前两篇已经简单为大家介绍了一下,有关Filter接口的知识,本篇就让我们以一个登录小功能,来具体实现一下过滤器的作用,便于大家掌握。具体为大家介绍一下如何使用Filter对访问进行过滤,及如何防止中文乱码的问题,内容不多,大家只要简单一练习便可以掌握。

     1、登录表单:

      和一般的表单没有任何区别,大家可以新建一个Login.jsp作为登录界面,在其中添加一个表单即可。

    <body>
        <center>
            <form method="post" action="<%=request.getContextPath() %>/servlet/login" enctype="application/x-www-form-urlencoded">
                姓名:<input type="text" name="name">
                密码:<input type="password" name="pwd">
                <input type="submit" value="登录">
                <input type="reset" value="重置">  
            </form>
        </center>
      </body>

     2、select:

      既然是登录,就一定少不了我们select类,因为我们仅仅讨论过滤器的作用,在这里就不再连接数据库了,大家如有需要可以参看我的前几篇博客,对于数据库连接,有详细的讲解。我们的select代码:

    public class login extends HttpServlet {
    
        /**
         * Constructor of the object.
         */
        public login() {
            super();
        }
    
        /**
         * Destruction of the servlet. <br>
         */
        public void destroy() {
            super.destroy(); // Just puts "destroy" string in log
            System.out.println("销毁select");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");//防止中文乱码
            response.setCharacterEncoding("utf-8");//防止中文乱码
            String username = request.getParameter("name");//获得表单中用户的填写的姓名
            String pwd = request.getParameter("pwd");//获得表单中用户输入的密码
            if("小米".equals(username)&&"admin".equals(pwd)){//身份判断
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                response.sendRedirect(request.getContextPath()+"/success.jsp");
            }else{
                response.sendRedirect(request.getContextPath()+"/error.jsp");
            }
        }
    
        /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
        public void init() throws ServletException {
            System.out.println("初始化select");
        }
    
    }

     2、success.jsp与error.jsp:

      为了达到极佳的用户体验,我们这里新建两个jsp界面,用于给用户反馈登录成功与否提醒。

     3、创建Filter类:

    public class firstFilter implements Filter {
    
        FilterConfig config ;
        
        public void destroy() {
    
        }
    
        public void doFilter(ServletRequest Request, ServletResponse Response,
                FilterChain arg2) throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest)Request;
            
            HttpServletResponse res = (HttpServletResponse)Response;
            
            //防止中文乱码
            req.setCharacterEncoding("utf-8");
            res.setCharacterEncoding("utf-8");
            
            //Filter设置字符集
            //req.setCharacterEncoding(config.getInitParameter("lanager"));
            //res.setCharacterEncoding(config.getInitParameter("lanager"));
            
            HttpSession session = req.getSession();
            String username = (String) session.getAttribute("username");
            
            //System.out.println(new String(username.getBytes("ISO-8859-1"),"utf-8"));//防止中文乱码
            
            System.out.println(username);
            
    //        if(req.getRequestURI().indexOf("index.jsp")!=-1 || req.getRequestURI().indexOf("servlet/login")!=-1){
    //            arg2.doFilter(Request, Response); 
    //            return ;
    //        }
            
            String noLoginPath = config.getInitParameter("noLoginPath");//获得我们设置的默认值
            if(noLoginPath!=null){
                String [] NoPath = noLoginPath.split(";");
                for(int i=0; i<NoPath.length; i++ ){
                    if(req.getRequestURI().indexOf(NoPath[i])!=-1){
                        arg2.doFilter(Request, Response); 
                        return ;
                    }
                }
            }
            
            if(username!=null){
                arg2.doFilter(Request, Response);
                //res.sendRedirect(req.getContextPath()+"/success.jsp");
            }else{
                res.sendRedirect(req.getContextPath()+"/index.jsp");
            }
        }
    
        public void init(FilterConfig arg0) throws ServletException {
            config = arg0;
        }
    
    }

      这里我们第一次使用了init()方法中的FilterConfig对象,该方法具体的作用是什么呢?我们在web.xml中配置我们的Filter时,设置的默认字段,可以通过其来获得。

     4、web.xml配置:

      这里我们第一次设置默认字段。

      

     5、运行测试:

      启动我们的项目,在地址栏输入工程名:http://localhost:8080/Test/index.jsp,进入登录界面,当我们登录成功后,复制一下地址栏的地址,然后打开一个新的浏览器,在地址栏直接访问刚刚拷贝的地址,我就会发现我们又回到了登录界面,这就表示我们的过滤器起作用了。当然我们的过滤器的功能远不止这些,剩下的就要靠到家自己摸索了。

     6、中文问题:

      在上面的代码中已经为大家介绍了一下有关防止中文乱码的问题,大家可以自行了解。

      到今天我们关于JSP基础知识的总结就真正的告一段落了,如果以上十二篇博客你均已学会,我保证做一个简单的动态网站不是什么问题,当然想做好一个网站,只有这些是远远不够的,大家还需要学习div+css以及JavaScript的知识,所谓师傅领进门,修行靠个人,祝大家在程序猿这条路上愈来愈好。

  • 相关阅读:
    Educational Codeforces Round 69 D E
    洛谷P2325 [SCOI2005]王室联邦
    洛谷P1494 [国家集训队]小Z的袜子
    洛谷P4514 上帝造题的七分钟
    POJ3321Apple Tree
    满血复活前的记录(持续更新ing)
    各位2019见
    LuoguP3338 [ZJOI2014]力
    Codeforces Round #478 Div2 975A 975B 975C 975D
    Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/4355318.html
Copyright © 2011-2022 走看看