请按题目的要求建立相应的Java Web项目源文件,截取程序及其运行结果的屏幕界面粘贴在文档中。请编写登录页面login.sp,向index,jsp提交用户名、密码等信息,在index,jsp中获取用户登录信息并在本页面显示(使用EL表达式)。
时间: 2024-02-26 17:54:17 浏览: 23
首先,创建一个Java Web项目,然后在 `src/main/java` 目录下创建 `LoginServlet.java` 文件,代码如下:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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");
// 将用户名和密码存储在request对象中
request.setAttribute("username", username);
request.setAttribute("password", password);
// 转发到index.jsp页面
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
```
然后,在 `src/main/webapp` 目录下创建 `login.jsp` 文件,代码如下:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form method="post" action="login">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
最后,在 `src/main/webapp` 目录下创建 `index.jsp` 文件,代码如下:
```jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<%-- 使用EL表达式获取用户名和密码 --%>
<h1>Welcome <%= request.getAttribute("username") %>!</h1>
<p>Your password is <%= request.getAttribute("password") %>.</p>
</body>
</html>
```
运行项目并访问 `http://localhost:8080/login.jsp` 页面,输入用户名和密码,点击登录按钮,将会跳转到 `index.jsp` 页面,并显示登录信息。