JSP getting started tutorial (3)

Source: Internet
Author: User

Lesson 2: Use HTML Forms

In most cases, commercial websites need to have some forms, such as entering the consumer's name, address, or keyword to search for them, or market personnel collect some data from visitors for reference.

How do I process the data returned from those forms?

The visitor enters data into the JSP engine through a form and stores the data in the request object. What should I do next?

Figure 2-1 shows how data streams are transmitted between the server and the customer (at least in SUN's JSP reference implementation). Other JSP engines may work a little differently, in fact, they are similar)



The word is too small, maybe not clear? I have to explain it.

First, the JSP engine sends the data stored in the request object to the server-side components (JavaBeans component, servlet, or enterprise bean) specified on the JSP page. After the component receives the data, it is possible to store the data in the database or other places, and return a response object to the JSP Engine. The JSP Engine then transmits the response object to the JSP page. The page contains the defined format and data obtained from the server. At this time, the JSP engine and the Web server send a complete page to the customer, that is, the results we see on the browser. The communication protocol between the customer and the server can use HTTP, of course, other.

The Request and Response objects play a role in the original JSP code you created. How to Use the request object? I will explain it to you in detail later.

How to create a form

Use HTML to define some representative forms into a JSP file, and then use JSP labels to transmit data in the form and server-side objects (usually Bean. This is generally the case:

1. Write the original JSP file, create some HTML forms, and name them.

2. Write beans, define attributes, and GET or SET methods in the Java file to work with a form with a name already specified by you.

3. Return to the original JSP file and add the <jsp: useBean> label to create or call a ready-made Bean.

4. Add the <jsp: setProperty> label to SET the Bean attribute of the SET Method in the HTML form.

5. Add the <jsp: getProperty> label to set the Bean attributes of the GET method in the HTML form.

6. If you need to process more user data, use the request object.

After half a day, you may not be able to understand it. In fact, you can understand it by looking at an example.

Let's take a look at a simple hello example:

This program is actually the most classic "hello, world" program in a computer program. However, I scratched him a little and made him look intelligent and complex. Enter your name first, and Duke will say "hello!" to you !"





Let's look at the Code:

Dukebanner.html

<Table border = "0" width = "400" cellspacing = "0" cellpadding = "0">

<Tr>

<Td height = "150" width = "150"> </td>

<Td width = "250"> </td>

</Tr>

<Tr>

<Td width = "150"> </td>

& Lt; td align = "right" width = "250" & gt;
</td>

</Tr> </table> <br>

Main JSP file: hellouser. jsp

<% @ Page import = "hello. NameHandler" %>

<Jsp: useBean id = "mybean" scope = "page" class = "hello. NameHandler"/>

<Jsp: setProperty name = "mybean" property = "*"/>

<Html>

<Head> <title> Hello, User </title>
<Body bgcolor = "# ffffff" background = "background.gif">

<% @ Include file = "dukebanner.html" %>

<Table border = "0" width = "700">

<Tr> <td width = "150" type = "codeph" text = "/codeph"> </td type = "codeph" text = "/codeph">

& Lt; td width = "550" & gt;

<H1> My name is Duke. What's yours? </H1> </td> </tr>

<Tr> <td width = "150" </td> <td width = "550">

<Form method = "get">

<Input type = "text" name = "username" size = "25">

<Br>

<Input type = "submit" value = "Submit">

<Input type = "reset" value = "Reset">

</Td> </tr>

</Form>

</Table>

<%

If (request. getParameter ("username ")! = Null)

{

%>

<% @ Include file = "response. jsp" %>

<%}

%>

</Body>
Response file: response. jsp

<Table border = "0" width = "700">

<Tr>

<Td width = "150"> </td>

& Lt; td width = "550" & gt;

<H1> Hello, <jsp: getProperty name = "mybean" property = "username"/>!

</H1>

</Td>

</Tr>

</Table>

Bean processing data :( namehandler. java)

Package hello;

Public class NameHandler

{

Private String username;

Public NameHandler (){

Username = null;

}

Public void setUsername (String name ){

Username = name;

}

Public String getUsername (){

Return username;

}

}

Create an HTML form

An HTML window is divided into three parts: <form> tag, input method, and submit button to send data to the server. <Form method = get action = someurl>, the action attribute on other pages may be other special CGI programs or other programs that can process data. How can this attribute be used in JSP, if you want to send data to Bean, You can omit the dongli in action and directly write the <jsp: useBean> tag or other specific JSP files. The following forms are similar to common HTML. The <input> method is added with a submit button. There may be a Reset button, right? Don't forget, you must add a name to each input form.
Write this statement: <input type = "text" name = "username">

Use the GET and POST Methods

The GET and POST methods can be used to send data to the server. In JSP programs, the GET and POST methods can be used to send data to beans, Servlets, or other server-side components.

Theoretically, GET requests data from the server, and POST requests data to the server. In fact, the GET method adds the data parameter Queue (query string) to a URL, and the values correspond to the form one by one. For example, name = John. In the queue, values and forms are separated by an & symbol, spaces are replaced by a + sign, and special symbols are converted into hexadecimal code. Because this queue is in the URL, the queue parameters can be viewed, recorded, or changed. The GET method also limits the character size. In fact, the POST method can transmit data to the server without time restrictions, and the user cannot see this process on the browser side. Therefore, the POST method is suitable for sending a confidential (such as a credit card number) or a large amount of data is sent to the server.

Write Bean

If the JSP program uses Bean, you have to design your Bean according to the JavaBeans API instructions.

Remember the following two key parts.

If the JSP program uses the <jsp: getProperty> tag, you must use the GET method in the Bean.

If the JSP program then uses the <jsp: setProperty> tag, you have to combine the Set Method in the Bean.

Setting parameters to Bean or getting parameters from inside will be detailed in later sections.



Transmit data to Bean
It takes two steps to upload HTML form data to Bean:

· Use the <jsp: useBean> label to create or locate the Bean

· Use <jsp: serProperty> in Bean to set the property value

The first step is to use the <jsp: useBean> tag to create or locate the Bean before <jsp: setProperty>. <jsp: useBean> first, search for the Bean according to the name you specified, if not found, you will be assigned. A Bean can be created in a JSP file and called in another file. This gives the Bean a wide range of runtime space.

Step 2: Use <jsp: setProperty> in Bean to set the property value. The simplest way is to define the value to work with the form name. For example, if you define a form name as "username", you define the attribute "username" in Bean and then use the getUsername and setUsername methods.

You can also define different names, as long as you don't think it is troublesome. Who makes your memory good!

Request object
The data entered by the user is stored in the Request object, and javax. servlet. httpServletRequest (you can also execute it using different tools, but they are actually javax. servlet. subset of HttpServletRequest)

You can also directly use scriptlet to directly access the Request object. Scriptlet will be discussed in detail in the next lecture. Now you only need to know that it is enough to write a piece of code written in the scripting language between <% and %>. In JSP 1.0, you must use JavaTM as your scripting language.
You often use the following method to process Request objects:

Method
Description
Execution result

GetRequest
Javax. servlet. jsp. PageContext
Returns the current Request object.

GetParameterNames
Javax. servlet. ServletRequest
Returns the parameter name of the current Request object.

GetParameterValues
Javax. servlet. ServletRequest
Returns the parameter value of the current Request object.


You will find other methods include ServletRequest, HttpServletRequest, or any subset of other ServletRequest.

The JSP Engine often uses the Request object after scenes, even if you do not explicitly call it in the JSP file.

Call data from Bean to JSP page
Once user data is transferred to Bean, you want to obtain the data again and display it on the JSP page. To achieve this, you need to use the <jsp: getProperty> tag. Pass Bean name and attribute name:

<H1> Hello, <jsp: getProperty name = "mybean" property = "username"/>!

<Jsp: useBean>, <jsp: setProperty>, must match the <jsp: getProperty> tag. For example:

Hellouser. jsp:
<Jsp: useBean id = "mybean" scope = "session" class = "hello. NameHandler"/>
<Jsp: setProperty name = "mybean" property = "*"/>
Response. jsp:
<H1> Hello, <jsp: getProperty name = "mybean" property = "username"/>!

In this example, labels are placed in two files, but the specified names are the same. If they are different, the system returns an error message.

Example
I am using a UNIX host. If you are using windows, change the corresponding path.

Create path ../jswdk-1.0/examples/jsp/tutorial/hellouser.

Put the background.gif, duke.waving.gif, dukebanner.html, hellousr. jsp, and response. jsp files.

Create a directory ../jswdk-1.0/examples/WEB-INF/jsp/beans/hello

Put the NameHandler. java and NameHandler. class files in.

Cd ../jswdk-1.0 then startserver

Open the browser http: // computer name: 8080/examples/jsp/tutorial/hellouser. jsp

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.