HTML form for JSP tutorial
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?
First, the JSP engine sends the data stored in the request object to the JavaBeans component, servlet, or enterprise bean on the server 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, This JSP tutorial will show you a step:
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 use beans. 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 Label to create or call a ready-made Bean.
4. Add The tag sets the Bean attribute that requires the SET Method in the HTML form.
5. Add The tag sets 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 !"
JSP source code:
- Dukebanner.html
-
- <TableBorder="0" Width="400" Cellspacing="0" Cellpadding="0">
-
- <Tr>
-
- <TdHeight="150" Width="150"></ Td>
-
- <TdWidth="250"></ Td>
-
- </Tr>
-
- <Tr>
-
- <TdWidth="150"></ Td>
-
- <TdAlign="Right" Width="250">
- Src="Duke.waving.gif"></ Td>
-
- </Tr> </table> <br>
-
- Main JSP file: hellouser. jsp
-
- <%@ PageImport="Hello. NameHandler"%>
-
- <Jsp: useBeanId="Mybean" Scope="Page" Class="Hello. NameHandler"/>
-
- <Jsp: setPropertyName="Mybean" Property="*"/>
-
- <Html>
-
- <Head> <title> Hello, User </title>
-
- <BodyBgcolor="# Ffffff" Background="Background.gif">
-
- <%@ IncludeFile="Dukebanner.html"%>
-
- <TableBorder="0" Width="700">
-
- <Tr> <tdWidth="150"></ Td>
-
- <TdWidth="550">
-
- <H1> My name is Duke. What's yours? </H1> </td> </tr>
-
- <Tr> <tdWidth="150"</Td> <tdWidth="550">
-
- <FormMethod="Get">
-
- <InputType="Text" Name="Username" Size="25">
-
- <Br>
-
- <InputType="Submit" Value="Submit">
-
- <InputType="Reset" Value="Reset">
-
- </Td> </tr>
-
- </Form>
-
- </Table>
-
- <%
-
- If (request. getParameter ("username ")! = Null)
-
- {
-
- %>
-
- <%@ IncludeFile="Response. jsp"%>
-
- <%}
-
- %>
-
- </Body>
-
- Response file: response. jsp
-
- <TableBorder="0" Width="700">
-
- <Tr>
-
- <TdWidth="150"></ Td>
-
- <TdWidth="550">
-
- <H1> Hello, <jsp: getPropertyName="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: