An important feature of Jsp is that it can be extended by using javaBean. Put most of the functions in javaBean to make the jsp page program clean, concise, and easy to maintain. JavaBean can be easily used to capture the input of Page forms and process various business logic. The following is a Hello example:
TestA. jsp page:
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Head>
<Title> example </title>
</Head>
<Body scroll = no>
<Form name = "frma" method = "post" action = "testB. jsp">
<P>
Your name:
<Input type = "text" size = "15" name = "yourName" value = "" id = yourName>
<Input type = "button" align = "center" name = "subBtn" value = "[Submit]" onClick = "f_check ()" id = subBtn>
</P>
</Form>
</Body>
</Html>
<Script language = "javascript" type = "text/javascript">
<! --
Function f_check (){
If (document. forms (0). yourName. value = ""){
Alert ("enter your name ");
} Else {
Document. forms (0). submit ();
}
}
-->
</SCRIPT>
TestB. jsp page
<% @ Page contentType = "text/html; charset = GBK" %>
<Html>
<Head>
<Title> example </title>
</Head>
<Jsp: useBean id = "tBean" scope = "page" class = "bean. TestBean">
<Jsp: setProperty name = "tBean" property = "*"/>
</Jsp: useBean>
<Body scroll = no>
<Form name = "frmb" method = "post" action = "">
<P>
<% = TBean. hello () %>
</P>
</Form>
</Body>
</Html>
TestBean. java code:
Package bean;
Public class TestBean {
Private String yourName = "";
Public void setYourName (String yourName ){
This. yourName = ConvertGBK (yourName );
}
Public String hello (){
String strHello = "Hello:" + yourName;
Return strHello;
}
// Chinese character conversion method
Public String ConvertGBK (String str ){
String strReturn = "";
Try {
StrReturn = new String (str. getBytes ("ISO-8859-1"), "GBK ");
} Catch (Exception ex ){
System. out. println ("TestBean. ConvertGBK (): ex =" + ex. toString ());
}
Finally {
Return strReturn;
}
}
}
TestA. the "Submit" button on the jsp page submits the form to testB. jsp page, testB. testA obtained by jsp. after instantiating the value of the yourName text box in jsp, execute the setYourName method in bean, then execute the hello method, and output the hello Statement on the page.
This simple example shows two important aspects of using javaBean in jsp. One is to capture and save the form input, and the other is to execute the logic function. Therefore, the javaBean used in jsp can be divided into value Bean and utility bean based on these two functions, as follows:
Value Bean
Package bean;
Public class TestValueBean {
Private String yourName = "";
Public void setYourName (String yourName ){
This. yourName = ConvertGBK (yourName );
}
// Chinese character conversion method
Public String ConvertGBK (String str ){
String strReturn = "";
Try {
StrReturn = new String (str. getBytes ("ISO-8859-1"), "GBK ");
} Catch (Exception ex ){
System. out. println ("TestBean. ConvertGBK (): ex =" + ex. toString ());
}
Finally {
Return strReturn;
}
}
}
Tool Bean
Package bean;
Public class TestUtilityBean {
Public String hello (TestValueBean tvBean ){
String strHello = "Hello:" + tvBean. getName ();
Return strHello;
}
Public String hello (String yourName ){
String strHello = "Hello:" + yourName;
Return strHello;
}
}
Of course, from this example, there is no need to separate value bean and utility bean, however, in web applications with complex business logic, value beans can be used to capture and save form input, reducing access to entities with almost unchanged values in the database, you can also place value bean in a certain scope to share multiple jsp pages in this scope. Use utility bean to complete database operations, data processing, and other business logic. Use value bean or the value passed on the page as the parameter.