随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

简介:

同样需要用到EL表达式,那么就需要导入对应的jar包;

参考:https://www.cnblogs.com/0099-ymsml/p/16143473.html

通过对用户登录名的识别来判断用户属于游客、普通用户还是管理员,然后将其分配到对应的页面。

首先需要创建几个文件:

LoginServlet: 

package demoServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset:utf-8");
        
        /**
         * 获取用户名
         * 判断用户名是否是cdml
         *         是:则是管理员
         *             跳转到管理员页面
         *         不是:则是普通用户
         * 把登录的用户名称保存到session中
         * 然后转发到index.jsp
         */
        String username = req.getParameter("username");
        if("cdml".equals(username)) {    // 管理员账号
            req.getSession().setAttribute("admin", username);
            req.getRequestDispatcher("/admin/admin.jsp").forward(req, resp);
        }else if("cd".equals(username)) {    // 普通用户账号
            req.getSession().setAttribute("username", username);
            req.getRequestDispatcher("/user/user.jsp").forward(req, resp);
        }
        else {    // 游客
            req.getSession().setAttribute("username", username);
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
        System.out.println("username: " + username);
    }
}

admin.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ADMIN</title>
</head>
<body>
<h1>管理员页面</h1>
<a href="<c:url value='/index.jsp'/>">toIndex</a><br/>
<a href="<c:url value='/user/user.jsp'/>">toUser</a><br/>
<a href="<c:url value='/admin/admin.jsp'/>">toAdmin</a><br/>
</body>
</html>

user.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>USER</title>
</head>
<body>
<h1>普通用户页面</h1>
<a href="<c:url value='/index.jsp'/>">toIndex</a><br/>
<a href="<c:url value='/user/user.jsp'/>">toUser</a><br/>
</body>
</html>

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
<h1>游客页面</h1>
<a href="<c:url value='/index.jsp'/>">toIndex</a><br/>
</body>
</html>

login.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<h1 align="center">登录</h1>
<form action="LoginServlet" method="post" align="center">
用户名:<input type="text" name="username"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

启动服务器 -- 浏览器访问:

输入账号cdml -- 点击登录:

因为输入的是cdml管理员账号,所以进入的是管理员页面,并且管理员页面可以跳转其它任何页面。

输入cd -- 普通用户:

普通用户页面只能跳转到游客或其本身。

那么现在随便输入用户名:

 

身份是游客,那么就只能在游客页面,没有去其它页面的权限。

 

 

 

 

 

 

 

 

 

posted on 2022-04-14 14:13  时间完全不够用啊  阅读(164)  评论(0编辑  收藏  举报