使用jsp+servlet+jdbc完成登录和注册功能.且主页面,如果登录成功则显示登录用户的账号名,以及退出登录链接。如果未登录,则显示登录,注册链接。可以使用css进行页面样式优化
时间: 2024-06-11 20:05:35 浏览: 13
很抱歉,作为AI语言模型,我无法提供完整代码和文件的生成。以下是一些示例代码和思路供您参考。
1. 创建数据库表
首先,需要创建一个用户表,用于存储用户信息。可以使用以下SQL语句创建表:
```sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
```
2. 创建登录页面
在登录页面(login.jsp)中,需要提供一个表单,让用户输入用户名和密码。表单的提交地址为一个Servlet,用于验证用户输入是否正确。可以使用以下代码:
```jsp
<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>
```
3. 创建注册页面
在注册页面(register.jsp)中,需要提供一个表单,让用户输入用户名和密码。表单的提交地址为一个Servlet,用于将用户信息插入到数据库中。可以使用以下代码:
```jsp
<form action="RegisterServlet" method="post">
<label>用户名</label>
<input type="text" name="username">
<br>
<label>密码</label>
<input type="password" name="password">
<br>
<input type="submit" value="注册">
</form>
```
4. 创建Servlet
登录和注册功能需要分别对应两个Servlet,分别为LoginServlet和RegisterServlet。在LoginServlet中,需要获取用户输入的用户名和密码,并查询数据库中是否存在对应的用户信息。如果存在,则将用户名保存到Session中,并跳转到主页面(index.jsp);否则,返回登录页面并显示错误信息。可以使用以下代码:
```java
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 查询数据库中是否存在对应的用户信息
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
stmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
if (rs.next()) {
// 将用户名保存到Session中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 跳转到主页面
response.sendRedirect("index.jsp");
} else {
// 返回登录页面并显示错误信息
request.setAttribute("error", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
在RegisterServlet中,需要获取用户输入的用户名和密码,并将其插入到数据库中。插入完成后,跳转到登录页面。可以使用以下代码:
```java
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 插入用户信息到数据库中
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
stmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
stmt.setString(1, username);
stmt.setString(2, password);
stmt.executeUpdate();
// 跳转到登录页面
response.sendRedirect("login.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
5. 创建主页面
在主页面(index.jsp)中,需要判断用户是否已登录。如果已登录,则显示登录用户的账号名和退出登录链接;否则,显示登录和注册链接。可以使用以下代码:
```jsp
<%-- 判断用户是否已登录 --%>
<%
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if (username != null) {
%>
登录用户: <%= username %>
<a href="LogoutServlet">退出登录</a>
<%
} else {
%>
<a href="login.jsp">登录</a>
<a href="register.jsp">注册</a>
<%
}
%>
```
在LogoutServlet中,需要清除Session中保存的用户名,并跳转到登录页面。可以使用以下代码:
```java
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 清除Session中保存的用户名
HttpSession session = request.getSession();
session.removeAttribute("username");
// 跳转到登录页面
response.sendRedirect("login.jsp");
}
}
```
6. 页面样式优化
可以使用CSS对页面进行样式优化,例如:
```css
body {
font-family: Arial, sans-serif;
}
form label {
display: inline-block;
width: 80px;
}
input[type="text"],
input[type="password"] {
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
input[type="submit"] {
padding: 5px 10px;
border: none;
border-radius: 3px;
background-color: #007bff;
color: #fff;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
```
以上是使用jsp servlet jdbc完成登录和注册功能的基本思路和示例代码,具体实现需要根据项目需求进行调整。