jar包下载地址:https://download.csdn.net/download/qq_43001132/88629520
CREATE TABLE db_users (
id INT(10) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) DEFAULT NULL,
password VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
package com.shop.pojo;
import com.shop.dao.Loginorregister;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public boolean login(String username, String password) {
Loginorregister lr = new Loginorregister();
if (lr.queryUser(username, password)) {
return true;
} else {
return false;
}
}
public boolean register(String username, String password) {
Loginorregister lr = new Loginorregister();
if (lr.registeruser(username, password)) {
return true;
} else {
return false;
}
}
}
package com.shop.dao;
import java.sql.*;
import com.shop.pojo.User;
public class LoginAndRegister {
private final String URL = "jdbc:mysql://localhost:3306/db_strust2";
private final String USENAME = "root";
private final String PASSWORD = "root";
public boolean queryUser(String username, String password) {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
User user = new User();
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USENAME, PASSWORD);
String sql = "select * from db_users where username = ? and password = ?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("登录成功" + username);
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
// return false;
}
public boolean registeruser(String username, String password) {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USENAME, PASSWORD);
String sql = "insert into db_users (username,password) value (?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setNString(2, password);
int count = pstmt.executeUpdate();
if (count > 0) {
System.out.println("注册成功!");
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
package com.shop.web;
import com.shop.pojo.User;
public class UserAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
User lb = new User();
if(lb.login(username, password)) {
return "success";
}else{
return "error";
}
}
public String execute2() {
User lb = new User();
if(lb.register(username, password)) {
return "success";
}else {
return "error";
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功页面</title>
</head>
<body>登录成功!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form method="post" action="register">
用户名:<input name="username" type="text"><br> 密码:<input
name="password" type="password"><br> <input
type="submit" value="注册">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功页面</title>
</head>
<body>
<h1>注册成功!</h1>
</body>
</html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>struts2-chpater1-annotationdisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="login" extends="struts-default" namespace="/">
<action name="login" class="com.shop.web.UserAction" method="execute">
<result name="success">/page/loginsuccess.jspresult>
<result name="error">/login.jspresult>
action>
package>
<package name="register" extends="struts-default" namespace="/">
<action name="register" class="com.shop.web.UserAction" method="execute2">
<result name="success">/registersuccess.jspresult>
<result name="error">/register.jspresult>
action>
package>
struts>