Servlet Connection Pooling for Dummies

In a previous entry ("Compiling JSP Files") in this blog I mentioned the cool NetBeans IDE feature which allows you to see the JSP file's translated servlet. Thanks to this feature, you're able to very quickly access a connection pool from a servlet without writing a single line of Java code, because once you've created the JSP file (below) and deployed it, all you've got to do is view the translated servlet, copy the code into a new servlet file, and then deploy it as you normally would (with one additional step, as explained in step 5 below). Accessing a connection pool from a servlet is a more natural scenario than doing so from a JSP file anyway (since you're dealing with data) and so the fact that you're able to see the translated servlet is a pretty helpful facility.
  1. After deploying, as described in the previous blog entry ("Super Cool Connection Pool"), right-click the index.jsp file and choose View Servlet.
  2. Select everything in the file and copy it.
  3. Create a servlet named index_jsp and put it in a package called org.apache.jsp. (Right-click the project node, choose New > Servlet.)
  4. Paste the code you copied over all the default code in the template.
  5. Right-click the Libraries node, choose Add Library, click Manage Libraries, click New Library, name it JASPER, click Add JAR/Folder, browse to enterprise1\\jakarta-tomcat-5.5.7\\common\\lib and select jasper-runtime.jar. Click OK.

Now deploy the servlet and you'll get the same result as below.

NB: There's one disadvantage with this and that is that the servlet is translated from the JSP file by the server. Therefore, the servlet created by the Sun Java System Application Server may not work with the Tomcat Web Server. Similarly, a servlet created by one version of a server may not work with another. Portability...

Update 04/25/05: Finding it hard to establish a connection from your Java source file to your database? Check out this blog entry: NetBeans IDE 4.1: No More Worrying About Database Connections.


Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« June 2016