Form. action: Value passing in form. action

Source: Internet
Author: User

Form. action: Value passing in form. action

Enter the url in the address bar of the browser and click? When passing parameters to request resources ,? The following parameter is called "query string", which triggers the background Servlet doGet (), because the method of direct access through the browser address bar is GET.


Next, let's take a look at the effect of form's method attribute on passing parameters.

Original Form:
<Form id = "myForm">
<Label> User name: </label>
<Input name = "username" type = "text">
<Input type = "submit" value = "submit">
</Form>


First case:
When the action attribute of the above form is not written, directly click the "Submit" button to trigger the form submission event. At this time, you can find through the Firefox console:
The message header contains the following key information: (1) the request address is followed '? 'Pass parameter (2) request method is GET

 




Parameter information: query string

 




When the method attribute of form is specified:
<Form id = "myForm" method = "post">
<Label> User name: </label>
<Input name = "username" type = "text">
<Input type = "submit" value = "submit">
</Form>

Case 2:
The message header contains the following key information: (1) the request address is not followed by any parameter (2) The request method is POST

 



Parameter information: form data

 




The above two methods both display the current page after refreshing. Because the form does not specify the action attribute, the default submitted address is the current page.

Now, you can access Servlet by specifying the action attribute to learn more about the method attribute.

Core source code of TestServlet. java:

@ WebServlet ("/servlet/TestServlet ")
Public class TestServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Response. getWriter (). print ("doGet ()");
}

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Response. getWriter (). print ("doPost ()");
}

}


Case 3: method = "get" action = "/test/servlet/TestServlet"

<Form id = "myForm" method = "get" action = "/test/servlet/TestServlet">
<Label> User name: </label>
<Input name = "username" type = "text">
<Input type = "submit" value = "submit">
</Form>

In the preceding message header, the parameters are the same as those in the first case, but the background returns a doGet ().

 





Case 4: method = "post" action = "/test/servlet/TestServlet"

<Form id = "myForm" method = "post" action = "/test/servlet/TestServlet">
<Label> User name: </label>
<Input name = "username" type = "text">
<Input type = "submit" value = "submit">
</Form>

In the preceding message header, the parameters are the same as those in the first case, but the background responds to a doPost ().

 





TestServlet. java:
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request. getParameter ("type ");
Response. getWriter (). print ("doGet () get type =" + param );
}

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request. getParameter ("type ");
Response. getWriter (). print ("doPost () get type =" + param );
}


In the fifth case, the method = "get" parameter action = "/servlet/TestServlet? Type = 01 ", and obtain parameters in the backend Servlet

In this case, the address bar of the browser is http: // localhost: 8080/test/servlet/TestServlet? Username =

 



The doGet () parameter in the background cannot be obtained.
At the same time, we found that the type parameter was not added to the "query string ".

 






In the sixth case, the method = "post" parameter action = "/servlet/TestServlet? Type = 01 ", and obtain parameters in the backend Servlet

 




We can find that the doPost () parameter in the background can obtain the type.

At the same time, the parameter column is somewhat different from the previous one: both "query string" and "form data"

 





So the above gives us a revelation, that is, through? When passing parameters after the form action, we need to manually specify the form method = "post", otherwise it cannot be obtained? The following parameter;
Misunderstanding: In the past, we passed parameters through url in the browser address bar? In the end, it also calls the doGet () method, so we think that in the form (default method = "get"), the action can be followed? To pass parameters.


Final reason: (my personal summary) When submitting form data in get mode, the url is reorganized. It only assembles form data of form into a "query string ", the url specified in the action submitted to form? Parameters passed in this method are not submitted because url reorganization is lost.

But when a form is submitted in post mode, it will convert the form data and? The following parameters are saved separately and submitted to the url specified by the action in the form.

(Refer to the resource) HTML Form: why action can't have get value in it?
Http://stackoverflow.com/questions/3548795/html-form-why-action-cant-have-get-value-in-it

 




 

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.