*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes In what ways can you pull something from the request? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "In what ways can you pull something from the request?" Watch "In what ways can you pull something from the request?" New topic
Author

In what ways can you pull something from the request?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Sep 10, 2004
Posts: 200
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


SCJP 1.4 SCWCD 1.4
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
A link for you:

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Nov 07, 2003
Posts: 4982
An additional question to Corey's.

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

If not, what's the difference?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Nov 07, 2003
Posts: 4982
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In what ways can you pull something from the request?
 
Similar Threads
Really confused about runtime.exec()
Is this the best way to get at dynamic data?
EL expression question. Getting a value from a map that use Integer like key
how to get the value saved in request scope in a servlet into a jsp page
When using MVC, to what degree should a model be tailored to a view?