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" pageEncoding="utf-8"%> < %@taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test</title> </meta></head> <body> <h1><c :out value="Test" /></h1> </body> </html>
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 { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>test</title></head><body><h1>Servlet test</h1></body></html>"); out.close(); out.flush(); } 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 TestServlet.java code and refresh your browser. It should work without application reload.
12. Set up DataSource (PostgreSQL example):
web.xml
<resource -ref> <description> Some description </description> <res -ref-name>jdbc/test</res> <res -type>javax.sql.DataSource</res> <res -auth>Container</res> </resource>
META-INF/context.xml
<context> <resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/> <resourceparams name="jdbc/test"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <parameter> <name>driverClassName</name> <value>org.postgresql.Driver</value> </parameter> <parameter> <name>url</name> <value>jdbc:postgresql://localhost:5432/test</value> </parameter> <parameter> <name>username</name> <value>xxx</value> </parameter> <parameter> <name>password</name> <value>xxx</value> </parameter> </resourceparams> </context>
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" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/test" username="xxx" password="xxx" />
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="https://java.sun.com/jsp/jstl/sql" 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"/> </sql>
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:
web.xml
<security -constraint> <web -resource-collection> </web><web -resource-name> Administration section </web> <url -pattern>/admin/*</url> <auth -constraint> <role -name>admin</role> </auth> </security> <security -constraint> <web -resource-collection> </web><web -resource-name> User section </web> <url -pattern>/user/*</url> <auth -constraint> <role -name>user</role> </auth> </security> <security -role> <description>Administrator</description> <role -name>admin</role> </security> <security -role> <description>User</description> <role -name>user</role> </security> <!-- Define the Login Configuration for this Application --> <login -config> <auth -method>BASIC</auth> <realm -name>Test App</realm> </login>
context.xml
<realm className="org.apache.catalina.realm.JDBCRealm" debug="99" driverName="org.postgresql.Driver" connectionURL="jdbc:postgresql://localhost:5432/test" connectionName="xxx" connectionPassword="xxx" 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.
Poslední komentáře