File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In what ways can you pull something from the request?

 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given some servlet code that looks like this:



How can I get that value out of the request within a JSP? I believe I can do either of these:



but I'm having difficulty getting the second one to work. I know that you can set your app server to ignore EL, but I haven't done so. Yet, the result is that the first line reads "Corey" and the second line reads "${user}". Obviously, the EL expression is not being interpreted. Anyone know why that might be?

Additionally, are there any other ways I might get that value from the attribute, besides using an expression or an EL expression?

Thanks.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- to get your EL working, check your web.xml file, and be sure the <web-app> element looks like this:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


I'm guessing yours does not have the most current version info...

cheers,
Kathy
 
Colin Fletcher
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,

The access of ${user} should work. You may want to check into your Containter (tomcat / jboss) config.

I was successfully able to use something similiar in Tomcat 5.0.28 without any problem.


You can access objects by
<jsp:useBean id="user" type="com.example.User" />

The id parameter identifies the name of the object.
The type identifies the class or interface.. NOTE the object must exist.
There is a class parameter - which will create the object if it does not exist... (this can NOT be an abstract class because if it does not exist it will need to be instatiated)
There is also a scope parameter to identify the scope of the 'bean'.

I will see if I can find a link to the detail for jsp:useBean.

-C
 
Colin Fletcher
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A link for you:

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Kathy, you're right, I don't have the most recent info, but that doesn't quite solve my problem, either.

Here's what I currently have for a web.xml file (entirely generated by WSAD):



If I try to add in the snippet you gave me, so I end up with this:



Then I end up with 21 errors. The errors seem to all of this nature:



Of course, I checked out the DTD file referenced from the second line:



As I expected, those attributes are not defined within that DTD. Hence, all the errors. Well, the lines you provided me with, Kathy, include an xml schema so I didn't really see the need for the DTD line, anyway, so I tried removing it, to end up with a web.xml file that looked like this:



When I do that, I just get a different error:



Gah! What gives? Anyone run into this before?
[ October 22, 2004: Message edited by: Corey McGlone ]
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you make the web-app tag *exactly* like the one I pasted in? Can you take out the id="WebApp"? Someone here must know WSAD... did you try the regular Servlets and JSP forums?

Wish I had more helpful info
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kathy Sierra:
What happens if you make the web-app tag *exactly* like the one I pasted in? Can you take out the id="WebApp"? Someone here must know WSAD... did you try the regular Servlets and JSP forums?

Wish I had more helpful info


Yeah, I tried that, still no luck. Without the DTD line, it's like WSAD doesn't understand what the XML doc should be and it doesn't seem to pay any attention to the xmlSchema information.

I haven't trued the other forums because, originally, I thought this was just a problem with the way I was writing my EL. It would appear now, however, that it's a problem with me DD.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An additional question to Corey's.

Should we use ${request.user}, instead of ${user}?

If not, what's the difference?

Nick
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Cheung:


Should we use ${request.user}, instead of ${user}?

If not, what's the difference?


Nope, all you need is ${user}, although if you think there may be more than one "user" attribute in the four possible attribute scopes (page, request, session, and application), you can prefix with the scope using the implicit object that represents a MAP of the scope attributes.

But it's "requestScope" not "request".

So, assume there is only one attribute named "user", then

${requestScope.user} and ${user} have the same result

You can also use the implicit scope map when you have an attribute name that doesn't follow normal Java identifier rules.

An example we use in the book is:
What if you have an attribute named "foo.person"? Imagine you did
request.setAttribute("foo.person", p);

You would be in trouble, because you could not use:

${foo.person.name}, because the dot between person and name is perceived to be the dot OPERATOR.

But if you use the requestScope implicit object, then you can use quotes like this:

${requestScope["foo.person"].name}

So using the scope lets you put the attribute name in quotes, which you can't do if you simply want to name the attribute.

cheers,
Kathy
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Cheung:
An additional question to Corey's.

Should we use ${request.user}, instead of ${user}?

If not, what's the difference?

Nick


Actually, I don't think ${request.user} will compile. There is no implicit object called "request". Rather, I believe you have to do this: ${requestScope.user}. (But I haven't compiled this on my own to know for sure.)

So what's the difference? Well, let me give it a shot. Someone please correct me if I'm wrong.

If you don't use a scope, you'll end up searching all scopes, starting at page scope. If no matching attribute is found there, you'll go on to search request scope, followed by session scope and application scope. If no attribute is found in any scope, a new attribute is created in page scope.

If you include a scope, such as requestScope, only that scope will be searched and, if no matching attribute is found, a new attribute will be made at request scope.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... sorry for the typo.

If a scope is not given, all scopes would be searched. However, how about if there are 2 scopes that contain the same key?

Nick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic