1. Why you need to use HTML forms
In the development of Dynamic Web pages, HTML forms are the main means of interacting information with users.
2. HTML form
A form should at least include descriptive text, form controls, submissions, and refilling. The primary mark of a form is <form> it has two important parameters: action and method. The action represents the location where the form is to be submitted, and method means that the form is sent in two ways: get (the default method) and post.
<fotm name= "Form1" method= "Post" action= "xinxin.jsp" >
2.1, get and post delivery mode
The Post:post method sends the contents of the form over HTTP, does not see the form's submit information in the address bar, is safe, and the Post method is used to send the message without the character length limit.
Get mode: The form content is encoded, sent through the URL, you can see the form information in the address bar, unsafe. A 255-character limit is used to send information using the Get method. The page for a GET request can be set as a bookmark or sent by mail, and the post cannot be used.
When 2.1.1, get method commits, garbled problem solves:
String sex =request.getparameter ("xinxin");
String tempsex = new String (Sex.getbytes ("iso-8859-1"));
......
2, form processing of the programming mode
This is typically a pattern: first, users enter and submit information through form controls. Next, the JSP page obtains the form data, carries on the logic processing. Finally, the JSP page shifts to different results pages based on the processing results.
3, JSP built-in objects
Built-in objects: A built-in object is an instance of a set of classes loaded by a Web container that does not use "new" as a common Java object to get an instance, but rather an object that can be used directly on a JSP page.
3.1. JSP built-in object out
Out objects are used to output data on the client, and the method used by Out objects is print (), which prints out string information in the page.
<%
Out.print ("Hi. My name New. ”);
%>
3.2. JSP built-in object request
The request object is primarily used to process client requests, which can be obtained by calling the Request object's method in the JSP page, which contains information about the browser request.
Several common methods of request object
Method name |
Description |
String GetParameter (string name) |
Get page submission data based on page form component name |
String[] Getparametervalues (String name) |
Gets the request data for a user when a page form component corresponds to multiple values. For example, gets the value of more than one check box. |
void Setcharacterencoding (String charset) |
Specifies the encoding of each request, which is set before calling the Request.getparameter () method, and can be used to solve the garbled Chinese problem. |
Request.getrequestdispatcher (String Path) |
Returns a Javax.servlet.RequestDispatchar object that is used by the forward method to forward requests. |
Page Jump
Request.getrequestdispatcher ("success.jsp"). Forward (request, response);
3.2.1,
<%
Request.. Setcharacterencoding ("GBK");
String name = Request.getparameter ("name");
String pwd = request.getparameter ("pwd");
String [] channels = Request.getparametervalues ("channel");
%>
The GetParameter () method of the request can obtain the value of the parameter that was submitted by the previous page.
Channel the names of all check boxes, the Getparametervalues ("channel") method gets an array that stores the values for all the selected check boxes.
The Setcharacterencoding () method is used to specify the encoding for each request.
3.3, JSP built-in object response
The response object corresponds to the Request object, which responds to customer requests and outputs information to the client.
Response Common method: void Sendredirect (String location). This method is used to redirect the request to a different URL.
For example:
Response.sendredirect ("xinxin.jsp");
Above, the client re-establish the link and the URL address has changed.
When you use the Getrequestdispatcher () method of the request to make a page jump, the client does not re-establish a new link and the client's URL address does not change.