aspose file tools*
The moose likes Servlets and the fly likes Dispatching request problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dispatching request problem" Watch "Dispatching request problem" New topic
Author

Dispatching request problem

Lawrence Kennon
Greenhorn

Joined: Jan 18, 2006
Posts: 23
I have a simple test servlet where I declare an object called bills of type com.lwk.Bills (a class of my own). The relevant code is very simple and looks like this:

Bills bills = new Bills();
request.setAttribute("bills", bills);
RequestDispatcher view = getServletContext().getRequestDispatcher("/DisplayAllBills.jsp");
view.forward(request, response);

I store an attribute called "bills" in the request and forward it to a .jsp page. However on the JSP side when I try to get this attribute it is always null, for example this JSP code bills is always null:

<%! Bills bills; %>

<% bills = (Bills) request.getAttribute("bills");

Is there some step I have missed, or some obvious idiocy I have committed so that my Bills object doesn't get attached to the request and forwarded to the JSP page (or should I be asking this on the JSP forum?).

Thanks for any insight on this.

lwk
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Upon initial inspection, I can't see any reason why the scoped variable would be null. There must be more factors at work than you are showing here. For example, do you have a <jsp:useBean> declaration for the scoped variable perhaps?

However, the following:
<%! Bills bills; %>

is an incredible blunder. You do realize that this will cause all users who access the JSP to share the same single instance of the value? That can't possibly be want you intend.

The higher-level question is: why are you using old-fashioned Java scriptlets in the JSP in the first place rather than the JSTL and EL?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lawrence Kennon
Greenhorn

Joined: Jan 18, 2006
Posts: 23
Originally posted by Bear Bibeault:
Upon initial inspection, I can't see any reason why the scoped variable would be null. There must be more factors at work than you are showing here. For example, do you have a <jsp:useBean> declaration for the scoped variable perhaps?

However, the following:

is an incredible blunder. You do realize that this will cause all users who access the JSP to share the same single instance of the value? That can't possibly be want you intend.

The higher-level question is: why are you using old-fashioned Java scriptlets in the JSP in the first place rather than the JSTL and EL?


In answer to your first question, no, the JSP is dead simple code and it doesn't have a <jsp:useBean> declaration. It imports com.lwk.* and java.util.*.

In answer to your second question, this is just a test case while I am working through some of these concepts in a book. Initially it wasn't instance scoped - I just tried that out of desperation. I intend to work up to using some of the technologies you mentioned, but right now I am just playing with "old school" stuff in order to understand it better (cause I often have to work on "old school" stuff written years ago).

lwk
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Hmmm. Could you show us the entire code for the JSP? Please be sure to use UBB code tags.
Lawrence Kennon
Greenhorn

Joined: Jan 18, 2006
Posts: 23
Here is the complete code of the test JSP (slightly redone):



And here is the very simple Java servlet that forwards the request to it:



I am sure it has got to be something really dumb that I am doing!

lwk
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

I don't see anything wrong (except as noted below), and when I copied your code exactly (creating a simple bean for Bills), everything works as expected.

Originally posted by Lawrence Kennon:
I am sure it has got to be something really dumb that I am doing!


One of the best ways to make your code fragile and confusing is to do unecessary and superfluous things. What's up with:

?

And the unnecessary cast:



These aren't causing your issue, but they're things you should avoid,

As regarding your issue, I'm at a loss. Works fine for me (Tomcat 5.5). What's your setup?
Lawrence Kennon
Greenhorn

Joined: Jan 18, 2006
Posts: 23
I agree with you on the unnecessary stuff. I removed those - they werew mostly desperation - "why doesn't this work, it's just like in the book using my classes." I went back to the servlet and as a test changed it to create another bill the value for which I got from doing a request.getAttribute("bills") after previously setting it. Then I just printed out the values in the servlet and quit (at that point). That worked and displayed the values I expected. So I went back and tried again and voila, now it is working. I have no clue as to what changed (although obviously something did - I am just not observant enough to see what it is).

Now after recoding (and taking out the stupider things I tried) it works and the JSP gets the request and I am working on displaying it now in the JSP.

Like I said, I have no idea.

I am using Java JDK 1.4.2_12 and Tomcat 5.0.12 and getting most of the servlet code from "Head First Servlets & JSP" (which is really a good book). I build the Java jar with all my classes (other than JSP) in JBuilder 2005.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Very weird. Glad it's all working for you now.
Lawrence Kennon
Greenhorn

Joined: Jan 18, 2006
Posts: 23
I really appreciate you looking at it. Probably some mistake I was making in staging the parts to correct location for Tomcat, or something like that. Anyway it appears to be working as advertised now.

Thanks!

lwk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dispatching request problem