首页 > 代码库 > JSP+JavaBean

JSP+JavaBean

使用JSP+JavaBean模式编写一个简单的用户信息管理系统

考虑实现一个数据库单表操作的简单Web应用,系统需求如下:

普通用户功能:用户登录系统;用户注册;

1、数据库设计

SQL Server中使用企业管理器建立数据库和user表。user表的字段设计如下:

字段名

id

name

password

email

age

birthday

money

描述

自增型int

varchar

varchar

varchar

int

datetime

float

 

2、创建User JavaBean

创建User JavaBean,其属性和类型如下:

属性名

id

name

password

email

age

birthday

money

类型

int

String

String

String

int

datetime

float

 

3、为User JavaBean增加业务方法

Student JavaBean的业务方法有loginregister,分别实现用户登录验证、注册用户信息。在Student JavaBean中增加实现上述功能的代码并测试释放正常。

4、编写JSP页面实现用户登录、用户注册

编写实现用户登录、注册的页面,并调试程序是否正常。


User类:

package JavaBeen_Demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class User {
	private int id;
	private String name;
	private String password;
	private int age;
	private String email;
	private String birthday;
	private float money;

	public User(){};
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	//登录
	public User login(){
		User user=null;
		DBConnection db=new DBConnection();
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;  
		try {
			conn=db.getConnection();
			String sql="select * from yonghu where name='"+this.name+"'";
			System.out.println(sql);
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				user=new User();
				user.name=rs.getString("name");  
				user.password=rs.getString("password");  
				user.age=rs.getInt("age");
				user.email=rs.getString("email");  
				user.birthday=rs.getString("birthday");
				user.money=rs.getFloat("money");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		db.close(rs);
		db.close(ps);
		db.close(conn);
		return user;
	}
	
	//注册
	public boolean register(){
		DBConnection db=new DBConnection();
		Connection conn=null;
		PreparedStatement ps=null;
		boolean flag=false;
		try {
			conn=db.getConnection();
			String sql="insert into yonghu values(?,?,?,?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, this.name);
			ps.setString(2,this.password);
			ps.setInt(3,this.age);
			ps.setString(4,this.email);
			ps.setString(5,this.birthday);
			ps.setFloat(6,this.money);
			ps.executeUpdate();
			flag=true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		db.close(ps);
		db.close(conn);
		return flag;
	}

}

连接数据库(SQL Server):

package JavaBeen_Demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConnection {
	static String user="sa";
	static String password="a123456";
	static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	static String url="jdbc:sqlserver://localhost:1433;DatabaseName=User";
	Connection conn;
	PreparedStatement ps;
	ResultSet rs;
	static{
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println("鍔犺浇椹卞姩鍑洪敊");
		}
	}

	public  Connection getConnection() throws SQLException{   
		conn=DriverManager.getConnection(url,user,password);
		return conn;
	}
	
	public void close(Connection connection,PreparedStatement preparedStatement,ResultSet rsResultSet) {
		try {
			if (null!=rsResultSet) {
				rsResultSet.close();
			}
			if (null != preparedStatement) {
				preparedStatement.close();
			}
			if(null!=connection){
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public  void close(Connection conn) {
		try {
			if(conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public  void close(PreparedStatement ps) {
		try {
			if(ps != null) {
				ps.close();
				ps = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public  void close(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

主界面:

<!DOCTYPE html>
<html>
  <head>
    <title>Main.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  </head>
  
  <body>
    <a href=http://www.mamicode.com/"log.html" name="log">登录
>
登录界面:

<!DOCTYPE html>
<html>
<head>
<title>log.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href=http://www.mamicode.com/"./styles.css">-->>
注册界面:

<!DOCTYPE html>
<html>
  <head>
    <title>registerl.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href=http://www.mamicode.com/"./styles.css">-->>
登录界面的操作:

<%@ page language="java" import="java.util.*,JavaBeen_Demo.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>
注册界面操作:

<%@ page language="java" import="java.util.*,JavaBeen_Demo.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>
个人信息界面:

<%@ page language="java" import="java.util.*,JavaBeen_Demo.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>
登录成功界面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>
登录失败界面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>



JSP+JavaBean