Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet fail to call init()  RSS feed

 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's my 1st attempt to use tomcat's connection pooling and I have tested the datasource acquisition in the jsp page without a problem. When I ported over the codes into a servlet, I am suggested to initialise the datasource once (as doing it each and every single time when an sql call is needed is an expensive affair).

My codes (copied from a tutorial somewhere and modified):




My jsp:



... returns a null.

If I rename the init() to become start() , and removes the line super.init(config);, the codes below returns the intended datasource. (as the output showing stuff like org.apache.tomcat.dbcp.dbcp.BasicDataSource@18e80a6



I thought the init() are suposed to be invoked ONCE when the servlet came to life , but it doesn't seem so in my case here. Is there anything that I missed out?

thanks in advance




 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what you are doing for a moment. What will happen when multiple requests for this servlet arrive at the same time?
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet would be initialized when the container loads the servlet. I am assuming that in you case you are directly going to the JSP and not the servlet class.

Another hint is that the scope of this class is page - which means that this bean is created in this page only (I think) and anyways doesn't see right to be using a HttpServlet class in a JSP.

<<At this point it is not the container that is creating the Servlet class but you are directly creating it in the JSP code. Hope you get the difference>>
# <jsp:useBean id="testConn" scope="page" class="test.controller" />
# <%= testConn.getTestDS() %>


 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Think about what you are doing for a moment. What will happen when multiple requests for this servlet arrive at the same time?


Please correct me if my understanding of initialising the datasource through the above methods are wrong: When the datasource (testDS) is initialised once, incoming requests create a connection (eg. newConn = testDS.getConnection(); ) , and they're unique and won't interfere with each other. NO?

Sam Mercs wrote:The servlet would be initialized when the container loads the servlet. I am assuming that in you case you are directly going to the JSP and not the servlet class.



Yes you are correct. What should I do to "let the countainer load the servlet" ? I've read somewhere there an entry must be created inside WEB-INF/web.xml for each servlet.



Sam Mercs wrote:
Another hint is that the scope of this class is page - which means that this bean is created in this page only (I think) and anyways doesn't see right to be using a HttpServlet class in a JSP.





You must be mistaken my "tested the datasource acquisition in the jsp page without a problem" sentence earlier (which is another set of codes altogether not shown here).


Well, my block of codes with HttpServlet in it actually located in mywebapp/WEB-INF/classes/test directory.




 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:At this point it is not the container that is creating the Servlet class but you are directly creating it in the JSP code.

Wow! I never made it that far! That's really whacked!

You should never instantiate a servlet yourself. I think you need to sit back with a good JSP and Servlet tutorial and learn how these concepts are meant to be used.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
You should never instantiate a servlet yourself. I think you need to sit back with a good JSP and Servlet tutorial and learn how these concepts are meant to be used.


Ya I agree - you need to understand a bit about JSP and Servlets before you start coding. I'd recommend getting a copy of Head First JSP and servlets. Its got to be one of the best books on the topic.

Maybe you could try searching for Simple servlet examples but I never found one that is simple and clear online. Maybe I should write one myself

Alan Yap wrote:
You must be mistaken my "tested the datasource acquisition in the jsp page without a problem" sentence earlier (which is another set of codes altogether not shown here).


No I am was not - I was looking at the code you have posted. When I said 'HttpServlet class ' I meant the 'controller' class (which is an HttpServlet class). Anyways at this point that is not really important.
Do you understand what it means to say sp:useBean id="testConn" scope="page". You might want to look here -- http://javapapers.com/jsp/explain-the-scope-of-jsp-objects/

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!