用了request.setAttribute()传递的值是不可用的request.getParameter() [英] Value passed with request.setAttribute() is not available by request.getParameter()

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

问题描述

我给一个字符串变量在code的正常执行的价值,但如果有异常发生,我都会给它一个值,的问题是,在catch块中值仍然相同,我第一次分配

I give a string variable a value in the normal execution of the code ,but if an exception happen I will give it another value , the problem is that in catch block the value is still the same as i assign first .

下面是我的code,首先,我指定页面值ADDUSERtry块内,并在追赶我给它的errorPage的价值,我送页的价值HTTP请求内前页方法和里面我打印页的值。
我会在code。将我希望它通过catch块的excution一个错误,这样做,但是当它的页面值发送到前页功能页面的价值是ADDUSER不的errorPage虽然我指定为的errorPage!!

Here is my code ,first I assign page value "addUser" inside try block and in catch I give it "ErrorPage" value , I send the value of page within http request to forword method and inside it i print the value of page. I cause an error in the excution of the code an i want it to go through catch block , and it does , but when it send the page value to the forword function the value of page is "addUser" not "ErrorPage" although i assign it to "ErrorPage" !!

String page = "addUser";

try {
    // ...

    request.setAttribute("page", page);
    forward(request, response);
} catch (SQLException e) {
    page = "ErrorPage";
    request.setAttribute("page", page);
    forward(request, response);
}

和这里的前页功能

String page = request.getParameter("page");
System.out.println("page is " + page); // each time it prints addUSer

有人能帮忙吗?和thanx提前。

Can someone help? and thanx in advance.

推荐答案

您正在呼叫的request.getParameter()而不是 request.getAttribute ()来获取价值。既然你已经把它设置为请求属性,你也应该把它作为请求属性。

You're calling request.getParameter() instead of request.getAttribute() to obtain the value. Since you've set it as request attribute, you should also get it as request attribute.

所以:

request.setAttribute("foo", foo);

仅可通过

Object foo = request.getAttribute("foo"); // NOT getParameter().

的getParameter()仅适用于HTTP请求参数,你可以在请求URL或HTML表单的输入字段中指定。

The getParameter() is only for HTTP request parameters as you can specify in request URL or in input fields of HTML forms.

这篇关于用了request.setAttribute()传递的值是不可用的request.getParameter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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