javaweb学习(五)------Javabean

Javabean

基本知识储备

  • 注意:
    java代码要放到指定包中,jsp调用java类要记得导包,导包时要加前缀包名。

  • 项目目录:
    在这里插入图片描述
    将jsp中登录的代码转移到LoginDao.java;其中LoginDao类就称之为javaBean。

  • Javabean的作用:
    a.减轻了jsp复杂度
    b.提高代码复用(以后任何地方的登录操作,都可以通过LoginDao实现)

  • Javabean(java类)的定义:
    a.public修饰的类,public无参构造
    b.所有的属性都是private,并且提供set/get(如果boolean 则get替换为is)

  • 使用层面,Javabean分为2大类:
    a.封装业务逻辑的Javabean(LoginDao.java封装了登录逻辑)
    b.封装数据的javabean(实体类:Student.java、Person.java)----对应于数据库里的一张表。

实例—用户名密码登录验证(jsp–>jsp+java)

  • index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
<style>
.box{
    position: absolute;/*生成绝对定位的元素,元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。(此处相对于body)*/
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);/* 实现块元素百分比下居中*/
    width: 400px;
    padding: 40px;/*设置四个内边距*/
    background: rgba(0,0,0,0.8);/*函数的前三个变量为颜色(黑),后一个为透明度(1为不透明)*/
    box-sizing: border-box;/*使实际所占宽高度 = 设置的高度(height)/ 设置的宽度(width)+ 外边距(margin)*/
    box-shadow: 0 15px 25px rgba(0,0,0,.5);/*产生盒子阴影:x轴偏移,y轴偏移,模糊度,透明色*/
    border-radius: 10px;/*使矩形四边圆滑*/
}
.box h2{
    margin: 0 0 30px;/*上、左右、下*/
    padding: 0;
    color: #ffffff;/*设置字体颜色*/
    text-align: center;/*文字居中*/
}
.box .inputBox{
    position: relative;/*相对定位*/
}
.box .inputBox input{
    width: 100%;
    padding: 10px 0;/*上下、左右*/
    font-size: 16px;/*字体大小(可用于调节输入框高)*/
    color: #ffffff;/*输入框颜色*/
    letter-spacing: 1px;/*设置对象中的文字之间的间隔*/
    margin-bottom: 30px;/*设置下外距*/
    border: none;/*定义无边框*/
    border-bottom: 1px solid #ffffff;/*底边框:大小1PX 实线 颜色#ffffff */
    outline: none;/*设置无外边框*/
    background: transparent;/*背景设置为透明*/
}
.box .inputBox label{
    position: absolute;
    top: 0;
    left: 0;
    letter-spacing: 1px;
    padding: 10px 0;
    font-size: 16px;
    color: #ffffff;
    pointer-events:none;/*阻止区块被点击,使得点击穿透*/
    transition: 1s;/*动画运行时间*/
}
/*产生对应操作时,label发生的样式变化*/
.box .inputBox input:focus~label,
.box .inputBox input:valid~label
{
    top:-18px;
    left: 0;
    color: #03a9f4;
    font-size: 12px;
}
.box input[type="submit"]{
   background: transparent;
    border: none;
    outline: none;
    color: #ffffff;
    background: #03a9f4;
    margin: 0px 80px;
    padding: 10px 30px;
    cursor: pointer;/*鼠标指针变成手的形状*/
    border-radius: 15px;/*调节矩形弧度*/
}
</style>
</head>
<body>

<div class="box">
        <h2>Login</h2>
        <!--创建供用户输入的 HTML 表单-->
        <form action="check.jsp" method="post">
            <div class="inputBox">
                <input type="text" name="uname" required="">
                <label>Username</label>
            </div>
            <div class="inputBox">
                <input type="password" name="upwd" required="">
                <label>Password</label>
            </div>
            <div align="center">
                <input type="submit" name="" value="提交">
            </div>

        </form>
    </div>

</body>
</html>
  • check.jsp
<%@ page import="com.zr.LoginDao" %>
<%@ page import="com.zr.person.Login" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	String name = request.getParameter("uname");
	String pwd = request.getParameter("upwd");
	Login login=new Login(name,pwd);
	LoginDao dao=new LoginDao();
	int result=dao.Login(login);
	if(result>0){
		out.println("登陆成功!");
	}else if(result==0){
		out.println("用户名或密码有误,登陆失败!");
	}else{
		out.print("系统出现异常!");
	}
%>
</body>
</html>
  • 封装业务逻辑的Javabean(LoginDao.java封装了登录逻辑)
package com.zr;
import java.sql.*;
//封装业务逻辑的Javabean
public class LoginDao {
	public int Login(com.zr.person.Login login) {
		Connection con=null;//连接流
		PreparedStatement pstmt=null;//PreparedStatement接口--实现SQL安全问题
		ResultSet rs=null;//结果流
		try {
		
			//数据库驱动
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("数据库驱动成功!");
			//创建连接
			String url="jdbc:sqlserver://localhost:1433;databaseName=用户注册库";//数据库配置信息
			String username="sa";//数据库登录账号
			String password="1234567";//数据库登录密码
			con=DriverManager.getConnection(url,username,password);
			if (con!=null) {
				System.out.println("已创建连接!");
			
				
				String sql="select count(*) from tb_user where username=? and password=?";
				pstmt=con.prepareStatement(sql);
				pstmt.setString(1, login.getUsername());
				pstmt.setString(2, login.getPassword());
				rs=pstmt.executeQuery();
				int count=-1;
				if(rs.next()){
					count=rs.getInt(1);
				}
			return count;
			
			}else {
				System.out.println("数据库连接失败!");
			}
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		}finally{
			//打开顺序与关闭顺序相反
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
			if(pstmt!=null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
			if(con!=null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		}
		return 0;
	}
}
  • 封装数据的javabean(实体类:Login)----对应于数据库里的一张表。
package com.zr.person;
//封装数据的javabean
public class Login {
	private String username;
	private String password;
	private String sex;
	private String question;
	private String answer;
	private String email;
	//get、set函数
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	//定义构造函数
	public Login(String username, String password, String sex, String question, String answer, String email) {
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.question = question;
		this.answer = answer;
		this.email = email;
	}
	public Login(String username, String password) {
		this.username = username;
		this.password = password;
	}
	public Login() {
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值