Part1 – Simple CRUD servlet web application by Virendra Singh

Assumptions:

The application has been developed and tested on Windows XP using following components:

  • JBoss 6.0 Application Server
  • Sun JDK 1.6
  • log4j 1.2.16
  • Ant 1.6.5 (bundled with JWSDP 2.0, however you should download the latest version independently and use it. At this time the latest available version is 1.8.1)

 Environment setup:

Set JAVA_HOME to JDK 1.6 installation directory
Set JBOSS_HOME to JBoss 6.0 installation directory
Set ANT_HOME to Ant installation directory

Add into PATH => %JAVA_HOME%\bin;%JBOSS_HOME%\bin;%ANT_HOME%\bin

Add into CLASSPATH => %JAVA_HOME%\lib\tools.jar;.;%ANT_HOME%\lib\ant.jar

JBoss server can be started as <JBOSS_HOME>\bin\run.bat from a command window, and stopped by pressing Ctrl C in that window.

Introduction:

This is a simple HTML, JSP, Servlet based application. No java script has been used except calling the onClick command on a button. It provides following functionality:

  • Add a new record
  • Delete a record
  • Update a record
  • Find records based on the first name and last name.

 Data will be saved into a flat file, and there are no security checks have been applied yet. These checks will be incorporated in later versions of this application.

MVC Architecture:

Starting point of the application is index.jsp which calls various other jsp pages or controller servlet actions. The controller servlet will capture action commands as:

 These actions have been added in the web.xml file under servlet mappings. There is only one servlet (ControllerServlet) which performs the business logic, provides results into a List and add its reference to the user session. Similarly some other data variables have also been added to the user session. Then the servlet forwards the call to an appropriate JSP page which retrieves the data from the session and displays it.

Following are the JSP pages:

  • jsp\insert.jsp           => inserts a record
  • jsp\list.jsp               => displays all the records with full information
  • jsp\search.jsp          => searches and displays based on the first & last name
  • jsp\update.jsp          => updates a record
  • jsp\view.jsp             => Similar to list but displays partial information only
  • jsp\yes-no.jsp          => Called while deleting a record for confirmation

 Drop down list on update.jsp:

There are three drop down fields which are: gender, dept and title. While updating a record, these options should be displayed properly selected as previous values. This logic has been written in CommonUtils.buildOptions(), which sets the values as session attributes which then update.jsp retrieves and displays.

log4j:

Log4j jar has been added under the lib directory and there is a corresponding log4j.properties file where a rolling file appender has been declared. The actual log file name comes from the build.properties via build.xml token replacement.

Resource Bundle:

Currently it has following values:

FieldSeparator=;
dept=IT;HR;Purchase;Accounts
gender=M;F
title=Director;Manager;Team Lead;Associate;Trainee

FieldSeparator is the data field separator for the data file where all the records are saved into a flat file. Name of the data file also comes from the build.properties, replaced via build.xml into the web.xml init parameters.

Build.properties:

  • javac.home    => Installation directory of Java JDK
  • javaee.home  => Installing directory of JBoss application server
  • data.file        => Name and location of the data file
  • log.file           => Name and location of the log file

 Button commands:

I wanted to provide a delete and update button for each record separately in the table format, where if we click on the button it knows that for which userId (i.e. unique key) the button click has been made. This functionality has been provided by the onClick command of Java Script. Other button commands are simple html post forms.

How to build it:

Once you download the zip bundle, extract into a directory and run:

ant buildwar or ant final from a command window from the extracted root directory. It will generate JavaWebApp.war in the dist folder. Copy the war file into <JBOSS_HOME>\server\default\deploy directory. There is no need to restart the JBoss application server, it picks up the changes automatically and loads the web application into the JVM.

Type following URL in the browser: http://localhost:8080/JavaWebApp/

Assuming the JBoss server was already running, which can be verified by typing the URL: http://localhost:8080

Source code:

The source code is self explanatory with enough comments. Following are the source code files:

<src\com\webapp>

  • dao
    • UserProfileDAO.java
  • domain
    • CommonUtils.java
    • UserProfile.java
  • servlets
    • ControllerServlet.java

Download the source code bundle:Download link

About these ads

About Virendra Singh

Software professional with 19 years of global experience (i.e. US and India) on designing and developing enterprise applications in Java/J2EE, C/C , TUXEDO, CORBA, Messaging, Oracle, Sybase and UNIX scripting. Currently working a Sr. Technology Manager with a reputed US based MNC in finance domain. Like photography, watching good movies, travelling, music, drink & dancing.
This entry was posted in WebApplication and tagged , , , , , , , . Bookmark the permalink.

