编写程序,实现以下功能: (1)编写实体类User,有属性userName,password和role,role值为1 表示管理员,2为普通用户。 (2)编写一个用户页面userwork.jsp,显示“用户名,欢迎你来到工作页面”。 (3)编写一个管理员管理页面manage.jsp,页面显示“admin,欢迎进行管理操作” (4)编写一个login.jsp页面,输入用户名和密码。 (5)编写用户登录处理的servlet,名为loginServlet,配置的url为login.do。如果用户输入的是用户名为admin,用户角色为管理员角色。用户名不为admin,为普通用户。登录成功后进入index.jsp页面。 (6)index页面编写userwork.jsp页面和manage.jsp页面的超链接。 (7)建立一个LoginFilter过滤器,用户没有登录则进入登录页面。 (8)建立ManageFilter过滤器对manage.jsp 页面过滤,只允许管理员页面进入,不是管理员则跳转回index.jsp页面。
时间: 2024-02-26 20:56:32 浏览: 97
好的,这是一个基于Java Web的权限管理系统,需要使用Servlet和JSP技术实现。下面是代码实现:
User.java
```java
public class User {
private String userName;
private String password;
private int role; // 1 表示管理员,2 表示普通用户
public User(String userName, String password, int role) {
this.userName = userName;
this.password = password;
this.role = role;
}
// getter and setter
// ...
}
```
userwork.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Work Page</title>
</head>
<body>
<h2>${user.userName},欢迎你来到工作页面</h2>
</body>
</html>
```
manage.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Manage Page</title>
</head>
<body>
<h2>admin,欢迎进行管理操作</h2>
</body>
</html>
```
login.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form method="post" action="login.do">
<p>
<label>用户名:</label>
<input type="text" name="userName" />
</p>
<p>
<label>密码:</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" value="登录" />
</p>
</form>
</body>
</html>
```
loginServlet.java
```java
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
// 简单模拟用户登录,实际情况需要从数据库中读取用户信息进行验证
User user = null;
if ("admin".equals(userName)) {
user = new User(userName, password, 1); // 管理员用户
} else {
user = new User(userName, password, 2); // 普通用户
}
// 将用户信息存储到Session中
HttpSession session = request.getSession();
session.setAttribute("user", user);
// 跳转到index.jsp页面
response.sendRedirect("index.jsp");
}
}
```
index.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index Page</title>
</head>
<body>
<h2>Index Page</h2>
<ul>
<li><a href="userwork.jsp">用户工作页面</a></li>
<li><a href="manage.jsp">管理员管理页面</a></li>
</ul>
</body>
</html>
```
LoginFilter.java
```java
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession();
// 判断用户是否登录
User user = (User) session.getAttribute("user");
if (user == null) {
// 跳转到登录页面
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("login.jsp");
} else {
// 继续执行
chain.doFilter(request, response);
}
}
}
```
ManageFilter.java
```java
public class ManageFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession();
// 判断用户是否为管理员
User user = (User) session.getAttribute("user");
if (user != null && user.getRole() == 1) {
// 继续执行
chain.doFilter(request, response);
} else {
// 跳转到index.jsp页面
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("index.jsp");
}
}
}
```
最后,需要在web.xml中配置Filter和Servlet:
```xml
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.example.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>ManageFilter</filter-name>
<filter-class>com.example.filter.ManageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ManageFilter</filter-name>
<url-pattern>/manage.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
```
阅读全文