js| Note 1. This example basically tells us how to call the JavaBean widget in JSP code
2. The advantage of using JavaBean is to simplify the JSP code, the interface code and the logical code are separated from each other, easy for the programmer to view and debug
3. This example requires five documents: login.jsp,test.jsp, Userbean.class
4. First look at the login.jsp
<html>
<center>
<form method=post action="http://127.0.0.1:8000/test.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=password>
<br><br>
<input type=submit value="注册">
</form>
</center>
</html>
5. The test.jsp code is as follows:
<html>
<jsp:useBean id="hello" class="userbean" scope="session" />
<jsp:setProperty name="hello" property="*" />
your username is:<jsp:getProperty name="hello" property="username"/>
<br><br>
your password is:<jsp:getProperty name="hello" property="password"/>
<br><br>
<%
out.println(hello.insert());
%>
</html>
6. The JavaBean widget Userbean.java code is as follows:
import java.sql.*;
public class userbean
{
private String username;
private String password;
public void setUsername(String username)
{
this.username=username;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public String insert()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,username);
stat.setString(2,password);
stat.executeUpdate();
return "success";
}
catch(Exception e)
{
System.out.println(e);
return e.toString();
}
}
}
7. The configuration method is as follows:
Login,test placed in the public_html of the Java EE, Userbean.class placed in the j2ee\lib\classes