I have been doing rails most of the time for the past 18 months and never considered myself a true convert. I like ruby and rails, but there are several issues with the ruby language that have never made me say, ok "I love Ruby", but after today, I will say, I love Rails, I wish java would stop the insanity and move to a model closer to rails for its web frameworks. Java is a great language, but the configurations of jsp/servlets/jdbc/templates should be made super easy and not be this complicated.
I havent used Tomcat in a while, so I needed to setup a very simple webapp that talks to a database (sqlserver) and presents some simple web pages. Something I did in rails in about 20 minutes total 1 controller, 1 model and 3 views). So I thought porting to java would take a couple hours at the most. Now I am going on 7 hours and nearly complete. I am going to document this because I could not find anything out there that worked for me on the first try.
I am using Eclipse and Tomcat 6 to do this with SqlServer as the database.
First create a project in eclipse and select "Dynamic Web Project" and follow the prompts, choose your existing tomcat install.
Under WebContent, add yourself your first jsp, yeah, I have a project and a jsp, and you can start your tomcat server from withing eclipse and even debug the jsp if you want, yeah, that was easy!
Ok, now I want a database, and I probably want to use connection pooling, since creating and destroying connections is expensive, but the documentation on this was very fuzzy, here is what I finally did after lots of reading:
1.) create a context.xml file under META-INF
<Context path="/myapp" docBase="myapp"
debug="5" reloadable="true" crossContext="true" >
<Resource name="jdbc/myapp" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="joel" password="password" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:sqlserver://10.211.55.4:1433;DatabaseName=MyDB;autoReconnect=true"></Resource>
</Context>
2.) Add this to yoru web.xml inside the webapp section:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myapp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.) Add to your jsp:
<%@page import="java.sql.*, javax.sql.*, java.util.*, javax.naming.*"%>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/happierwidget");
conn = ds.getConnection();
// ... do your jdbc work here
conn.close();
%>
3.) add your jdb drivers to your server/lib folder, I could not get mine to work by putting them in my application's lib folder. Add your drivers to your "java build path" by right click on your project and pick properties, then add external jar file.
4.) Restart your server and you should be able to connect. YEAH.
I couldnt get the standard template libraries to work now matter what I tried, I ended up punting and doing ugly jsp work to make this happen.
Recent Comments