用jsp内置对象写一个用户登录注册网页
时间: 2023-10-06 20:05:56 浏览: 50
以下是使用JSP内置对象制作用户登录注册网页的示例:
index.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>
<h1>用户登录</h1>
<form action="login.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
<p>还没有账号?<a href="register.jsp">点击注册</a></p>
</body>
</html>
```
login.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)) {
session.setAttribute("username", username);
response.sendRedirect("home.jsp");
} else {
out.print("<script>alert('用户名或密码错误!');history.back();</script>");
}
%>
```
register.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>
<h1>用户注册</h1>
<form action="register.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="confirmPassword"><br>
<input type="submit" value="注册">
</form>
<p>已有账号?<a href="index.jsp">点击登录</a></p>
</body>
</html>
```
register.jsp(注册处理页面)
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmPassword = request.getParameter("confirmPassword");
if (!password.equals(confirmPassword)) {
out.print("<script>alert('两次输入的密码不一致!');history.back();</script>");
} else {
session.setAttribute("username", username);
response.sendRedirect("home.jsp");
}
%>
```
home.jsp(登录成功后的主页)
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = (String) session.getAttribute("username");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎访问</title>
</head>
<body>
<h1>欢迎访问,<%= username %>!</h1>
<p><a href="logout.jsp">退出登录</a></p>
</body>
</html>
```
logout.jsp(退出登录页面)
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>退出登录</title>
</head>
<body>
<h1>已成功退出登录!</h1>
<p><a href="index.jsp">返回登录页</a></p>
</body>
</html>
```