Lesson 3-JSP: useBean usage and Explanation

Source: Internet
Author: User

 

Lesson 3-JSP-useBean usage and Explanation

One worker basic element Bean class:
1. there must be an empty constructor. When the JSP element creates the Bean, it will call the empty constructor 2. there cannot be any public instance variables (that is, variables cannot be accessed directly through the variable name), and the first letter of the variable name must be lowercase. 3. Read and Write the value of a variable through the getter/setter method, and the first letter of the variable in the method must be capitalized. 4. Do not use native classes (to package) 5. Supplement: it is best to implement the java. io. Serializable interface to convert it to a byte stream,
(The data difference between a local network can be better eliminated through decompilation. Baidu is recommended for other advantages .)



Second, the basic usage code is as follows:

 
  
/* The reference name after the id is instantiated. class Bean class scope is the scope of the variable. The default value is page. (page is the page local variable, request range, session range, and application range) by default, the type is the same as the class, and the new BeanClass is used as the TypeName. (Parent class references to subclass objects) equivalent to java statement: */package. BeanClass beanName = null, TypeName = null; TypeName beanName = new package. BeanClass ();
  
   
/* Name Bean class name after instantiation the value assignment of the Private member variable of the Bean class is equivalent to the java statement: */beanName. setProperty (value );
   
    
/* Name Bean class name after instantiation the private member variable of the property Bean class is equivalent to the java statement: */out. write (beanName. getProperty ());
   
  
 
Standard instance Student. java
package com.qsuron.study;public class Student implements java.io.Serializable{private String name;public  String getName() {return name;}public  void setName(String name) {this.name = name;}}
UseBean. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
The retrieved value is:

Detailed description of Three-dimensional Scope-page request session applicationBean. java
package com.qsuron.study;public class Bean implements java.io.Serializable{private int count = 0;public int getCount() {count++;return count;}public void setCount(int count) {this.count = count;}}

PageBean. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
Scope: page:
RequestBean. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
<% -- Equivalent to com. qsuron. study. bean bean = (Bean) request. getAttribute (bean); if (bean = null) {bean = new com. qsuron. study. bean (); request. setAttribute (bean, bean) ;}-- %> <% bean. setCount (100); %>/> scope: request1 <% -- RequestBean2 returns 101 -- %> <% -- -- %> <% -- RequestBean2 returns 1 -- %> <% response. sendRedirect (RequestBean2.jsp); %> Please note the remarks section. If you do not understand, please refer to the blog post-(Lesson 2-difference between Forward and SendReDirect in Jsp)


RequestBean2.jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
<% -- Equivalent to com. qsuron. study. bean bean = (Bean) request. getAttribute (bean); if (bean = null) {bean = new com. qsuron. study. bean (); request. setAttribute (bean, bean);} -- %> scope: request2 is taken out of the following values: Forward-normal incremental sendRedirect-reinitialization

SessionBean. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
<% -- Equivalent to com. qsuron. study. bean bean = (Bean) session. getAttribute (bean); if (bean = null) {bean = new com. qsuron. study. bean (); session. setAttribute (bean, bean) ;}-- %> scope: the value retrieved by the session is:
The same window increments normally, and the new window is reinitialized.
ApplicationBean. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@page import=com.qsuron.study.* %>
<% -- Equivalent to com. qsuron. study. bean bean = (Bean) session. getAttribute (bean); if (bean = null) {bean = new com. qsuron. study. bean (); session. setAttribute (bean, bean);} -- %> scope: the value retrieved by application is:
If you do not restart Tomcat, it will increase progressively. Restart and then reinitialize.


IV. Other related syntax param Parameters
 
  
// Directly assign the URLparam passed from the previous page by rewriting the URL to num. // equivalent
  
   
/>
  
 

Property = * store the key-value pairs passed in the previous page form to the corresponding bean instance: SayHello. jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%>

SayHello2.jsp
<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%><%@ page import=com.qsuron.study.* %>
Hello! <% = New String (man. getName (). getBytes (iso8859_1), UTF-8) %> <% = new String (man. getSex (). getBytes (iso8859_1), UTF-8) %>





Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog master Xiaoshu (qsuron)

 

 

 

 


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.