当前位置:网站首页>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 .
边栏推荐
- Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
- 布隆过滤器 课程研究报告
- Comprehensive evaluation of outline note taking software workflow: advantages, disadvantages and evaluation
- abnormal
- 浅谈小程序对传媒行业数字化的影响
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- Dbeaver installation and use tutorial (super detailed installation and use tutorial)
- Machine virtuelle 14 installer win7 (tutoriel)
- R language plot visualization: plot to visualize overlapping histograms, and use geom at the bottom edge of the histogram_ The rugfunction adds marginal rugplots
- 股票开户用中金证券经理发的开户二维码安全吗?知道的给说一下吧
猜你喜欢

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

Redis sentinel cluster main database failure data recovery ideas # yyds dry goods inventory #

Full link service tracking implementation scheme

bye! IE browser, this route edge continues to go on for IE

通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别

Dolphin scheduler uses system time

HDI的盲孔设计,你注意到这个细节了吗?

The concept of "tree structure" perfectly interprets the primary and secondary of things

Proxy mode (proxy)

组合模式(Composite Pattern)
随机推荐
Unity AssetBundle资源打包与资源加载
An error is reported when uninstalling Oracle
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
微信小程序开发日志
dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能
Dolphin scheduler uses system time
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
==And eqauls()
PMP考试重点总结六——图表整理
第三章 栈和队列
引入 flink-sql-mysql-cdc-2.2.1 好多依赖冲突,有解决的吗?
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
缓存之王Caffeine Cache,性能比Guava更强
Comprehensive evaluation of outline note taking software workflow: advantages, disadvantages and evaluation
异常处理4种方法
Custom exception classes and exercises
装饰模式(Decorator)
Explain final, finally, and finalize
【云驻共创】DWS告警服务DMS详细介绍和集群连接方式简介