简单的javaweb项目
时间: 2023-12-26 15:07:11 浏览: 91
以下是一个简单的Java Web项目的实现,主要包括登录、注册、用户信息管理和商品信息管理等功能:
1. 登录页面(login.jsp):
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<form action="loginServlet" method="post">
<label>用户名:</label>
<input type="text" name="username"><br>
<label>密码:</label>
<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
2. 登录Servlet(LoginServlet.java):
```java
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名和密码是否正确
if(username.equals("admin") && password.equals("123456")) {
// 登录成功,将用户信息保存到Session中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 跳转到用户信息管理页面
response.sendRedirect("userList.jsp");
} else {
// 登录失败,返回登录页面并提示错误信息
request.setAttribute("errorMsg", "用户名或密码错误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
```
3. 用户信息管理页面(userList.jsp):
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息管理</title>
</head>
<body>
<h1>用户信息管理</h1>
<p>欢迎您,<%=session.getAttribute("username")%>!</p>
<a href="logoutServlet">退出登录</a>
<hr>
<table border="1">
<tr>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
<tr>
<td>admin</td>
<td>123456</td>
<td>
<a href="#">编辑</a>
<a href="#">删除</a>
</td>
</tr>
</table>
</body>
</html>
```
4. 退出登录Servlet(LogoutServlet.java):
```java
@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogoutServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 销毁Session并跳转到登录页面
request.getSession().invalidate();
response.sendRedirect("login.jsp");
}
}
```
阅读全文