JSP 中的 request.getParameter() 无法访问 Servlet 中 request.setAttribute() 设置的值 [英] Value set by request.setAttribute() in Servlet is not accessible by request.getParameter() in JSP

查看:44
本文介绍了JSP 中的 request.getParameter() 无法访问 Servlet 中 request.setAttribute() 设置的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我禁止在单击表单按钮后向控制器 servlet 发送请求,它需要检查用户是否已登录以及按下了哪个按钮.

I'm forwording a sending a request after a click on a form button to a controller servlet and it needs to check if the user is logged in and which button was pressed.

第一部分工作正常,但是当我尝试将结果设置为参数并将请求转发到 JSP 页面时,参数按钮"始终返回 null.有人能帮我理解为什么会这样吗?也许有更好的方法来做到这一点?

The first part works fine but when im tring to set the result as a parameter and forward the request the JSP page ,the parameter "Button" always returns null. Can someone help me understand why this is happening? maybe there is a better way to do this?

服务端:

request.setAttribute("Buttons", "Add a new coupon");
request.getRequestDispatcher("/admin_main.jsp").forward(request,response);

JSP:

if (request.getParameter("Buttons") != null) {
    // ...
} 

推荐答案

if(request.getAttribute("Buttons") != null) 会起作用.

信息:

始终执行 request.getParameter() 以提取请求参数(即通过发布 html 表单发送的数据).request.getParameter() 总是返回 String 值,数据来自客户端.

Always do a request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String value and the data come from client.

始终使用 request.getAttribute() 将对象添加到服务器端的请求范围,即使用 request.setAttribute().

Always use request.getAttribute() to get an object added to the request scope on the server side i.e. using request.setAttribute().

这篇关于JSP 中的 request.getParameter() 无法访问 Servlet 中 request.setAttribute() 设置的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