随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。
posts - 398,comments - 0,views - 13万

简介:

同样需要用到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   时间完全不够用啊  阅读(181)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示