当前位置:网站首页>解决表单action属性传参时值为null的问题
解决表单action属性传参时值为null的问题
2022-06-28 09:40:00 【一一哥Sun】
一. 异常重现
最近壹哥有个学生在学习Servlet进行Web开发时,尝试着使用表单中的action传递参数,结果他发现在Servlet中无法接收到前端传过来的参数值。
我们先来看看他的代码,具体代码如下所示:
1. 前端代码参数
这里用action配置了后端接口及传递的参数1001。
<form action="/user?id=1001">
<input type="submit" value="提交" />
</form>
2. 后台Servlet代码
这里是对应的后端Servlet代码,可以从请求参数中获取id参数的值。
@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);
}
}
上面的代码执行之后,最终的打印结果是: null
这个学生就很好奇,为什么id的值是null?不应该是1001吗?这是怎么回事呢?
二. 异常原因
对于这样的结果,这个同学表示很疑惑,那么结果为什么是null呢?他始终想不明白原因,于是就来找壹哥给他解决。于是壹哥就拿来他的代码,给他仔细排除了起来。
通过debug调试,我发现,如果是从url中直接访问http://localhost:8080/user?id=1001时,则可以接收到前端传来的数据。但当利用form表单传参时,获取的值却是null。由此可见,问题应该出在action参数上面,而不是我们的后端代码有问题!
之所以会出现上述现象,这是因为当action传参时,默认用的是get请求方式,get请求会将action中的参数给清除掉,也就是url中的参数被清除掉了,传递时变成了http://localhost:8080/user? 这种形式,所以Servlet代码中自然就无法获取到对应的请求参数了。
三. 解决方案
那么这个问题该怎么解决呢?这里壹哥给出如下2种具体的解决方案。
1. 第一种方案,我们可以将表单中的请求方式改为post。因为post请求是以请求体的方式传递参数,url中的参数不会被清理。
2. 第二种方案,我们可以使用隐藏控件传参,这时无论是get/post请求都可以。
这里壹哥就给大家展示使用隐藏控件传参的代码示例:
<form action="/user">
<input type="hidden" name="id" value="1001"/>
<input type="submit" value="提交" />
</form>
大家以后要记住,如果我们是以get请求传参,则会以url方式传递;如果是以post请求传参,则会将id参数以请求体的方式传到后台Servlet中。
现在你知道这个问题是怎么产生、怎么解决的吗?如果你还有其他问题,可以将问题在评论区贴出来,壹哥看到就会给你详细解答哦。
边栏推荐
- 如何查看谷歌浏览器保存的网页密码
- The private attribute of this class can be used directly? New() in use!!!
- PMP Exam key summary IX - closing
- mysql打不开,闪退
- Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
- The concept of "tree structure" perfectly interprets the primary and secondary of things
- Caffeine cache, the king of cache, has stronger performance than guava
- Huawei OSPF single region
- 六月集训(第28天) —— 动态规划
- The constructor is never executed immediately after new()!!!!!
猜你喜欢
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Installing redis under Linux and windows (ultra detailed graphic tutorial)
通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
详解final、finally和finalize
Numpy array: join, flatten, and add dimensions
Instant messaging and BS architecture simulation of TCP practical cases
数字人行业爆发在即,市场格局几何?
Matplotlib attribute and annotation
PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)
随机推荐
JSON数据与List集合之间的正确转换
Key summary V of PMP examination - execution process group
卸载oracle报错
桥接模式(Bridge)
RESTful风格
线程的生命周期
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
组合模式(Composite Pattern)
On the influence of small program on the digitalization of media industry
01-分布式系统概述
Summary of PMP learning experience
老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
Matplotlib attribute and annotation
Installing redis under Linux and windows (ultra detailed graphic tutorial)
JDBC connection database (MySQL) steps
PMP Exam key summary VI - chart arrangement
Importerror: no module named image [duplicate] - importerror: no module named image [duplicate]
What is online account opening? Is it safe to open an account online now?