JSP and JavaBean can call each other's content. Now we implement a simple JSP and JavaBean.
We implement the function of inputting text, passing the text to the JavaBean, and then letting the JavaBean return content to the JSP. We need a JSP file and a JavaBean.
JavaBean file
Create a packagemane package in the scr directory, and create a java file named BeanName. java under this package (with any file name)
The Code is as follows:
[Java]
Package packagename;
Public class BeanName {
String ProName;
// The parameter name must be the same as the value of property and param in jsp: setProperty.
// JavaBean has a set method and a get method. Pay attention to the relationship between method names and parameter names. Pay attention to the correspondence between method return values.
Public void setProName (String proName ){
This. ProName = proName;
}
Public String getProName (){
String newStr = "you entered:" + ProName;
Return newStr;
}
}
JSP file
Create a jsp file under WebRoot or its subdirectory with any name.
The Code is as follows:
There are three key points:
1. useBean.
Define the Bean name. The names of setProperty and getProperty must be the same as the names of setProperty and getProperty.
2. setProperty
There are several methods to pass the content on the webpage to JavaBean. setProperty.
We recommend that you use the same property And param values. In fact, I have not yet figured out what exactly corresponds to them.
Note that this value must be the same as the parameter name in the JavaBean file. For example, I wrote proName here, and the parameter in JavaBean is String ProName.
3. getProperty
It is used to pass the content in the JavaBean to the webpage.
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% Request. setCharacterEncoding ("UTF-8"); %>
<% Response. setCharacterEncoding ("UTF-8"); %>
<% -- Strong defense against Chinese garbled characters -- %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> simplest JSP and JavaBean </title>
<Jsp: useBean id = "entityName" scope = "page" class = "packagename. BeanName"> </jsp: useBean>
<! -- Scope has four parameters: page, request, session, and application.
The class value is the package name. Bean file name -->
<Jsp: setProperty name = "entityName" property = "proName" param = "proName"/>
<! -- SetProperty is to pass the JSP webpage parameter to JavaBean, while getProperty is to pass the JavaBean parameter to the JSP page
To avoid confusion, it is best to use the same property And param. And the value must be the same as the parameter name in the Bean file. -->
</Head>
<Body>
<Form name = "form1" action = "" method = "post">
Enter the content sent to JavaBean here: <br>
<Input type = "text" name = "proName"/>
<Input type = "submit" name = "T1" value = "submit"/>
<Br>
</Form>
The content returned from the JavaBean is shown below: <br>
<Jsp: getProperty name = "entityName" property = "proName"/>
</Body>
</Html> www.2cto.com
Effect
Before Input
After submission
By: shirenfeigui