request.getParameter() 无法使用 request.setAttribute() 传递的值 [英] Value passed with request.setAttribute() is not available by request.getParameter()

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

问题描述

我在代码的正常执行中给一个字符串变量一个值,但如果发生异常,我会给它另一个值,问题是在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 .

这是我的代码,首先我在try块中分配页面值addUser",在catch中我给它ErrorPage"值,我将http请求中的页面值发送到forword方法,并在其中打印值页.我在执行代码时导致错误,我希望它通过 catch 块,并且确实如此,但是当它将页面值发送到前向函数时,页面的值是addUser"而不是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

有人可以帮忙吗?并提前谢谢.

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.getParameter() 无法使用 request.setAttribute() 传递的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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