Forums Register Login

NullPointerException in JSP after calling Servlet method

+Pie Number of slices to send: Send
I have a JSP that calls a method from a Servlet and it gives me a NullPointerException error in my Tomcat 5.5 Container on Windows.

Here is my Servlet Class:

The JSP (showDate.jsp):

Error messages:

Please advise how I can get this to work.
+Pie Number of slices to send: Send
 

String myDate = new DateServlet().fetchDate(request);



Why on earth would you do something like this? Servlets are not intended to by instantiated by anything but the container. Why aren't you using a bean? (Or better yet, why not follow best practices and not do any Java in a JSP in the first place?)
+Pie Number of slices to send: Send
I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available. Here is my attempt for the Bean:


Please advise if this is the right direction and what line should I change because the request object is not available when I try and compile it:



Thanks

[ July 21, 2007: Message edited by: Dan Parsons ]
[ July 21, 2007: Message edited by: Dan Parsons ]
+Pie Number of slices to send: Send
 

Originally posted by Dan Parsons:
I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available.



You're passing the request as a parameter. That makes it available no matter what.

Please advise if this is the right direction



Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.
+Pie Number of slices to send: Send
Thanks, your info has helped alot because in future I will not display Java code in JSP AND I now will concentrate on using beans for displaying objects in JSP.

Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.



Trying to output the last modified date in JSP page called showDate.jsp:


You're passing the request as a parameter. That makes it available no matter what

How can I use the request object reference in the bean class file (because it wont compile) or should I use something else in the fetchDate method?
[ July 21, 2007: Message edited by: Dan Parsons ]
+Pie Number of slices to send: Send
What are you trying to get at in your method? It's considered poor form to pass container objects like the request around. Better to get the data you need and pass that.
+Pie Number of slices to send: Send
I thought I could get the last modified date and return it as a String in the fetchDate method.

This works if I just put it in a JSP with Scriplet:


I would rather not do it this way and use a Bean as you suggested and for giving me guidance to do it correctly in the future.
[ July 21, 2007: Message edited by: Dan Parsons ]
+Pie Number of slices to send: Send
Out of curiosity.
Why are you trying to get the last modified date of the JSP?
+Pie Number of slices to send: Send
I want Last Modified to show in my JSP to give a Date and time of when the info was last modified/changed in the Web page.
+Pie Number of slices to send: Send
Sorry to keep this going but I ended up putting this in a Bean class and hard coding file name in the Bean class and it works but now I would like to have the file name passed as a condtion in the JSP Java Bean call.

Here is how it works with hardcoding the file name in the bean class and calling it in my JSP where it outputs the last modified date just as I wanted:

Java Bean class:



The above works but now how can I put the filename in the setProperty so I can have a variable in the JSP. I tried all the below and it just prints out a literal value: C:\\tomcathome\\anotherFile.jsp. Please advise.


Attempt in Java Bean:
+Pie Number of slices to send: Send
 

Originally posted by Dan Parsons:
I want Last Modified to show in my JSP to give a Date and time of when the info was last modified/changed in the Web page.



That part I figured.
What I should have asked is, why?

To re-iterate and expand on what Bear said earlier, in most up to date Java web applications, the JSPs are only one part of the mechanism that handles the request/response chain. If a JSP is forwared to or included in another page, you're not going to be able to dynamically read this information. Also if your application has been deployed as a packed war file, then the method getRealPath, that you were using earlier to locate resources withing the app will return null; breaking your logic.

It sounds like you're going through a lot of trouble to build something that's only going to work in very specific cases.

Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.
+Pie Number of slices to send: Send
Thanks again for all your time on this!


Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.



I have three static JSP web pages where each has information on it and each time I update a page I would like the last modified date to show up on the page to show when that specific page was updated. This web app has just 3 static JSP web pages in it and does not use any MVC or other frameworks.
[ July 24, 2007: Message edited by: Dan Parsons ]
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2074 times.
Similar Threads
HTTP Status 500 - error running servlet with JSP
I am struggling with this error since 1 week
tag libs
Help Please
IntervalCategoryDataset error in JSP page
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:30:41.