1. This instance mainly tells us how to call the JavaBean Component in JSP code.
2. The advantage of using Javabean is that JSP code is simplified, interface code and logic code are separated from each other, so that programmers can easily view and debug the code.
3. This instance requires five files: Login. jsp, test. jsp, userbean. Class
4. First, let's take a look at login. jsp.
<HTML>
<Center>
<Form method = post action = "http: // 127.0.0.1: 8000/test. jsp">
Username <input type = text name = username>
<Br>
Password <input type = password name = PASSWORD>
<Br>
<Input type = submit value = "register">
</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>
Your password is: <JSP: getproperty name = "hello" property = "password"/>
<Br>
<%
Out. println (hello. insert ());
%>
</Html>
6. The userbean. Java code of the an component 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.exe cuteupdate ();
Return "success ";
}
Catch (exception E)
{
System. Out. println (E );
Return e. tostring ();
}
}
}
7. The configuration method is as follows:
Login and test are placed in public_html of J2EE, and userbean. Class is placed in J2EE/lib/classes.