• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[ Servlet ] Error when I added the init() method to a servlet

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Context
IBM VAJ WTE environment
Problem
I wrote a servlet and it is working fine.
I then needed the servlet to read some
parameters and to persist them to the
application scope. The servlet compiled fine.
But when the servlet is loaded, I get this
error on my browser:
Server caught unhandled exception from servlet [FooServlet]: null
My init method looks something like this:

Can anyone help me out ?
Thanks
Pho
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is that if pageSize < 1 you will throw an IllegalArgumentException() at that point processing stops, the exception will not be caught by the "IllegalArgumentException" catch phrase, so the method signature of the init() method has to specify the
IllegalArgumentException in the throws clause.
Craig
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, I think it would be more correct if you changed the code to:

Any other suggestions.
Craig
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your code with Tomcat 3.2 and yes I'm getting a

javax.servlet.ServletException: PhotekServlet: (error) no pageSize parameter was specified
at PhotekServlet.init(PhotekServlet.java:13)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)


(Yes, I named it after you. )
The occurs when I access it with
http://anthony:8080/myContext/servlet/PhotekServlet
but it disappears when I use
http://anthony:8080/myContext/servlet/photek
where "photek" is the servlet alias of PhotekServlet. It also works when you have an explicit servlet mapping.
-anthony
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
I've rewritten the init method to look like this, and it's working fine.

The changes made:
* In the line marked 1, I made a call to the superclass's init method. I read that if you're going to override the init, this is something which needs to be done. Why ? I don't know.
* I've dispensed with all the exceptions by rolling up the exceptions into one catch-all because that simplifies the code.
Pho
[ April 06, 2002: Message edited by: Pho Tek ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the line marked 1, I made a call to the superclass's init method. I read that if you're going to override the init, this is something which needs to be done. Why ? I don't know.


Simple, thats the only way to attach the configuration information to your servlet. If you don't do that you get mysterious null pointer exceptions later.
Your problem with different behaviour when addressing the servlet different ways is related to the information in web.xml. If you don't use the web.xml alias, the servlet does not get init-params from web.xml.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic