• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Runtime Exception

 
BO KANG
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers:

I found one issue when i do the mock exam from http://j2eeworld.weebly.com

public class MyServlet extends HttpServlet {
private int x = 0;
public MyServlet (int x) {
this.x = x;
}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
x++;
PrintWriter out = response.getWriter();
out.println(x);
}
}
What will be the output (assume all imports are done correctly), when invoking MyServlet ?

A.Compiler error

B.Runtime Exception

C.Prints 1 in browser window.

D.Prints the value of x in browser window , depending up on the value used to create the servlet.

The answer is B,but why?Could you give me some hint?

And another issue is if i override doPost,but not doGet in the servlet,and in my jsp,I use the method="POST",are there some problem or not ?

Thanks anyway.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give me some hint?

Hint : will the container be able to instanciate this servlet ?
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will surely throw a Run TIme Exception as you cannot use the parameter in a servlet's constructor. You have used like:

public Myservlet(int a){
this.i=i;
}

As Servlet container creates instance of servlet by invoking common constructor.
So use use <init-param> in web.xml for nay parameter you need to pass to the servlet.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand, please rethink your answer.
1. You said that you cannot use parameter in a servlet's constructor. In fact, you can. But you need to do something else to do so. (memories from SCJP)
2. init-param has nothing to do with the servlet constructor.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

Servlet container creates instance of servlet by invoking no-arg constructor.Can we do constructor-chaining if we wish to use parameterized constructor? as you said its related to SCJP concept.Correct me if I am wrong
Or we intent to rewrote container to invoke parameterized constructor,if yes how?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that there is a constructor with a parameter is not a problem, as long as there is a default constructor too. If there is a constructor with a parameter, Java will not create a default constructor (SCJP), thus the container will not be able to instanciate the servlet, resulting in an exception thrown at runtime. The constructor with a parameter will be sitting there, never being called by the container.

So, the problem itself is not that there is a constructor with a parameter. The problem is that there is no default constructor

Can we do constructor-chaining if we wish to use parameterized constructor?

Yes you can, but you should not. Initialization should be done in the init method.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
But I think I was also trying to state the same:

"As Servlet container creates instance of servlet by invoking common constructor.(its means no-arg constructor)"
anyways thanks for ellaborating this.
 
BO KANG
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for such elaborate discussion on that.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats' great! had been expecting the missing 'init' method. Finally christopher mentioned!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic