JSP to javabean the various common operation method summary _jsp programming

Source: Internet
Author: User

Define a JavaBean (JavaBean is actually a simple Java class)

javac-d./Userbean.java

Build Package file

Package com.javaweb.ch07;
A simple JavaBean example public
class userbean{
  //username attribute
  private String username;
  User Password attribute
  private String password;
  Get user name public
  String GetUserName () {return
    username;
  }
  Set user name public
  void Setusername (String username) {
    this.username = username;
  }
  Get user Password public
  String GetPassword () {return
    password;
  }
  Set the user's password public
  void SetPassword (String password) {
    this.password = password;
  }
}

<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>
 
 

The generated class must be placed under the Classes folder in the Web-inf in the directory of your own project, not created yourself.

To set JavaBean properties:
use <jsp:setProperty> action directives in JSP to set JavaBean properties, there are four different formats

<jsp:setproperty name= "Instantiate object name" property= "*"/>

The "*" in this way means that the JavaBean property is set based on all the parameters of the table conveys, and the parameter value passed in must be consistent with the name of the property in JavaBean

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <% @page import= "java.sql.*"%> <! DOCTYPE html>  

Submit a form page ditto

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <!
    DOCTYPE html>  

Note Here password the Name property of the form

<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>
 
 

The code above notices the blue part of the red, which is a better elasticity.

<jsp:setproperty name= "instantiated object Name" property= "Property name value=" property value "/>

<% @page language=" java "contenttype=" text /html;charset=gb2312 "%>
<! DOCTYPE html>
 
 

To get JavaBean properties:

<jsp:getproperty name= "property=" "/>

<% @page language=" java "contenttype=" text/html;charset=gb2312 " %>
<! DOCTYPE html>
 
 

Set the scope of the JavaBean:
Set page-range JavaBean

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>  

Set the JavaBean for the request scope

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>  

Set JavaBean for session scope

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>  

Set the application range JavaBean

<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>  

To remove JavaBean properties:
the removal of JavaBean varies according to different ranges

Page range, request range, session range, application range corresponding to use Pagecontext,request,session,application

Method uses the RemoveAttribute () method

Example

<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>
 
 

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.