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

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

项目结构

在这里插入图片描述

项目所需jar包文件

在这里插入图片描述
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文件配置

<?xml version="1.0" encoding="UTF-8"?>
<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-annotation</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

struts.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!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.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
	<package name="register" extends="struts-default" namespace="/">
		<action name="register" class="com.shop.web.UserAction" method="execute2">
			<!-- 配置方法的返回值到页面 -->
			<result name="success">/registersuccess.jsp</result>
			<result name="error">/register.jsp</result>
		</action>
	</package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值