JSP实现简单的登录页面实现及代码
时间: 2024-05-15 20:19:35 浏览: 20
以下是一个简单的JSP登录页面的实现及代码:
1. 编写login.jsp页面
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
2. 编写login.do页面,用于处理登录请求
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "123456".equals(password)) {
// 登录成功,跳转到首页
response.sendRedirect("index.jsp");
} else {
// 登录失败,返回登录页面
response.sendRedirect("login.jsp");
}
%>
```
在上面的代码中,使用了request.getParameter方法获取用户名和密码,然后判断是否正确,如果正确则跳转到首页,否则返回登录页面。注意,这里使用了response.sendRedirect方法来实现页面跳转。