html+JavaBean+jsp实现用户注册

1.建立JavaBean,包含username和passsword属性以及regist()方法
2.建立regist.html页面,包含一个表单,提交到regist.jsp页面进行处理
3.建立regist.jsp页面,set属性值,调用JavaBean中的regist方法()进行注册

UserInfo.java:

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

import myPackage.MySqlClass;

public class UserInfo {

    private String username ;
    private String password ;
    public String getUsername() {
        return username;
    }
    public void setUsername(String name) {
        this.username = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void regist(){
        Connection con=null;
        try {
            con= MySqlClass.MyConnection("db_students");
        } catch (ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        //注册
        String regString="insert into tb_users (用户名,密码) values (?,?)";
        try {
            PreparedStatement ps=con.prepareStatement(regString);
            ps.setString(1, this.username);
            ps.setString(2, this.password);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }


}

regist.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="regist.jsp" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="提交">
</form>
</body>
</html>

regist.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>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8"); %>
<%response.setCharacterEncoding("utf-8"); %>
<%response.setContentType("text/html;charset=UTF-8");%>
<jsp:useBean id="userinfo" class="javabean.UserInfo">
    <jsp:setProperty  name="userinfo" property="*"/><!-- set所有属性 -->
    <!-- name属性对应Javabean中的id值,property属性指明要获取的javabean属性名称。 -->
</jsp:useBean>

<p><jsp:getProperty name="userinfo" property="username"/></p>
<p><jsp:getProperty name="userinfo" property="password"/></p>
<% userinfo.regist(); //注册%>

</body>
</html>
posted @ 2017-04-09 15:30  SEC.VIP_网络安全服务  阅读(146)  评论(0编辑  收藏  举报