search
HomeWeb Front-endHTML TutorialForm.action value transfer problem

When you enter the url through the browser address bar and request resources through ?, the parameter after ? is called the "query string", which will trigger the doGet() of the background Servlet, because the direct access method through the browser address bar is the GET method .


Let’s take a look at the impact of form’s method attribute on parameter passing.

Original form:








The first case:
When the action attribute of the form above is not written, directly clicking the "Submit" button will trigger the submit event of the form. This At this time, you can find through the console of Firefox browser:
The message header contains the following key information: (1) The request address is followed by '?' to pass parameters (2) The request method is GET




Parameter information: Query string




When specifying the method attribute of the form:







Second case:
The message header contains the following key information: (1) Behind the request address No parameters are passed (2) The request method is POST



Parameter information: Form data




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

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

TestServlet.java core source code:

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

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Servlet Exception , IOException {
response.getWriter().print("doGet()");
}

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

}


The third case: method="get" action="/test/servlet/TestServlet"







The above message header and parameters are the same as the first case, but the background responds with a doGet().





The fourth case: method="post" action="/test/servlet/TestServlet"







The above message header and parameters are the same as the first case, but the background responds with a doPost().





TestServlet.java Add the code to get parameters:
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);
}


The fifth case, method="get" passes the parameter action = "/servlet/TestServlet?type=01" after the action, and at the same time Get the parameters in the background Servlet

The browser address bar at this time: http://localhost:8080/test/servlet/TestServlet?username=



It can be found that the doGet() in the background cannot obtain the parameter type.
At the same time, it is found that the type parameter is not added to the "query string"






The sixth case, method="post" passes the parameter action = "/servlet/TestServlet?type=01" after the action, and obtains the parameters in the background Servlet at the same time




You can find that the doPost() in the background can get the parameter type.
At the same time, the parameter column is a little different from before:
There is both "query string" and "form data"




So the above gives us a revelation, that is, when passing parameters after the action of the form, we need to manually specify the method="post" of the form, otherwise we will not be able to get the parameters behind the ?;
Thinking Misunderstanding: Previously we passed parameters through the URL in the browser address bar, also through? To achieve this, it ultimately calls the doGet() method, so we think that the action in the form (default method="get") can be passed? Come and pass on the ginseng.


Final reason: (Personal summary) When submitting form data in the get method, the URL will be reorganized. It will only assemble the form data into a "query string" and submit it to the URL specified in the action of the form, so the original The parameters passed in the ? method will not be submitted because the URL is lost after reorganization.

But when the form is submitted in post mode, it will save the form data and the parameters after ? separately, and submit them together to the url specified by the action in the form.

(reference 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




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

What problems will you encounter when using native select on your phone?What problems will you encounter when using native select on your phone?Apr 30, 2025 pm 03:06 PM

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!