setting request parameters ?

Hi there

I tried the following:

request.setAttribute("myVar", "myValue");

pageContext.forward("/main");

When it loads the main page, my variables are not being set.

I have tried the <jsp:forward> page directive and this works for a simple test example I've used, but not when I try it in my application main page ( which consists of 3 frames). The 3 frames are all JSP pages, so does the time they get loaded affect the request and <jsp:forward> attributes being sent ?

Or can I not just set request parameters?

I'm stumped !

Aaron

[615 byte] By [arcooper] at [2007-9-26 3:38:46]
# 1

When I want to pass parameters from one JSP to another I do...

In caller JSP...

<jsp:forward page="somePage.jsp">

<jsp:param name="paramName" value="paramValue" />

</jsp:forward>

And in called JSP...

request.getParameter("paramName");

neville_sequeira at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
And I forgot to mention that it works that way.
neville_sequeira at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I just did this and it worked:

MattTest3.jsp

<%

request.setAttribute("a", "b");

pageContext.forward("MattTest5.jsp");

%>

MattTest5.jsp

<%= request.getAttribute("a") %>

Are you mistakenly using request.getParameter instead of request.getAttribute in your main JSP?

The method neville suggested is probably the best to use, as its more fleible in terms of re-using the main page when you aren't forwarding to it - It allows you to add the parameters to the query string instead if you decide you want to do that in the future.

mattbunch at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi guys

I have tried neville's way,and for some reason it wont' work when I am using the three frames, but in a simple example, it works fine.

As for your example Matt, my simple example is very similar to yours, except I use "request.getParameter()".

getParameter() returns it as a string, while getAttribute() returns as an Object. Isn't that the only difference?

Thanks for the input!

arcooper at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Ok, I know what the problem was :) You were right Matt.. I should use getAttribute() when I use setAttribute() too... the brain is working slowly today! LOL

Thanks a lot guys for your help. I shall split the Duke Dollars among the two of you, and I shall give Matt the rest of my Duke Dollars for my previous post.

Cheers!

Aaron

arcooper at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

No, thats not the case. getParameter and getAttribute return from different internal collections.

An HttpServletRequest object will have two different HashMap objects (at least, they're probably HashMaps).

One stores the parameters that were passed by the client's request (That's why they're always Strings - the client passes text using the HTTP GET and POST protocols). getParameter returns Strings from here.

The other HashMap maintains objects that you add on the server side to pass between your servlets. You may want to add any type of object, so that's why these aren't required to be Strings.

Think of neville's example (which allows you to use getParameter on the next page) as creating a new request that works as if the client had appended the extra parameters to the query string. You can't add non-String Objects using that method.

If you try your simple example again using getAttribute, you'll probably find that it works.

mattbunch at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Just beat me to the post there...Hey, it's Friday - I can forgive you. Have a good weekend.Matt
mattbunch at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Cool down mattbunch,As arcooper says the brain is working slowly today![\b]
neville_sequeira at 2007-6-29 12:12:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...