Sensible Open Source

A JSP, A Servlet : Deployment Simple, short and sweet!

posted 4 December 2005, Sunday
A Beginning JSP AND Servlet!!

T
his is nothing more than a quick guide to a simple JSP and Servlet deployment. Simply showing the directory strucuture of the servlet container, configuration files, application directory structure and using DB pool connections.

Identify The Environment

First Create the DB Table:
Database : Lessons
Table : NewsRelease
Tomcat Directory : /servers/tomcat/
Tomcat Apps : /tcapps/
Application name : EXamp
Application Directory : /tcapps/EXamp



SQL Script
CREATE TABLE `lessons`.`NewsRelease` (
  `nrID` INT NOT NULL AUTO_INCREMENT,
  `date` DATE NOT NULL,
  `title` varchar(255) NOT NULL,
  `descr` TEXT NOT NULL,
  `dysToDisplay` INT NOT NULL,
  `CreateuserID` varchar(35) NOT NULL,
  `CreateDate` DATE NOT NULL,
  `LastUserID` varchar(35) NOT NULL,
  `LastUpdate` DATE NOT NULL,
  PRIMARY KEY(`nrID`)
)
ENGINE = MYISAM
CHARACTER SET utf8;


Now lets create the JSP:

Name it getNewsRelease.jsp


The code above needs to be placed in the TOMCAT (5.x) or servlet container of your choice.  Tomcat seems to be good as any so please change the directory structures and configuration files accordingly.  If you can do it in TOMCAT yo ucan do it in most others.  I also like JETTY, a heck of a lot, but its a little more raw than TOMCAT; so again, I chose TOMCAT.

Its important to identify all the files that glue this application together. The files, inclusive of the JSP file above are listed below with the respective directories.

EXAmp.xml : In Directory /servers/tomcat/conf/Catalina/localhost/EXUtils.xml

    This file setsup a DB Connection pool for the JSP and Servlet code to access the JDBC context.

Pay Attention to the NAME references to the EXAmp Application.  BE consistent!! Notice the File Logger logs the localhost_EXAmp_, the path name is /EXAmp, docbase=EXAmp and the JDBC name we give the connection pool in the TOMCAT configuration file EXAmp.xml is EXAmp.  Of course, you can change these references.  However, for now keep it all the same to get the servlet and the JSP working!! 

Once these files and parameters are understood configure and name the parameters what you deem fit!  But remember, if other people have to understand it to maintain it be cautious with your creativity.. 

<Context path="/EXAmp" docBase="EXAmp" debug="0">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_
EXAmp_" suffix=".log" timestamp="TRUE"/>
<Resource name="jdbc/
EXAmp" auth="Container" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/
EXAmp">

web.xml
: In Directory /tcapps/EXamp/WEB-INF

    This file the servlet name and class as well as the url-pattern for browser access.

     Here this file is spefically to configure the access to the servlet  from the url-pattern.

getNewsRelease.jsp : In Directory /tcapps/EXAmp

newsReleaseServlet.java : In Directory
    This file hits the database and executes the SQL to retrieve and display the results.

Create under  the directories below in your specific Tomcat Application Directory:

/tcapps/Examp
                    |_getNewsRelease.jsp
                    |_WEB-INF
                               |_classes/us/savard/utils
                               |                     |_NewsReleaseServelt.class
                               |_lib                           
                               |_tlds
                               |web.xml

The director structure only holds three files.  This removes all the confusion and mystery and hopefully is not daunting at all from this perspective.


These directory structures and files should be managed through Ant.  Its best to take the time to learn this tool!  There are many tutorials on line; however, give Alan, Kirk, Joey, Andrew and David a read to their "Ant Developer's Guide". 

Some hints:

Pay attention to the Context usage in the JSP and JAVA code.  This is how the code retrieves that CONTEXT of the DB Connection pool of the Servlet container.  :"LEARN CONTEXT" its good for you!! :-)


   

Hope that helps!! :-)

tags:      

links: digg this    del.icio.us    technorati    reddit