JSP Fifth Chapter

Source: Internet
Author: User

Using Java Bean Component features: It is possible to reuse anywhere, including the ability to use in applications, other components, documents,
Reuse the Web site and application Builder tools in a variety of scenarios.

The concept of JavaBean
JavaBean is a platform-and architecture-independent application interface that can itself ignore internal
The structure and details of the problem, just define its external features and external functions on the line.

It has three interface surfaces that can be developed independently of each other: properties, methods, and events
Properties of the JavaBean


There are two types of properties for a JSP component: single-valued, indexed properties
Single-Valued Properties:
Equivalent to a member variable defined in programming, which can have only one data value, the type of data value may be any data type in Java, including classes and interfaces.
Such as:
int counter;
Float tax;
String name;
Date datetime;
Note: A special single-valued attribute (Boolean attribute) that has only two allowable values: true, False
Indexed properties:

Equivalent to the usual said array, when storing a batch of the same type of data, you need to use the index attribute.
such as: int id[];
String name[];
Float score[];
Methods of JavaBean
Methods include construction methods and access methods.
Construction Method:


Like a constructor in a class (that is, initializing it and its member variables), its method name is the same as this JavaBean class. Such as:
Package student;
public class student{
int counter;
IND id[];
String name[];
float score[];
Boolean whiszkid;

Public student () {
Id=new INT[50];
Name=new STRING[50];
Score=new FLOAT[50];
counter=0;
}
}


Access method: is to access the properties defined in the component, including read and write (the general situation is similar to the method defined and used in Java), pay attention to its naming rules:
Read an attribute: GetXXX ()
Overwrite an attribute: Setxxx ()

Access single-valued properties: For example, the counter single-valued attribute above, its two access methods can be:
Read Property value:
public int getcounter{
return counter;
}
Overwrite property values:
public void Setcounter (int counter) {
This.counter=counter;
}

Access Boolean single-valued properties: Use the Isxxx () method for reading attribute values
For example, the above Boolean property Whizkid Access can be:
public Boolean iswhizkid () {
return whizkid;
}

Accessing indexed properties: Sometimes you have to define several access methods, but their naming rules are the same,
For the above name attribute, its reading and rewriting methods need to be defined as follows:
Read one of the property values in the indexed property:
Public String getName (int index) {
return Name[index];
}

Read all the property values in the indexed property:
Public string[] GetName () {
return name;
}

Overwrite one of the attribute values in an indexed property:
public void setName (int index,string str) {
NAME[INDEX]=STR;
}

Overwrite all property values in the indexed property:
public void SetName (string[] str) {
NAME=STR;;
}


Creating a bean written in JavaBean in a JSP is actually creating an object of this class.

Client-to-server work



Client-Done work


The client's work can be divided into two parts:
First, send a request to the server to send a submission to the server side
Second, the client to verify, as well as the page decoration and other functions


Methods submitted by the client
There are two ways to submit a client: Get vs. post


Get can pass information only about 2KB, and is a plaintext transmission of information, all the information can be seen in the address bar.

Post can transmit a lot of information, encrypted transmission, all the information can be in the address bar is not visible.

Post Submission
All post submissions can only be generated in a form and are submitted in two different ways: direct submission (submit submission), indirect submission (script submission)

1) Submit directly: Click submit button to commit immediately
<form Method=post action=*.asp>
<input type=submit value= "register" >
Note: The Submit button has a default event, which is to submit
<input type=submit value= "register" onclick= "alert (' Hello '); return ture or false;" >
Note: Before submitting, first return a warning box "hello" on the screen, if return is true, then the page commits, and if return is false, the page will not commit.
</form>

2) Indirect submission: Refers to the click of the button itself is not able to complete the submission, and only through the script to complete the submission, called indirect submission.
<input Type=button value= "registered" onclick= "check (); submit ();" >
Note:
OnMouseDown, onmouseup and other event names can also be found at the onclick
The button is simply a normal pushbutton and cannot be submitted by itself, only through the script specified by the onclick to complete the submission
After clicking "Register", the function of the onclick event is executed first, and if the return value is True the commit occurs, otherwise the commit is aborted.


Get Commit
Get commits occur in form forms and URLs

1) generated in the form
<form Method=get action=*.asp>

2) generated in all URLs
Enter the URL address in the address bar, followed by the parameter to be passed HTTP://*.ASP?NAME=WWW&AMP;AGE=20

On the hyperlink <a href= "*.asp>mode=1&grade=1" >click me </a>

In the SRC attribute of the picture

In the SRC attribute of the frame <frame src= "*.asp?name=www" Name= frame name >

Response.Redirect "*.asp?name=www" in ASP redirection statements

In client object Document: document.location= "/http *.asp?name=www"

In the Client object window: window.open ("/http/*.asp?name=www")

Server-side work
The method to obtain client information is done through the request object.


The information that the client submits to the server side is divided into three categories:
First Class: Client information (including the client's IP address, port number, hostname, method of submission, etc.)
Type II: Submission to server-side information via form or URL
Category III: Cookie information
To obtain the first type of information by:


Request.getheader ("User-agent") returns the version number, type of the client browser

GetHeader (String name): Gets the transfer header information defined by the HTTP protocol,

Request. GetMethod (): Get, POST, put, etc. for client to send data to server side

Request. Getrequesturi (): Gets the client address that issued the request string

Request. Getservletpath (): Gets the file path of the script file requested by the client

Request. getServerName (): Get the name of the server

Request.getserverport (): Get the port number of the server

REQUEST.GETREMOTEADDR (): Obtain the IP address of the client

Request.getremotehost (): Gets the name of the client computer and, if it fails, returns the IP address of the client computer

Request.getprotocol ():

Request.getheadernames (): Returns the name of all request headers, which is an instance of a enumeration (enum) class

Request.getheaders (String name): Returns all values of the request header for the specified name, which is an instance of the enumeration (enum) class


Get the second type of get information by:


Request. GetQueryString ("Variable name or control name") gets the query string, which is sent by the client to the server side by the Get method

Get the second type of post information by:

Request.getparameter (String name): Gets the parameter value that the client sends to the server side, which is specified by name

Request.getparameternames (): Gets all the parameter names that the client sends to the server side, and the result set is an instance of the enumeration (enum) class

Request.getparametervalues (String name): Gets all values for the specified parameter


To obtain the third type of cookie information by:


Request.getcookies ("Cookie variable name"): Returns the cookie object of the client, resulting in a cookie array

JSP Fifth Chapter

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.