JSP tutorial basics-HTML form demonstration

Source: Internet
Author: User

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:

 
  1. Dukebanner.html
  2.  
  3. <TableBorder="0" Width="400" Cellspacing="0" Cellpadding="0">
  4.  
  5. <Tr>
  6.  
  7. <TdHeight="150" Width="150"></ Td>
  8.  
  9. <TdWidth="250"></ Td>
  10.  
  11. </Tr>
  12.  
  13. <Tr>
  14.  
  15. <TdWidth="150"></ Td>
  16.  
  17. <TdAlign="Right" Width="250">
  18. Src="Duke.waving.gif"></ Td>
  19.  
  20. </Tr> </table> <br>
  21.  
  22. Main JSP file: hellouser. jsp
  23.  
  24. <%@ PageImport="Hello. NameHandler"%>
  25.  
  26. <Jsp: useBeanId="Mybean" Scope="Page" Class="Hello. NameHandler"/>
  27.  
  28. <Jsp: setPropertyName="Mybean" Property="*"/>
  29.  
  30. <Html>
  31.  
  32. <Head> <title> Hello, User </title>
  33.  
  34. <BodyBgcolor="# Ffffff" Background="Background.gif">
  35.  
  36. <%@ IncludeFile="Dukebanner.html"%>
  37.  
  38. <TableBorder="0" Width="700">
  39.  
  40. <Tr> <tdWidth="150"></ Td>
  41.  
  42. <TdWidth="550">
  43.  
  44. <H1> My name is Duke. What's yours? </H1> </td> </tr>
  45.  
  46. <Tr> <tdWidth="150"</Td> <tdWidth="550">
  47.  
  48. <FormMethod="Get">
  49.  
  50. <InputType="Text" Name="Username" Size="25">
  51.  
  52. <Br>
  53.  
  54. <InputType="Submit" Value="Submit">
  55.  
  56. <InputType="Reset" Value="Reset">
  57.  
  58. </Td> </tr>
  59.  
  60. </Form>
  61.  
  62. </Table>
  63.  
  64. <%
  65.  
  66. If (request. getParameter ("username ")! = Null)
  67.  
  68. {
  69.  
  70. %>
  71.  
  72. <%@ IncludeFile="Response. jsp"%>
  73.  
  74. <%}
  75.  
  76. %>
  77.  
  78. </Body>
  79.  
  80. Response file: response. jsp
  81.  
  82. <TableBorder="0" Width="700">
  83.  
  84. <Tr>
  85.  
  86. <TdWidth="150"></ Td>
  87.  
  88. <TdWidth="550">
  89.  
  90. <H1> Hello, <jsp: getPropertyName="Mybean" Property="Username"/>!
  91.  
  92. </H1>
  93.  
  94. </Td>
  95.  
  96. </Tr>
  97.  
  98. </Table>
  99.  
  100. Bean processing data :( namehandler. java)
  101.  
  102. Package hello;
  103.  
  104. Public class NameHandler
  105.  
  106. {
  107.  
  108. Private String username;
  109.  
  110. Public NameHandler (){
  111.  
  112. Username=Null;
  113.  
  114. }
  115.  
  116. Public void setUsername (String name ){
  117.  
  118. Username= Name;
  119.  
  120. }
  121.  
  122. Public String getUsername (){
  123.  
  124. Return username;
  125.  
  126. }
  127.  
  128. }

Create an HTML form

An HTML window is divided into three parts:

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.