Imagine the following situation. You need to write some small web app with few simple features just for internal company needs. You can check your usual JEE technology stack – EJB3, Hibernate, Spring, etc., but … Other possibility is to use some scripting language, PHP or something, but you are Java guy !!!. You may try Groovy, but there is a learning curve, you can’t stand. So what?

decided to go back to J2EE roots and try good old JSP/JSTL scripting with tag and other wicked concepts. I added Tomcat, Eclipse with hot code replace feature, shaked all together and I got it – instant one tier web application.

Short installation guide:

1. You certainly have Eclipse 3.2 with WTP and Tomcat 5.x already installed. This should work also with other relatively new versions, but I can’t guarantee that.

2. Eclipse: File -> New -> Project … -> Web -> Dynamic Web Project

3. New Dynamic Web Project wizard: Target Runtime -> New … . Here choose Your Tomcat, set Tomcat installation directory, etc. Other settings leave default on this screen. Next.

4. Project Facets: check Dynamic Web Module and Java. Next.

5. Following screen leave in default configuration and click Finish.

6. Create simple index.jsp. Something like this:

< %@ page language="java" contentType="text/html; charset=utf-8"
 < %@taglib uri="" prefix="c"%>
 < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <h1><c :out value="Test" /></h1>

and put it into WebContent directory.

7. Manually (use eg. Total Commander) copy jstl.jar a standard.jar into WebContent/WEB-INF/lib

8. In Eclipse right click on index.jsp. Run as -> Run on server. Index.jsp should open in eclipse web browser or in external browser (in case of Linux).

9. Add some servlet. Left click on „src“ directory -> ctrl+N -> Web -> Servlet. Create your servlet. Something like this:

public class TestServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException {
       PrintWriter out = response.getWriter();
       out.println("<html><head><title>test</title></head><body><h1>Servlet test</h1></body></html>");
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request, response);

In New Servlet wizard You can configure servlet mapping and this will be automatically reflected by web.xml. Let’s presume /TestServlet pattern to be chosen for our servlet.

10. In Servers view righ click on Tomcat server -> Restart -> Debug. In debug mode, you can utilize Eclipse hot code replace feature among other things.

11. Check TestServlet URL in your browser, then change content of H1 element in code and refresh your browser. It should work without application reload.

12. Set up DataSource (PostgreSQL example):


 <resource -ref>
       Some description
     <res -ref-name>jdbc/test</res>
     <res -type>javax.sql.DataSource</res>
     <res -auth>Container</res>


 <resource name="jdbc/test" auth="Container"
 <resourceparams name="jdbc/test">

This Resource definition in context.xml should be used in Tomcat 5.0. In Tomcat 5.5 will have to be used following equivalent:

 <resource name="jdbc/test" 

You need to place appropriate JDBC driver eg. to TOMCAT_HOME/common/lib.

13. In Server View restart Tomcat, than test data source configuration by following JSP fragment in your index.jsp. Of course I presume You have created table USERS with specified columns in your database already:

< %@taglib uri="" prefix="sql"%>
 <sql :setDataSource dataSource="jdbc/test" />
 <sql :update>
 INSERT INTO USERS(username, first_name, surname, passw) VALUES(?, ?, ? ,?)
   <sql :param value="testusername"/>
   <sql :param value="testfirstname"/>
   <sql :param value="testsurname"/>
   <sql :param value="testpassword"/>

14. And you will probably need some security settings, so for those who forgot how set up container security in Tomcat I provide short reminder:


 <security -constraint>
   <web -resource-collection>
     </web><web -resource-name>
       Administration section
     <url -pattern>/admin/*</url>
   <auth -constraint>
       <role -name>admin</role>
 <security -constraint>
   <web -resource-collection>
     </web><web -resource-name>
       User section
     <url -pattern>/user/*</url>
   <auth -constraint>
       <role -name>user</role>
 <security -role>
     <role -name>admin</role>
 <security -role>
    <role -name>user</role>
 <!-- Define the Login Configuration for this Application -->
 <login -config>
   <auth -method>BASIC</auth>
   <realm -name>Test App</realm>


<realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
        userTable="users" userNameCol="username" userCredCol="passw"
    userRoleTable="user_roles" roleNameCol="user_role"/>

You need USERS(username,passw) and USER_ROLES(username,user_role) tables in database as you see.

15. That’s it. If something fucked up, try to shutdown tomcat and do other usual things in such situation.

Good luck and develop rapidly.

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Email this to someone