6 Responses to Part1 – Simple CRUD servlet web application by Virendra Singh

  1. saurabh says:

    file is damaged

  2. sean t. says:

    gettting the following error when trying to comple can you assist

    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:67:
    error: cannot find symbol
    [javac] protected void service(HttpServletRequest request, HttpServletRe
    sponse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:68:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:85:
    error: cannot find symbol
    [javac] protected void doGet(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:85:
    error: cannot find symbol
    [javac] protected void doGet(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:86:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:92:
    error: cannot find symbol
    [javac] protected void doPost(HttpServletRequest request, HttpServletRes
    ponse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:92:
    error: cannot find symbol
    [javac] protected void doPost(HttpServletRequest request, HttpServletRes
    ponse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:93:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:100:
    error: cannot find symbol
    [javac] private void process(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:100:
    error: cannot find symbol
    [javac] private void process(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:101:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:37:
    error: cannot find symbol
    [javac] super.init(config);
    [javac] ^
    [javac] symbol: variable super
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:38:
    error: cannot find symbol
    [javac] context = getServletContext();
    [javac] ^
    [javac] symbol: method getServletContext()
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:82:
    error: cannot find symbol
    [javac] super.service(request, response);
    [javac] ^
    [javac] symbol: variable super
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:103:
    error: cannot find symbol
    [javac] HttpSession session = request.getSession();
    [javac] ^
    [javac] symbol: class HttpSession
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:104:
    error: cannot find symbol
    [javac] RequestDispatcher dispatcher = null;
    [javac] ^
    [javac] symbol: class RequestDispatcher
    [javac] location: class ControllerServlet
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 24 errors
    [javac] 1 warning

    BUILD FAILED
    C:\simple-webapp\build.xml:54: Compile failed; see the compiler error output for
    details.

    Total time: 0 seconds

    C:\simple-webapp>ant final
    Buildfile: C:\simple-webapp\build.xml

    final:

    clean:
    [echo] Deleting existing directories
    [delete] Deleting directory C:\simple-webapp\dist
    [delete] Deleting directory C:\simple-webapp\build\JavaWebApp

    init:
    [echo] Creating directories
    [mkdir] Created dir: C:\simple-webapp\dist
    [mkdir] Created dir: C:\simple-webapp\build\JavaWebApp\WEB-INF\classes
    [mkdir] Created dir: C:\simple-webapp\build\JavaWebApp\WEB-INF\lib

    compile:
    [echo] Compiling source files
    [javac] C:\simple-webapp\build.xml:54: warning: ‘includeantruntime’ was not
    set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 4 source files to C:\simple-webapp\build\JavaWebApp\WEB-IN
    F\classes
    [javac] warning: [options] bootstrap class path not set in conjunction with
    -source 1.5
    [javac] C:\simple-webapp\src\com\webapp\dao\UserProfileDAO.java:5: error: pa
    ckage javax.servlet does not exist
    [javac] import javax.servlet.*;
    [javac] ^
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:5: e
    rror: package javax.servlet does not exist
    [javac] import javax.servlet.*;
    [javac] ^
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:6: e
    rror: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.*;
    [javac] ^
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:12:
    error: cannot find symbol
    [javac] public class ControllerServlet extends HttpServlet {
    [javac] ^
    [javac] symbol: class HttpServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:17:
    error: cannot find symbol
    [javac] private ServletContext context;
    [javac] ^
    [javac] symbol: class ServletContext
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:30:
    error: cannot find symbol
    [javac] public void init(ServletConfig config) throws ServletException {

    [javac] ^
    [javac] symbol: class ServletConfig
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:30:
    error: cannot find symbol
    [javac] public void init(ServletConfig config) throws ServletException {

    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:67:
    error: cannot find symbol
    [javac] protected void service(HttpServletRequest request, HttpServletRe
    sponse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:67:
    error: cannot find symbol
    [javac] protected void service(HttpServletRequest request, HttpServletRe
    sponse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:68:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:85:
    error: cannot find symbol
    [javac] protected void doGet(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:85:
    error: cannot find symbol
    [javac] protected void doGet(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:86:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:92:
    error: cannot find symbol
    [javac] protected void doPost(HttpServletRequest request, HttpServletRes
    ponse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:92:
    error: cannot find symbol
    [javac] protected void doPost(HttpServletRequest request, HttpServletRes
    ponse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:93:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:100:
    error: cannot find symbol
    [javac] private void process(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletRequest
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:100:
    error: cannot find symbol
    [javac] private void process(HttpServletRequest request, HttpServletResp
    onse response)
    [javac] ^
    [javac] symbol: class HttpServletResponse
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:101:
    error: cannot find symbol
    [javac] throws ServletException, IOException
    [javac] ^
    [javac] symbol: class ServletException
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:37:
    error: cannot find symbol
    [javac] super.init(config);
    [javac] ^
    [javac] symbol: variable super
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:38:
    error: cannot find symbol
    [javac] context = getServletContext();
    [javac] ^
    [javac] symbol: method getServletContext()
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:82:
    error: cannot find symbol
    [javac] super.service(request, response);
    [javac] ^
    [javac] symbol: variable super
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:103:
    error: cannot find symbol
    [javac] HttpSession session = request.getSession();
    [javac] ^
    [javac] symbol: class HttpSession
    [javac] location: class ControllerServlet
    [javac] C:\simple-webapp\src\com\webapp\servlets\ControllerServlet.java:104:
    error: cannot find symbol
    [javac] RequestDispatcher dispatcher = null;
    [javac] ^
    [javac] symbol: class RequestDispatcher
    [javac] location: class ControllerServlet
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 24 errors
    [javac] 1 warning

    BUILD FAILED
    C:\simple-webapp\build.xml:34: The following error occurred while executing this
    line:
    C:\simple-webapp\build.xml:54: Compile failed; see the compiler error output for
    details.

    Total time: 0 seconds

    C:\simple-webapp>ant final

  3. Looks like ant is not able to find the j2ee jar, which it is picking up from JBoss directory. You need to have JBoss installed first before the compilation. Also, depending upon where you install JBoss and JDK, you may need to update the build.properties file. Sorry I have not checked my post for a very long time.

  4. Hi Amit, I just emailed you the code! thanks -Virendra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s