看重了request.setAttribute()在Servlet的设置是不是的request.getParameter()在JSP访问 [英] Value set by request.setAttribute() in Servlet is not accessible by request.getParameter() in JSP

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

问题描述

我forwording上的表单按钮点击到一个控制器servlet后发送请求,它需要检查,如果用户登录和哪个按钮是pressed。

第一部分工作正常,但当IM特林设置结果作为参数,并转发请求的JSP页面,参数按钮始终返回null。
有人可以帮助我了解为什么发生这种情况?也许有更好的方法来做到这一点?

Servlet的:

 了request.setAttribute(按钮,添加新的优惠券);
的request.getRequestDispatcher(/ admin_main.jsp)向前(请求,响应)。

JSP:

 如果(的request.getParameter(按钮)!= NULL){
    // ...
}


解决方案

如果(request.getAttribute(按钮)!= NULL)将工作。

信息:

永远做一个的request.getParameter()提取(通过发布HTML形式发送的数据)请求参数。在的request.getParameter()总是返回字符串值和数据来自客户端。

总是使用 request.getAttribute()来获取对象添加到服务器端请求范围即采用了request.setAttribute()

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.

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?

Servlet:

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) will work.

Info:

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.

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

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

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