In most cases, a business site will have a list of forms, such as entering a consumer's name, an address, or a word to check with a search engine, or a marketing person collecting data from visitors for reference.
What about the data from those watches conveys back?
The visitor enters the data in the form of a one-way JSP engine and is saved in the Request object, so what happens next?
Figure 2-1 shows you how the data flow is passed between the server and the customer (at least in the Sun's JSP reference implementation), and the other JSP engines may be a little bit different, in fact, similar to each other.
The words are too small, may not see clearly? I'll explain it.
First, the JSP engine sends the data stored in the request object to the server-side component (JavaBeans component, servlet, or Enterprise bean) specified by the JSP page, and after the component receives the data, It is possible to store this data in a database or other place, while returning a response object to the JSP engine. The JSP engine then passes the response object to the JSP page, where the page contains the defined format and the data obtained from the server side. Then the JSP engine and the Web server send a complete page to the customer, which is the result of what they see in the browser. The communication protocol between client and server can be used HTTP, and of course, it can be used in other.
The request and Response objects work in your JSP's original code. To the request object how to use, I want to tell you in detail in the next.
How do I create a form
Use HTML to define some representative forms as a JSP file, and then use the JSP tag to pass data between the form and the server-side objects (usually beans). In general, this is the case:
1, write JSP original file, create some HTML form and name.
2. Write beans in Java files, define attributes, get, or set methods to match a form that has already been named by you.
3, back to the original JSP file, add <jsp:useBean> tag to create a or call a ready-made bean.
4, add <jsp:setProperty> label settings The properties of the bean in the HTML form that require the Set method.
5, add <jsp:getProperty> label settings The properties of the bean that requires the Get method in the HTML form.
6. If you need to process more user data, use the Request object.
Say for a long time you may not understand, actually see an example you understand.
Let's look at a simple example of hello:
This program is actually a computer program in the most classic "Hello,world" program, but I made him a little corner, make him look more intelligent and complex. First you type your name, and Duke says to you, "hello!."
An HTML window is divided into three parts:<form> tags, input method, submit button to send data to the server. General HTML page, is so written <form Method=get Action=someurl> The action attribute on other pages may be other special CGI programs or other programs that can process data, so how do you use it in the JSP? Well, if you want to send the data to the bean, then you can omit the action inside the east, directly write <jsp:useBean> tags or other specific JSP files. The next ones are about the same as the normal HTML,<input>, then add a Submit button, there may be a reset button, and, by the way, don't forget to add a name to each input form.
So write: <input type= "text" name= "username" >
Using the Get and post methods
You can send data to the server using the Get and post methods, and the Get and post methods in the JSP program can send data to the bean, servlet, or other server-side components.
In theory, get is requesting data from the server, and post is sending data to the server. In fact, the Get method adds the data parameter queue (query string) to a URL, and the value corresponds to the form one by one. For example, Name=john. In the queue, values and tables are separated by A & symbol, the spaces are replaced with the + number, and special symbols are converted into 16 code. Because this queue is inside the URL, the parameters of the queue can be seen, can be recorded, or changed. Usually the Get method also restricts the size of the characters. In fact, the post method can pass data to the server without time limit, the user cannot see this process in the browser, so the post method is more suitable for sending a confidential (such as a credit card number) or a larger amount of data to the server.
Write Bean
If the JSP program uses beans, you have to design your bean according to the JavaBeans API instructions.
Remember the following two key sections.
If you use the <jsp:getProperty> tag in your JSP program, you have to match the Get method inside the bean.
If the JSP program then uses the <jsp:setProperty> tag, then you have to match the set method inside the bean.
Setting parameters to the bean or taking parameters from inside will be described in detail in a later section.
Pass data to Bean
It takes two jobs to upload data from an HTML form to a bean:
· Create or navigate to beans with <jsp:useBean> tags
· Use <jsp:serProperty> to set property values inside beans
The first step is to use <jsp:useBean> tags to create or navigate to the bean must be used before <jsp:setProperty>,<jsp:usebean> first find the bean according to the name you specify, if it is not found, will give you a designation. Allows you to create a bean in one JSP file and then call it in another file, which gives the bean a very wide range of running space.
The second step is to set the property value in the Bean with <jsp:setProperty>. The simplest method is to define the value to match the table Single-name. For example, if you define a table Single-name as "username" then you define the attribute "username" in the bean and then use the method GetUserName and Setusername.
Of course can also be defined as different names, as long as you do not think the trouble. Who gives you a good memory?
Request Object
The data entered by the user is stored in the request object. Execute with javax.servlet.HttpServletRequest (you can do it with a different tool, but they are all subsets of Javax.servlet.HttpServletRequest)
You can also use Scriptlet directly to access the request object directly. Scriptlet will discuss it in detail in the next lecture, and now you just need to know that he wrote in a scripting language a piece of code between <% and%> is enough. In JSP 1.0, you must use the JAVATM program language as your scripting language.
You will often use the following methods to process the request object:
Method
Description
Execution results
Getrequest
Javax.servlet.jsp.PageContext
Returns the current Request object
Getparameternames
Javax.servlet.ServletRequest
Returns the name of the current request object parameter
Getparametervalues
Javax.servlet.ServletRequest
Returns the current request object parameter value
You will find that other methods include a subset of Servletrequest,httpservletrequest or any other servletrequest.
The JSP engine often uses the request object after scenes, even if you don't explicitly call it in the JSP file.
To tune the data from the bean to the JSP page
Once the user's data is uploaded to the bean, you want to get the data back, and then display it in the JSP surface page. To achieve this, you have to use the <jsp:getProperty> tag. Pass bean Name and attribute name:
<jsp:usebean>, <jsp:setproperty>, and <jsp:getProperty> labels must match, for example:
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.