开发者

Value passed with request.setAttribute() is not available by request.getParameter()

开发者 https://www.devze.com 2023-01-28 14:22 出处:网络
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 assi

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 .

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);
}

and here is the forword function

String page = request.getParameter("page");
System.out.println("page is " + page); // ea开发者_Go百科ch time it prints addUSer

Can someone help? and thanx in advance.


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.

So:

request.setAttribute("foo", foo);

is only available by

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

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


In addition to BalusC's point"

In your code you have declared two "String page" variable. This will not compile. I think the parameter that you are passing to the request in the catch must be the "other" page variable. But it is hard to tell since this is not a true example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号