| Author |
[ Servlet ] Error when I added the init() method to a servlet
|
Pho Tek
Ranch Hand
Joined: Nov 05, 2000
Posts: 757
|
|
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
|
Regards,
Pho
|
 |
Craig Jackson
Ranch Hand
Joined: Mar 19, 2002
Posts: 405
|
|
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
Joined: Mar 19, 2002
Posts: 405
|
|
But, I think it would be more correct if you changed the code to: Any other suggestions. Craig
|
 |
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
|
|
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
Joined: Nov 05, 2000
Posts: 757
|
|
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
Joined: Mar 22, 2000
Posts: 12271
|
|
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
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: [ Servlet ] Error when I added the init() method to a servlet
|
|
|