当前位置:网站首页>解决表单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中。
现在你知道这个问题是怎么产生、怎么解决的吗?如果你还有其他问题,可以将问题在评论区贴出来,壹哥看到就会给你详细解答哦。
边栏推荐
- 线程和进程
- June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 is equal and the number of 0 is equal in the two intervals. The two intervals can intersect bu
- JSON数据与List集合之间的正确转换
- PHP curl forged IP address and header information code instance - Alibaba cloud
- Bridge mode
- 纵观jBPM从jBPM3到jBPM5以及Activiti
- PMP needs to master its own learning methods
- JVM family (2) - garbage collection
- 布隆过滤器 课程研究报告
- Do static code blocks always execute first? The pattern is smaller!!!
猜你喜欢
![QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])](/img/17/57ffb7393b71eddc5ac92ae3944338.jpg)
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])

组合模式(Composite Pattern)

How to reduce the risk of project communication?

满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”

Numpy array: join, flatten, and add dimensions

老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码

1181: integer parity sort
![1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation

JVM family (2) - garbage collection

How to view the web password saved by Google browser
随机推荐
详解final、finally和finalize
异常处理4种方法
云服务器MYSQL查询速度慢
手机号、邮箱正则验证[通俗易懂]
Inventory of excellent note taking software: good-looking and powerful visual note taking software, knowledge map tools heptabase, hydrogen map, walling, reflect, infranodus, tiddlywiki
On the influence of small program on the digitalization of media industry
[ybtoj advanced training guide] maximum separation [hash] [Floyd]
再见!IE浏览器,这条路由Edge替IE继续走下去
Sword finger offer | linked list transpose
abnormal
再見!IE瀏覽器,這條路由Edge替IE繼續走下去
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
异常的产生,及解决
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
PMP Exam key summary IX - closing
谁知道在中信建投证券开户是不是安全的
装饰模式(Decorator)
Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app
理想中的接口自动化项目