问题
Lets say I have a form with method=POST on my page. Now this form has some basic form elements like textbox, checkbox, etc It has action URL as http://example.com/someAction.do?param=value
I do understand that this is actually a contradictory thing to do, but my question is will it work in practice.
So my questions are;
Since the form method is POST and I have a querystring as well in my URL (?param=value) Will it work correctly? i.e. will I be able to retrieve param=value on my receiving page (someAction.do)
Lets say I use Java/JSP to access the values on server side. So what is the way to get the values on server side ? Is the syntax same to access value of param=value as well as for the form elements like textbox/radio button/checkbox, etc ?
回答1:
1) YES, you will have access to POST and GET variables since your request will contain both. So you can use $_GET["param_name"] and $_POST["param_name"] accordingly.
2) Using JSP you can use the following code for both:
<%= request.getParameter("param_name") %>
If you're using EL (JSP Expression Language), you can also get them in the following way:
${param.param_name}
EDIT: if the param_name
is present in both the request QueryString and POST data, both of them will be returned as an array of values, the first one being the QueryString.
In such scenarios, getParameter("param_name)
would return the first one of them (as explained here), however both of them can be read using the getParameterValues("param_name")
method in the following way:
String[] values = request.getParameterValues("param_name");
For further info, read here.
回答2:
Yes. You can retrieve these parameters in your action class. Just you have to make property of same name (param in your case) with there getters and setters.
Sample Code
private String param;
{... getters and setters ...}
when you will do this, the parameters value (passed via URL) will get saved into the getters of that particular property. and through this, you can do whatever you want with that value.
回答3:
The POST
method just hide the submitted form data from the user. He/she can't see what data has been sent to the server, unless a special tool is used.
The GET
method allows anybody to see what data it has. You can easily see the data from the URL (ex. By seeing the key-value pairs in the query string).
In other words it is up to you to show the (maybe unimportant) data to the user by using query string in the form action. For example in a data table filter. To keep the current pagination state, you can use domain.com/path.do?page=3
as an action
. And you can hide the other data within the form components, like input
, textarea
, etc.
Both methods can be catched in the server with the same way. For example in Java, by using request.getParameter("page")
.
来源:https://stackoverflow.com/questions/10310684/html-form-post-method-with-querystring-in-action-url