JSP+Servlet+MysQL(MVC)实现登陆验证
程序员文章站
2022-07-08 17:21:31
...
第一步写登陆的jsp页面(View)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
账号:<input type="text" name="name" /><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登陆" />
</form>
</body>
</html>
第二步,建立对应的Servlet(Controller)
package cn.yf.MVC.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
Login login = new Login(name,password);
LoginDao loginDao = new LoginDao();
int result = loginDao.login(login);
if(result == 1){
System.out.println("登陆成功!");
}else if(result == 0){
System.out.println("账号密码有误!");
}else{
System.out.println("系统出现错误!");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
这一步中第一件事就是拿到jsp传过来的name和password,然后将name和password封装进Login(一个JavaBean也就是Model中的数据封装),然后调用DAO层写好的LoginDao中的login方法(也是JavaBean不过说Model中的功能封装),然后根据返回值判断结果。
第三步就是写JavaBean(Model)
Login
package cn.yf.MVC.servlet;
public class Login {
private String id;
private String name;
public Login(){}
public Login(String id, String name) {
super();
this.id = id;
this.name = name;
};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
LoginDao
package cn.yf.MVC.servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginDao {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8";
private static final String USERNAME="root";
private static final String PASSWORD="admin";
public int login(Login login){
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//此条sql语句返回的是0或1
String sql = "select count(*) from category_ where id=? and name=?";
ps = connection.prepareStatement(sql);
ps.setString(1, login.getId());
ps.setString(2, login.getName());
rs = ps.executeQuery();
if(rs.next()){
int count = rs.getInt(1);
if(count == 1){
return 1;
}else{
return 0;
}
}else{
return -1;
}
}catch(ClassNotFoundException e){
e.printStackTrace();
return -1;
}catch(SQLException e){
e.printStackTrace();
return -1;
}catch(Exception e){
e.printStackTrace();
return -1;
}finally{
try{
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(connection!=null) connection.close();
}catch(final SQLException e){
e.printStackTrace();
return -1;
}
}
}
}
最后总结一下,首先是在LoginServlet拿到jsp传过来的name和password。然后我们要做的是登陆验证,所以我们下一步要做的是把DAO层写好,也就是JDBC。而且我们要把jsp传过来的数据,传递到DAO层,因此我们选择了将数据封装成一个JavaBean,方便以后使用。完成登陆验证后,根据DAO层返回的值,我们可以做出判断,可以跳转到其他页面,也可以返回登陆页面重新登陆,这里我只是做了简单的处理。