JSP study notes (5) ----- JSP Using Javabean

xiaoxiao2021-03-06  87

1. This example mainly tells us how to call JavaBean components in JSP code

2. The advantage of using JavaBean is to simplify the JSP code, interface code, and logic code mutual separation, easy to view and debug the programmer.

3. This example requires five files: login.jsp, test.jsp, Userbean.class

4. Let's take a look at Login.jsp

Username



Password



5. Test.jsp code is as follows:

Your username is:



Your password is:



<%

Out.println (Hello.insert ());

%>

6. The JavaBean component 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 value (?,?)");

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 is placed in J2EE's public_html, Userbean.class is placed in J2EE / LIB / CLASSES

转载请注明原文地址:https://www.9cbs.com/read-122283.html

New Post(0)