1,建立一个javabean封装用户的信息(姓名和密码)UserInfo.java,其中的方法有
setter方法来设置用户的信息,getter方法来获取用户的信息
package user;//存放UserInfo.java的包,该包中放在web-inf目录下
public class UserInfo
{
private String password;
private String name;
//获取信息
public String getPassword()
{
return this.password;
}
public String getName()
{
return this.name;
}
//设置信息
public void setPassword(String p)
{
this.password=p;
}
public void setName(String n)
{
this.name=name;
}
}
--------------------------------------------------------------------------------------------------
/*2,现在做的事就是建立一个javabean处理数据库操作以及一些简单的逻辑操作UserRegist.java*/
package user;//同理也放在这个包中
import java.sql.*;
public class UserRegist
{
private UserInfo userInfo;//在此javabean中把UserInfo.java这个javabean引进来
private Connection conn=null;
//连接数据库
public UserRegist()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/joke?user=root");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setUserInfo(UserInfo userInfo)
{
this.userInfo=userInfo;
}
//进行注册
public void regist()throws Exception
{
String reg="insert into user_Info values(?,?)";
try
{
PreparedStatement pstmt=conn.prepareStatement(reg);
//create a preparestatement sentence ,then to set their name
pstmt.setString(1,userInfo.getPassword());
pstmt.setString(2,userInfo.getName());
//excute update
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
} -----------------------------------------------------------------------------------------------------------------
setter方法来设置用户的信息,getter方法来获取用户的信息
package user;//存放UserInfo.java的包,该包中放在web-inf目录下
public class UserInfo
{
private String password;
private String name;
//获取信息
public String getPassword()
{
return this.password;
}
public String getName()
{
return this.name;
}
//设置信息
public void setPassword(String p)
{
this.password=p;
}
public void setName(String n)
{
this.name=name;
}
}
--------------------------------------------------------------------------------------------------
/*2,现在做的事就是建立一个javabean处理数据库操作以及一些简单的逻辑操作UserRegist.java*/
package user;//同理也放在这个包中
import java.sql.*;
public class UserRegist
{
private UserInfo userInfo;//在此javabean中把UserInfo.java这个javabean引进来
private Connection conn=null;
//连接数据库
public UserRegist()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/joke?user=root");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setUserInfo(UserInfo userInfo)
{
this.userInfo=userInfo;
}
//进行注册
public void regist()throws Exception
{
String reg="insert into user_Info values(?,?)";
try
{
PreparedStatement pstmt=conn.prepareStatement(reg);
//create a preparestatement sentence ,then to set their name
pstmt.setString(1,userInfo.getPassword());
pstmt.setString(2,userInfo.getName());
//excute update
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
} -----------------------------------------------------------------------------------------------------------------
/*3,好了,我们的要用到的javbean就建立完了,现在就是做一个简单的表单html页面了,login.html,接受用户输入的信息*/
<form action="regist.jsp" name="form1">
<table>
<tr>
<td> username:</td>
<td><input name="name"></input></td>
</tr>
<tr>
<td>userpassword:</td>
<td><input type="password" name="password"></input></td>
</tr>
<tr>
<td><input type="submit" value="go"></td>
<td><input type="reset" value="reset"></td>
<table>
</form>
/*4,最后我们做的事就是用一个简单的jsp页面让两个javabean工作起来,regist.jsp*/
<%@page contentType="text/html;charset=GB2312"%>
//利用此标签来调用UserInfo.java
<jsp:useBean id="userInfo" class="user.UserInfo" scope="page">
//利用此标签来用UserInfo.java接受并保存表单中的信息
<jsp:setProperty name="userInfo" property="*"/>
</jsp:useBean>
//来调用UserRegist.java
<jsp:useBean id="regist" class="user.UserRegist" scope="page"/>
<%
regist.setUserInfo(userInfo);//调用UserRegist.java中的setUserInfo()方法获取保存在serInfo.java中的信息
regist.regist( );//调用UserRegist.java中的regist( )方法进行注册
out.println("registed ok!");
%>
<br><hr><br>
<a href="login.jsp">back</a>