使用javaweb写一个新闻发布系统的登录注册页面,要求不使用xml文件,要求使用数据库储存用户信息,要求在用户点击登录后页面跳转至新闻发布系统主页面。
时间: 2023-07-16 19:14:33 浏览: 99
超级简单javaweb项目SSM用户管理登录注册查询添加删除修改操作.rar(含源码)
5星 · 资源好评率100%
好的,下面是实现步骤:
1.创建一个数据库news,其中建立一个表user,表中包括id、username、password三个字段。
2.编写登录页面login.jsp,包括用户名和密码输入框以及登录按钮。代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="login" method="post">
<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>
```
3.编写注册页面register.jsp,包括用户名、密码和确认密码输入框以及注册按钮。代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register Page</title>
</head>
<body>
<h2>Register Page</h2>
<form action="register" method="post">
<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>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
```
4.编写LoginServlet类,处理登录操作。在doPost方法中,获取用户输入的用户名和密码,并在数据库中查找是否有该用户,如果有则跳转至主页面index.jsp,否则返回登录页面。代码如下:
```java
package com.example.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
@Override
public void init() throws ServletException {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
pstmt = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5.编写RegisterServlet类,处理注册操作。在doPost方法中,获取用户输入的用户名和密码,并在数据库中插入一条新的记录。代码如下:
```java
package com.example.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
private PreparedStatement pstmt;
@Override
public void init() throws ServletException {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
pstmt = conn.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
response.sendRedirect("login.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
6.编写index.jsp页面,作为主页面。代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index Page</title>
</head>
<body>
<h2>Index Page</h2>
<p>Welcome to the News Publishing System!</p>
</body>
</html>
```
7.部署到Tomcat服务器中,启动Tomcat服务器,在浏览器中输入http://localhost:8080/login.jsp,即可进入登录页面。
8.在登录页面中输入正确的用户名和密码,即可跳转至主页面。在注册页面中输入用户名和密码并确认密码后,即可注册新用户,并跳转至登录页面。
阅读全文