美文网首页
Web 基础20 Filter 入门案例一 ~ 统一全站的编码

Web 基础20 Filter 入门案例一 ~ 统一全站的编码

作者: 小熊先生很不开心 | 来源:发表于2018-04-05 10:11 被阅读10次

    1.1 首先使用普通方法进行

    1.1.1 顺便说一下案例环境

      这里我直接使用的是上次Session登录注销的案例用jsp改写的所以我这就直接放代码了不进行说明了。。。对不起

    • 数据准备
    
    package com.itbear.domain;
    
    public class User {
        private String name;
        private String password;
        public User() {
            super();
            // TODO Auto-generated constructor stub
        }
        public User(String name, String password) {
            super();
            this.name = name;
            this.password = password;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        @Override
        public String toString() {
            return "User [name=" + name + ", password=" + password + "]";
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            result = prime * result
                    + ((password == null) ? 0 : password.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            User other = (User) obj;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            if (password == null) {
                if (other.password != null)
                    return false;
            } else if (!password.equals(other.password))
                return false;
            return true;
        }
        
        
    }
    
    package com.itbear.domain;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class UseDB {
        private static List<User> users = new ArrayList<User>();
        
        static {
            users.add(new User("itcast","123456"));
            users.add(new User("itheima","123456"));
            users.add(new User("zhangsan","123456"));
        }
        
        public static List<User> getUsers(){
            return users;
        }
        
    }
    
    • 首页
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>这里是小熊先生的个人主页</h1>
        <c:choose>
            <c:when test="${user == null }">
                <h4>先完成登录</h4>
                <a href="${pageContext.request.contextPath }/login.jsp">登录</a>
            </c:when>
            <c:otherwise>
                <h4>登录完成</h4>
                <a href="${pageContext.request.contextPath }/Loginout">登出</a>
            </c:otherwise>
        </c:choose>
        
        <a href="${pageContext.request.contextPath}/importance/importance.html">很重要的资源</a>
    </body>
    </html>
    
    
    • 登录页面
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
            用户名<input type="text" name="name" >
            密码<input type="password" name="password">
            <input type="submit" value="提交">
        </form>
    </body>
    </html>
    
    • 登录Servlet
    
    package com.itbear.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import com.itbear.domain.UseDB;
    import com.itbear.domain.User;
    
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        
        public LoginServlet() {
            super();
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name = request.getParameter("name");
            String pwd = request.getParameter("password");
            PrintWriter out = response.getWriter();
            
            User user = new User(name, pwd);
            
            List<User> users = UseDB.getUsers();
            boolean flag = users.contains(user);
            if(flag){
                out.println("登录成功,3秒后跳转");
                response.setHeader("Refresh", "3;URL=index.jsp");
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
            }else{
                
                out.println("登录失败,3秒后跳转");
                response.setHeader("Refresh", "3;URL=login.jsp");
            }
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
            
        }
    
    }
    
    
    • 注销Servlet
    
    package com.itbear.domain;
    
    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;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class LoginOut
     */
    public class LoginOut extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        public LoginOut() {
            super();
           
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            PrintWriter out = response.getWriter();
            
            HttpSession session = request.getSession();
            session.invalidate();
            out.println("注销完成");
            response.setHeader("Refresh", "3;URL=/mySession/index.jsp");
            
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Web 基础20 Filter 入门案例一 ~ 统一全站的编码

          本文链接:https://www.haomeiwen.com/subject/pzyrhftx.html