1:先写登录的jsp文件 ,文件名叫做index.jsp
<%--
Created by IntelliJ IDEA.
User: Acer
Date: 2022/9/28
Time: 21:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<a href="Register.jsp">注册</a>
<form action="LogoServet" method="post">
账号:<input type="text" name="id" ><br>
密码: <input type="password" name="pwd"><br>
<input type="submit" value="登录"><input type="reset" value="重新输入">
</form>
</body>
</html>
2.再写注册的jsp文件,文件名叫Register.jsp
<%--
Created by IntelliJ IDEA.
User: Acer
Date: 2022/9/28
Time: 21:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="RegisterServet" method="post">
用户名:<input type="text" name="name" ><br>
密  码: <input type="password" name="pwd"><br>
确认密码: <input type="password" name="pwd2"><br>
<input type="submit" value="注册"><input type="reset" value="重新输入">
</form>
</body>
</html>
3:再写转跳3秒钟,文件名叫jump.jsp
<%--
Created by IntelliJ IDEA.
User: Acer
Date: 2022/9/24
Time: 17:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>跳转页面</title>
<script type="text/javascript">
function fun1(){
var span1 = document.getElementById("span1");
var count = span1.innerText;
if (count>0){
count--;
span1.innerText=count;
}
}
window.onload= function(){
window.setInterval("fun1()",1000);
}
</script>
</head>
<body>
登录成功.<span id="span1" style="color:red">3</span>秒钟自动跳转.如果没有跳转,请<a href="main.jsp">点击</a>
<%
response.setHeader("refresh","3;url=main.jsp");
%>
</body>
</html>
4:main的页面可以是你的网页主页,我只写了个登录成功
5在项目中的src,新建一个叫com的软件包,然后再里面新建一个controller的软件包
6.在controller中写登录的Select,文件名叫做LogoServet
package com.controller;
import com.db.Database;
import com.model.Userdata;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet("/LogoServet")
public class LogoServet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id=Integer.parseInt(request.getParameter("id"));
String pwd=request.getParameter("pwd");
PrintWriter out=response.getWriter();
//out.println("你的账号:"+id+"你的密码:"+pwd);
try {
Database database = new Database();
Userdata userdata = database.login(id,pwd);
if (userdata == null) {
out.println("该用户不存在或者密码错误"+"<a href=\"index.jsp\"> 返回登录</a>");
}else {
out.println("欢迎登录"+userdata.getName()+"点击转跳-->"+"<a href=\"jump.jsp\"> 进入网页</a>");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
out.println("注册失败");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
7:在controller中写注册的Servlet,文件名叫RegisterServet
package com.controller;
import com.db.Database;
import com.model.Userdata;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet("/LogoServet")
public class LogoServet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id=Integer.parseInt(request.getParameter("id"));
String pwd=request.getParameter("pwd");
PrintWriter out=response.getWriter();
//out.println("你的账号:"+id+"你的密码:"+pwd);
try {
Database database = new Database();
Userdata userdata = database.login(id,pwd);
if (userdata == null) {
out.println("该用户不存在或者密码错误"+"<a href=\"index.jsp\"> 返回登录</a>");
}else {
out.println("欢迎登录"+userdata.getName()+"点击转跳-->"+"<a href=\"jump.jsp\"> 进入网页</a>");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
out.println("注册失败");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
8:在com的软件包下新建一个加db的软件包,在包里新建一个叫Database文件,这个文件有连接数据库和查询数据和添加数据,我自己的数据库名叫web001,记住一定要用MySQL的jar包
package com.db;
import com.model.Userdata;
import java.sql.*;
import java.util.ArrayList;
public class Database {
Connection conn=null;
public Database() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
try {
this.conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/web001?serverTimeZone=GMT%288","root","123456");
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println(conn);
}
public void showConnect(){
if (conn==null){
System.out.println("数据库连接失败");
}else {
System.out.println("数据库连接成功");
}
}
//关闭链接
public void close() throws SQLException {
conn.close();
}
//在login表里添加数据
public void inser(String name,String pwd) throws SQLException {
PreparedStatement prep = conn.prepareStatement("insert into web001.login value(null,?,?) ");
prep.setString(1,name);
prep.setString(2,pwd);
prep.execute();
}
public ArrayList<Userdata> getAllUser() throws SQLException {
ArrayList<Userdata> uselist = new ArrayList<>();
PreparedStatement prep = conn.prepareStatement("select * from web001.login");
prep.execute();
ResultSet resultSet=prep.executeQuery();
while (resultSet.next()){
int myid=resultSet.getInt("id");
String myname=resultSet.getString("name");
String mypwd=resultSet.getString("pwd");
uselist.add(new Userdata(myid,myname,mypwd));
}
return uselist;
}
//在表里查询数据
public Userdata getUser(int id) throws SQLException {
PreparedStatement prep = conn.prepareStatement("select * from web001.login where id=?");
prep.setInt(1,id);
prep.execute();
ResultSet resultSet=prep.executeQuery();
if (resultSet.next()){
int myid=resultSet.getInt("id");
String myname=resultSet.getString("Name");
String mypwd=resultSet.getString("passwrod");
return new Userdata(myid,myname,mypwd);
}
return null;
}
public Userdata login(int id,String pwd) throws SQLException {
PreparedStatement prep = conn.prepareStatement("select web001.login.passwrod from web001.login where id=? ");
prep.setInt(1,id);
prep.execute();
ResultSet resultSet = prep.executeQuery();
if (resultSet.next()) {
String mypwd=resultSet.getString("passwrod");
if (pwd.equals(mypwd)) {
return getUser(id);
}else {
return null;
}
}else {
return null;
}
}
}
9:新建一个叫做web001的数据库
10.在web001的数据库中新建一个叫做login的表内容如下
然后一个简单的登录注册转跳主页就做好了