当前位置:网站首页>Solve the problem that the value of the action attribute of the form is null when transferring parameters
Solve the problem that the value of the action attribute of the form is null when transferring parameters
2022-06-28 09:58:00 【11 brother sun】
One . Abnormal recurrence
lately Yige There is a student studying Servlet Conduct Web When developing , Try using the... In the form action Pass parameters , And he found out that Servlet The parameter value passed from the front end cannot be received in .
Let's take a look at his code first , The specific code is as follows :
1. front end Code Parameters
Here we use action Configure the back-end interface and passed parameters 1001.
<form action="/user?id=1001">
<input type="submit" value=" Submit " />
</form>2. backstage Servlet Code
Here is the corresponding backend Servlet Code , You can get... From the request parameters id The value of the parameter .
@WebServlet("/user")
public class MyServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
System.out.println(id);
}
}After the above code is executed , The final print result is : null
This student is very curious , Why? id The value of is null? Should not be 1001 Do you ? What's going on here ?

Two . abnormal reason
For such a result , The student expressed his doubts , Then why is the result null Well ? He never understood the reason , So I came to Yige Solve it for him . therefore Yige Just take his code , He carefully ruled it out .
adopt debug debugging , I find , If from url Direct access in http://localhost:8080/user?id=1001 when , You can receive the data from the front end . But when using form When transferring parameters from a form , The obtained value is null. thus it can be seen , The problem should be action Parameters above , It's not that our back-end code has a problem !
The reason why the above phenomenon occurs , This is because when action Reference time , The default is get Request mode ,get The request will action The parameter in is cleared , That is to say url The parameter in is cleared , When passing, it becomes http://localhost:8080/user? This form , therefore Servlet The corresponding request parameters cannot be obtained in the code .
3、 ... and . Solution
So how to solve this problem ? here Yige The following are given. 2 Specific solutions .
1. First option , We can Change the request mode in the form to post. because post A request passes parameters in the form of a request body ,url The parameters in will not be cleaned up .
2. Second option , We can Use hidden controls to pass parameters , At this time, whether it is get/post Requests can be .
here Yige Let's show you the code example of using hidden controls to pass parameters :
<form action="/user">
<input type="hidden" name="id" value="1001"/>
<input type="submit" value=" Submit " />
</form>Everyone should remember that , If we were to get Ask for a reference , Will take url Means of transmission ; If so post Ask for a reference , Will be id Parameters are passed to the background in the form of request body Servlet in .
Now you know how this problem arises 、 How to solve it ? If you have other questions , You can post the questions in the comment area , Yige I will give you a detailed answer when I see it .
边栏推荐
- 谁知道在中信建投证券开户是不是安全的
- Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
- PMP examination key summary VIII - monitoring process group (2)
- PMP Exam key summary VI - chart arrangement
- 线程和进程
- Full link service tracking implementation scheme
- abnormal
- 解决表单action属性传参时值为null的问题
- 适配器模式(Adapter)
- 结巴分词器_分词器原理
猜你喜欢
随机推荐
谁知道在中信建投证券开户是不是安全的
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)
第五章 树和二叉树
结巴分词器_分词器原理
Django数据库操作以及问题解决
纵观jBPM从jBPM3到jBPM5以及Activiti
卸载oracle报错
PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)
线程和进程
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
An error is reported when uninstalling Oracle
Inventory of excellent note taking software: good-looking and powerful visual note taking software, knowledge map tools heptabase, hydrogen map, walling, reflect, infranodus, tiddlywiki
Chapter 3 stack and queue
自定义异常类及练习
栈的弹出压入序列<难度系数>
Thread lifecycle
Dotnet uses crossgen2 to readytorun DLL to improve startup performance
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
bad zipfile offset (local header sig)
Caffeine cache, the king of cache, has stronger performance than guava








