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="log.html" name="log">登录</a><br/>
    <a href="registerl.html" name="register">注冊</a><br/>
  </body>
</html>

登录界面:

<!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="./styles.css">-->

</head>

<body>
	<center>
		用户登录
		<form action="../JSP/log_do.jsp" method="post">
			username:<input type="text" name="name" /><br />
			 密码:  <input type="password" name="password" /><br /> 
			 <input type="submit" value="登录" />
		</form>
	</center>
</body>
</html>

注冊界面:

<!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="./styles.css">-->

  </head>
  
  <body>
    <center>
    	注冊界面
    	<form action="../JSP/register_do.jsp" method="post">
    	username:<input type="text" name="name"/><br/>
    	密码:<input type="password" name="password"/><br/>
    	年龄:<input type="text" name="age"/><br/>
    	邮箱:<input type="text" name="email"/><br/>
    	生日:<input type="text" name="birthday"/><br/>
    	金钱:<input type="text" name="money"/><br/>
    	<input type="submit" value="注冊"/><br/>
    	</form>
    </center>
  </body>
</html>

登录界面的操作:

<%@ 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="<%=basePath%>">
    
    <title>登录操作</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
  	<!-- javabeen -->
   	<jsp:useBean id="user" class="JavaBeen_Demo.User" scope="session"/>
    <jsp:setProperty name="user" property="*"/>
    <%
    	User new_user=user.login();
    	request.setAttribute("info", new_user);//传对象
    	if(new_user!=null){//假设存在此user
    		request.getRequestDispatcher("Info.jsp").forward(request, response);
    	}
    	else{
    		request.getRequestDispatcher("Faild.jsp").forward(request, response);
    	}
     %>
  </body>
</html>

注冊界面操作:

<%@ 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="<%=basePath%>">
    
    <title>注冊操作</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

  </head>
  
  <body>
    <jsp:useBean id="user" class="JavaBeen_Demo.User" scope="session"/>
    <jsp:setProperty name="user" property="*"/>
    <%
    	boolean flag=user.register();
    	if(flag){
    		request.getRequestDispatcher("Success.jsp").forward(request, response);
    	}
    	else{
    		request.getRequestDispatcher("Faild.jsp").forward(request, response);
    	}
     %>
  </body>
</html>

个人信息界面:

<%@ 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="<%=basePath%>">
    
    <title>个人信息</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
  <center>
  	<%
  		User user=(User)request.getAttribute("info");//接收对象
  	 %>
  	 id: 
  	 <%=user.getId() %><br/>
  	 name: 
  	 <%=user.getName() %><br/>
  	 password: 
  	 <%=user.getPassword() %><br/>
  	 age: 
  	 <%=user.getAge() %><br/>
  	 birthday: 
  	 <%=user.getBirthday() %><br/>
  	 email: 
  	 <%=user.getEmail() %><br/>
  	 money: 
  	 <%=user.getMoney() %><br/>
  	 </center>
  </body>
</html>

登录成功界面:

<%@ 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="<%=basePath%>">
    
    <title>My JSP 'Success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	注冊成功。
  </body>
</html>

登录失败界面:

<%@ 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="<%=basePath%>">
    
    <title>My JSP 'Faild.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    注册失败!

</body> </html>




原文地址:https://www.cnblogs.com/blfshiye/p/4565264.html