前言:对于登陆验证,常有的就是用户名,密码,验证码,手机号码等等进行合法性验证
完成登陆验证的基本思路
- 创建login.jsp界面,搭建好相关的内容
- 创建数据库,创建dao类
- 创建逻辑处理类,用来处理登陆逻辑
1.login.jsp界面
<form action="${pageContext.request.contextPath }/doLoginServlet" method="post">
<input type="text" placeholder="InputUserName" name="user_name"/>
<input type="text" placeholder="InputPassWd" name="user_pwd"/>
<input type="text" placeholder="InputPassCode" name="user_code"/>
<img src="/LoginProject/loginCode"/>
<input type="submit" value="提交"/>
</form>
需要注意的是
1input标签中的name属性很重要,因为他需要请求对象通过获取参数方法获取值
2.form中的action路径,在实际的项目中,动态获取虚拟路径是比较合适的
Dao模式操作类
//连接方法
public static Connection getConn(){
try {
Class.forName(CONTEXT_QD);
System.out.println("驱动加载成功!");
Connection conn = DriverManager.getConnection(DB_URL , DB_NAME , DB_PASS);
if(conn != null){
System.out.println("数据库连接成功!!");
}
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(ResultSet rs , PreparedStatement ps , Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//重载
public static void close( PreparedStatement ps , Connection conn){
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
**养成打印程序状态的好习惯,为了方便,这里暂时使用比较简便的连接方式 **
public List<User> executeQue(String sql , Object...obj){
//连接
Connection conn = getConn();
try {
//创建预编译对象
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
//把传入的内容逐渐传入每一列
ps.setObject(i+1, obj[i]);
}
//接受查询到的ResultSet对象
ResultSet rs = ps.executeQuery();
List<User> list = new ArrayList<>();
//遍历所有的
while(rs.next()){
String name = rs.getString(3);
String pwd = rs.getString(4);
User us = new User(name, pwd);
list.add(us);
System.out.println("name = " + name + " ---- pwd = " + pwd);
}
close(rs, ps, conn);
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
实体类
public class User {
private String name;
private String pwd;
}
因为是表格传输,无法确定传过来的是中文还是英文,所以在servler中应该设置编码
request.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
根据input中的name属性,将值取出来
String user_name = request.getParameter(“user_name”);
String user_pwd = request.getParameter(“user_pwd”);
//验证码在servlet中存入session中,这里是获取用户输入的验证码
String user_code = request.getParameter(“user_code”);
验证码中将程序生成的存入Session中
String session_str = “”;
String str = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”
Random r = new Random();
for (int j = 1; j <= 4; j++) {
int index = r.nextInt(str.length());
char chars = str.charAt(index);
session_str += chars;
graphics.drawString(chars+"", width/5 * j, height/5 * j);
}
HttpSession session = request.getSession();
session.setAttribute("code_str", session_str);
逻辑文件doLogin.java
HttpSession session = request.getSession();
String str_code = (String) session.getAttribute("code_str");
System.out.println("session ----- code_str = " + str_code);
if(str_code != null && user_code != null){
System.out.println("说明session 和 用户输入 不是空噢 ----- code_str = " + str_code);
if(str_code.equals(user_code)){
System.out.println("用户输入的和session ----- code_str 一致");
//当验证码存到的session值和用户输入的值一致时,即可继续判断用户名和密码
//判断用户名或者密码不能为空
if(user_name != null && user_pwd != null){
//检测登陆 ,查询数据库
UserDao dao = new UserDao();
//查询
Boolean isTrue = dao.CheckLogin(user_name, user_pwd);
/**
* 登陆成功或者失败
*/
//成功 , 将参数存起来打包 再 重定向到success.jsp界面
if(isTrue){
System.out.println("-------- 查询成功-----");
User isUser = new User(user_name, user_pwd);
request.getSession().setAttribute("Users", isUser);
response.sendRedirect("success.jsp");
}else{
System.out.println("查询失败...");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
}
查询成功了,则跳转登陆页面,如果失败了,转发到登陆界面重新登陆
好了,简单的Servlet+jsp+数据库登陆验证案例,就完成咯,希望能够帮到大家