Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But, I think it would be more correct if you changed the code to:

Any other suggestions.
Craig
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • 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
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic