登陆页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>提交页面</title>
</head>
<body>
<form action="Login/DBcheck" method="post">
登录名:<input type="text" name="username"/>
密 码:<input type="password" name="passWd"/><br>
<input type="submit" value="登录"/>
<a href="Register.jsp">注册</a>
</form>
</body>
</html>
注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
注册页面,欢迎注册<p/>
<form action="LoginUp/Register" method="post">
用户名<input type="text" name="username"/><br/>
密--码<input type="password" name="passwd"/><br/>
<input type="submit" value="完成"/>
</form>
</body>
</html>
登陆逻辑处理
package Login;
/**
*
*基于JSP+servlet+JDBC实现登录验证
*/
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
public class DBcheck extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("username");
String passwd = req.getParameter("passWd");
RequestDispatcher dis =null;
System.out.println(name+passwd);
try {
try {
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection comm = DriverManager.getConnection(url);
String SQL = "SELECT * FROM [user] where(username=? and password =?)";//注意:表名一定要带[]
//结果集
PreparedStatement pstmt = comm.prepareStatement(SQL);
pstmt.setString(1,name);
pstmt.setString(2,passwd);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(comm!=null){
comm.close();
}
// dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
// dis.forward(req,resp);
resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
System.out.println("hah");
}else{
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(comm!=null){
comm.close();
}
// dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");
// dis.forward(req,resp);
resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/error.jsp");
System.out.println("ggggggggg");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注册逻辑处理
package LoginUp;
/**
* 注册逻辑,通过拿到JSP页面传回来的登录名和密码,然后写入数据库中,写入成功则跳转回登陆主页
* 于JSP+servlet+JDBC实现注册功能
*/
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Register extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置服务器端以UTF-8编码进行输出
resp.setCharacterEncoding("UTF-8");
//设置浏览器以UTF-8编码进行接收,解决中文乱码问题
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String username = req.getParameter("username");
String passwd = req.getParameter("passwd");
System.out.println("zheli");
try {
try {
System.out.println("here");
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection comm = DriverManager.getConnection(url);
String sql = "INSERT into [user] (username,password) VALUES ('"+username+"','"+passwd+"')";
System.out.println("a");
Statement st = comm.createStatement();
//5 执行sql语句
int temp =st.executeUpdate(sql);
System.out.println("返回的值:"+temp);
//6 关闭sql对象
st.close();
//7 关闭连接
comm.close();
out.println("注册成功!,将在3s内返回登陆页面。");
resp.setHeader("refresh","3,http://localhost:8080/javaWeb_war_exploded/tijiao.jsp");
System.out.println("b");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
重置密码页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户重置密码</title>
</head>
<body>
用户重置密码
<form action="LoginUp/changePassWd" method="post">
输入账号<input type="text" name="username"/><br>
输入新密码<input type="password" name="passwd"/><br>
<%-- 再输一次密码<input type="password" name="passWd"/><br>--%>
<input type="submit" name="get"/>
</form>
</body>
</html>
package LoginUp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* 重置用户密码
*/
public class changePassWd extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置服务器端以UTF-8编码进行输出
resp.setCharacterEncoding("UTF-8");
//设置浏览器以UTF-8编码进行接收,解决中文乱码问题
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String user = req.getParameter("username");
String passwd = req.getParameter("passwd");
try {
try {
System.out.println("here");
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection comm = DriverManager.getConnection(url);
// String sql = "INSERT into [user] (username,password) VALUES ('"+username+"','"+passwd+"')";
String sql = "update [user] set password='"+passwd+"' where username='"+user+"'";
Statement st = comm.createStatement();
int temp = st.executeUpdate(sql);
System.out.println(temp);
st.close();
comm.close();
out.println("更改成功,请重新登陆");
resp.setHeader("refresh","3,http://localhost:8080/javaWeb_war_exploded/tijiao.jsp");
System.out.println("b"); resp.setHeader("refresh","3,http://localhost:8080/javaWeb_war_exploded/tijiao.jsp");
//
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
更改完成: