JSP Study Notes (5) -- Use Javabean in JSP

Source: Internet
Author: User
Tags odbc

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.