Java的struts2+MySQL+jsp实现登录、注册以及文件上传

Java的struts2+MySQL+jsp实现登录、注册以及文件上传

项目结构

Java的struts2+MySQL+jsp实现登录、注册以及文件上传_第1张图片

项目所需jar包文件

Java的struts2+MySQL+jsp实现登录、注册以及文件上传_第2张图片
jar包下载地址:https://download.csdn.net/download/qq_43001132/88629520

数据库设计SQL

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; 

pojo实体类

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;
		}
	}
}

dao层

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;
		}
	}
}

Action

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";
		}
	}
}

登录页面login.jsp

<%@ 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.xml文件配置


<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>

struts.xml文件配置


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>

你可能感兴趣的:(Java,struts框架,java,struts,mysql)