The simplest JSP and JavaBean: setProperty and getProperty

Source: Internet
Author: User

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
 

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.