The Ninth Lab

JSP Programming & Tomcat

Octy 30, 2015


  •         1) What is JSP (JavaServer Pages):
                    Dynamic Web Pages (Web content is not fixed)
                    Server-Side Scripting (Server controls the Web construction)
                    A JSP page is basically a web page with traditional HTML and bits of Java code (Inside JSP or Outside JSP).
                    In a JSP page, <% ... %> encloses Java code.

  •         2) How JSP runs:
                    a) When JSP page is called, it will be compiled (by the JSP Engine ) into a Java servlet.
                            JSP files along with java code are compiled into * and *_jsp.class files
                            In Tomcat, you can find those java servlet files in /apache-tomcat-X.X.XX/work/Catalina/localhost/...
                    b) The servlet is loaded and executed (by the Servlet Container ) to create dynamic HTML to be sent to the browser.

  •         3) What is Tomcat:
                    Tomcat is the web server and servlet container
                            Jasper is the JSP engine in Tomcat
                            Catalina is the JAVA servlet container in Tomcat
                    Beside Tomcat , you may also choose Jetty , Jboss(Wildfly) , and so on.

  •         4) What is Ant:
                    Ant is a build tool
                    Ant uses XML file to describe the build process.
                    If there is no argument:
                            Ant looks for a build.xml file in the current directory
                            Ant runs the target in the default attribute of the < project > tag.
                    Use "-f FILENAME" to specify the build file
                    We use "ant -f FILENAME all" to runs the 'all' target

(II) About PA1

  •         1) What if Jasper does not re-compile the jsp files:
                    a) Stop the Catalina server.
                    b) Delete the photoshare folder in webapps folder
                    c) Re-compile your code using ant
                    d) Re-copy the photoshare.war file to webapps folder
                    e) Start the Catalina server.

  •         2) What if you cannot stop Catalina normally:
                    a) Use ps ux to show your processes
                            If you start the Catalina server, you should find processes with command as "java -Djava.util...."
                    b) Get the PIDs in the second column of those processes
                    b) Use kill -9 PID to terminate those processes

  •         3) What if .bashrc does not work:
                    a) Use ls -a to check whether you have the file .bash_profile
                            If you do not have .bash_profile , create the .bash_profile file with content Here
                    b) Use source ~/.bashrc to load bash file manually
                    c) You can even customize the bash prompt. ( Tutorial )
                            For example: Include the following content into .bashrc to make prompt colorful

  •         4) You can use shell script for update:
                    A sample shell script is Here
                            a) Download the script file
                            b) Change PA1DIR directory in the script file
                            c) Use ./ to execute the shell script
                    The script file execute the following commands:
                            a) Stop the Catalina Server
                            b) Re-compile the Code from Photoshare
                            c) Remove the previous files in Tomcat
                            d) Copy the updated war file
                            e) Start the Catalina Server
                    Feel free to change the file to satisfy your requirements

(III) JSP Programming

  •         1) HTML Forms:
                    You can find details about HTML Forms Here
                    a) Input Tag:
                            Input Type
                            Input Name: Variable Name
                            Input Value: Variable Value (Typed or Selected)
                    b) Action Attribute
                            Action specifies where to send the form-data
                            JSP file in PA1
                            Not required for HTML5
                    c) Method Attribute
                            GET: form data is visible
                            POST: submitted data is not visible

  •         2) JavaBeans:
                    a) Use JavaBeans to get information from HTML Forms
                    b) Two tutorials Here and Here
                    c) Define properties in JavaBeans.
                            The first letter of property names should be in lower-case.
                    d) Define getPropertyName() in JavaBeans.
                            The first letter of property name should be translated into upper-case
                    e) Define setPropertyName() in JavaBeans.
                            The first letter of property name should be translated into upper-case
                    f) Use < jsp:useBean id="Variable Name" class="JavaBean Name" / > to use JavaBean classes

  •         3) Data Access Object (Dao):
                    Three-tier Architecture (Presentation tier, Logic tier, Data tier)
                    Write DAO classes to conduct SQL queries
                    Use < %@ page import="*.*Dao" % > to import the DAO classes

(IV) Sample Code for Inserting Dates: