通过所学Servlet和JSP的JSTL、EL表达式,实现用户登录、用户显示以及用户查询功能。
时间: 2024-06-02 10:14:03 浏览: 31
实现用户登录功能:
1. 创建一个登录页面(login.jsp),包含用户名和密码输入框。
2. 创建一个Servlet(LoginServlet),用于处理用户登录请求。
3. 在LoginServlet中,获取用户输入的用户名和密码,并与数据库中存储的用户信息进行比对。
4. 若用户名和密码匹配成功,则将用户信息存储在Session中,并跳转到用户显示页面(user.jsp)。
5. 若用户名和密码匹配失败,则跳转回登录页面并提示用户重新输入。
实现用户显示功能:
1. 创建一个用户显示页面(user.jsp),包含用户信息表格。
2. 在user.jsp中,使用JSTL的<c:forEach>标签遍历存储在Session中的用户信息,将其显示在表格中。
实现用户查询功能:
1. 在user.jsp页面中,添加一个查询表单,包含查询条件输入框和查询按钮。
2. 创建一个Servlet(QueryServlet),用于处理用户查询请求。
3. 在QueryServlet中,获取用户输入的查询条件,并根据条件从数据库中查询用户信息。
4. 将查询结果存储在Session中,并跳转回用户显示页面(user.jsp)。
5. 在用户显示页面中,使用JSTL的<c:choose>和<c:when>标签判断Session中是否存在查询结果,若存在则显示查询结果表格,否则显示全部用户信息表格。
相关问题
在com.entity包中创建一个名称为User的JavaBean对象,用于封装用户信息,其中User类中包含userName、age两个属性,并在该包中创建一个用于存储User对象的类MyServlet,通过JSTL表达式和EL表达式,将User对象集合数据循环显示到myjsp.jsp页面
首先,在com.entity包中创建一个User类,代码如下:
```java
package com.entity;
public class User {
private String userName;
private int age;
public User(String userName, int age) {
this.userName = userName;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
然后,在com.entity包中创建一个用于存储User对象的类MyServlet,代码如下:
```java
package com.entity;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<User> userList = new ArrayList<>();
userList.add(new User("张三", 20));
userList.add(new User("李四", 22));
userList.add(new User("王五", 25));
request.setAttribute("userList", userList);
request.getRequestDispatcher("myjsp.jsp").forward(request, response);
}
}
```
在MyServlet中,我们创建了一个List<User>类型的用户列表,然后将其存储到request域中,并将请求转发到myjsp.jsp页面。
最后,在myjsp.jsp页面中,我们可以使用JSTL表达式和EL表达式来循环显示用户列表,代码如下:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>用户名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.userName}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
在myjsp.jsp页面中,我们使用<c:forEach>标签循环显示用户列表,其中var属性表示当前循环的User对象,items属性表示要循环的用户列表。在表格中使用${user.userName}和${user.age}来显示用户的用户名和年龄。
以上就是创建User对象、存储User对象的类MyServlet,并在myjsp.jsp页面中使用JSTL表达式和EL表达式循环显示用户列表的完整代码。
模拟实现一个用户登录功能,用户登陆成功后,跳转后的页面显示用户的用户名和密码。假设用户登录的用户名为itcast,密码为123,要求使用EL表达式实现用户名和密码的显示
这里提供一个基于Java Web的用户登录示例,使用EL表达式实现用户名和密码的显示。
1. 在JSP页面中添加登录表单,并设置表单提交地址:
```html
<form action="${pageContext.request.contextPath}/login" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">登录</button>
</form>
```
2. 在Servlet中处理用户登录请求,并将用户名和密码存储在Session中:
```java
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username.equals("itcast") && password.equals("123")) {
// 登录成功,将用户名和密码存储在Session中
HttpSession session = req.getSession();
session.setAttribute("username", username);
session.setAttribute("password", password);
// 跳转到用户信息页面
resp.sendRedirect(req.getContextPath() + "/userinfo.jsp");
} else {
// 登录失败,返回登录页面
resp.sendRedirect(req.getContextPath() + "/login.jsp");
}
}
}
```
3. 在用户信息页面中,使用EL表达式获取Session中存储的用户名和密码,并进行展示:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
</head>
<body>
<h1>欢迎您,${sessionScope.username}!</h1>
<p>您的密码是:${sessionScope.password}</p>
</body>
</html>
```
在用户信息页面中,使用`${sessionScope.username}`和`${sessionScope.password}`分别获取Session中存储的用户名和密码,并进行展示。
注意:以上示例仅供参考,实际开发中需要根据具体业务需求进行修改和完善。