Login to javaweb

Step 1: Create a database and the corresponding user table in the
database

Step 2: Create com.modal
public class UserInfo {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Step 3: Create interface and create method
public interface IUserDAL {
public UserInfo login (String name,String password);
}
Step 4: Create a java class to implement verification, driver registration, and data connection
public class DBUtil {
private static String driver;
private static String name;
private static String url;
private static String password;
static{
Properties prop=new Properties();
try {
prop.load(DBUtil.class.getClassLoader().getResourceAsStream("config.properties"));
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Configuration file error") ;
}
driver=prop.getProperty("driverClassName");
url=prop.getProperty("url");
name=prop.getProperty("name");
password=prop.getProperty("password");
//try to register driver
try {
Class.forName(driver);


} catch (Exception e) {
// TODO: handle exception
System.out.println("Driver registration failed");
}
}
public static Connection Open(){


Connection conn =null;
try {
conn=(Connection)DriverManager.getConnection (url,name,password);
} catch (SQLException e) {
// TODO: handle exception
System.out.println("Database connection failed");
}
return conn;



}
}
Step 5: Create a java class to implement the created Interface, get the corresponding data from the database to implement the connection
public class UserDAL implements IUserDAL{

@Override


public UserInfo login(String name, String password) {
// TODO Auto-generated method stub
//Call the database tool class to connect to the database
Connection conn=DBUtil.Open();
//Verify the login sql statement
String sql="select * from users where name=? and password=?";

try {
PreparedStatement pstt=conn.prepareStatement(sql);
//Pass the parameters corresponding to the placeholder
pstt.setString(1, name);
pstt.setString(2, password) ;


ResultSet rs=pstt.executeQuery();
if(rs.next()){
int id=rs.getInt(1);
UserInfo u=new UserInfo();
u.setId(id);
u.setName(name) ;
u.setPassword(password);

return u;
}


} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
}
System.out.println("shiiba");

return null;
}
}
Step 6: Create a login servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Execute the login operation
String name=request.getParameter("name");
String password=request.getParameter("password");


//The background verification parameter is not Empty
if(null==name||"".equals(name.trim())){
//If the username is empty, exit the program
response.sendRedirect("login.jsp");
return;
}

if(null= =password||"".equals(password.trim())){
//Exit the program if the username is empty
response.sendRedirect("login.jsp");
return;
}

UserDAL userdal=new UserDAL();
UserInfo u=userdal.login(name, password);

//if the user object is empty
if(u==null){
/ /Login

failureresponse.sendRedirect("login.jsp");
}else{
//Login successful
HttpSession session=request.getSession();
session.setAttribute("UserSession", u);
response.sendRedirect("ShowBooklist");
}
}
}
Step 7 Create a configuration file config.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/Database name
name=root
password=root
Step 8: Create a login .jsp login page
<form action="UserLogin" method="post">
<input type="text" id="name" name="name" placeholder="用户名">
<input type="text" id="password" name="password" placeholder="密码">
<input type="submit"value="登录"/>
</form>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325201068&siteId=291194637