You are on page 1of 6

JavaServer Pages - Servlets - MVC Pattern Objectives This lab's objective is to understand JavaServer Page and Servlet technologies

by building a simple HTML form that sends a HTTP POST request to the application server (jBoss bundles Tomcat) We will use JSP for the presentation layer. We will create the web.xml descriptor as well as the WAR (Web ARchive) file. We will also use cookies to manage the HTTP session. Finally, we will understand and apply the MVC pattern. Folders

1. HTML -> Servlet via POST HTML Form Develop a HTML form and place it in the folder at the same level as WEB-INF.
Nom : Prnom : Email : Date de naissance : Adresse :
Valider Annuler

Servlet

Develop a servlet (class that extends HttpServlet) and override the method doPost. Get the incoming parameters via the method getParameter of the class HttpServletRequest. Set the "content-type" of the reply to "text/html" (Using "image/jpg" you could also generate an image dynamically on the server side and display it in the browser). Using PrintWriter (from HttpServletResponse), write the HTML code to send back to the browser in order to display the data filled in by the user. web.xml
<web-app> <display-name>JspServMVC</display-name> <!-- Action Servlet Configuration --> <servlet> <servlet-name>WhoAmIServlet</servlet-name> <servlet-class>org.jyperion.j2ee.jspservmvc.WhoAmIServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WhoAmIServlet</servlet-name> <url-pattern>WhoAmIServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

build.properties
#################### # J2EE Libs Folder # #################### jboss.home=/Users/janaudy/Softwares/jboss-3.2.2RC4/ jboss.deploy=${jboss.home}/server/default/deploy/ ejb.libs=/Users/janaudy/Softwares/jboss-3.2.2RC4/server/default/lib ################ # XDoclet Libs # ################ xdoclet.libs=/Users/janaudy/Softwares/xdoclet-bin-1.2b3/lib ################## # Modules and co # ################## base=.. ear-module=${base}/ear-module

client-module=${base}/client-module client-meta-inf=${client-module}/META-INF ejb-module=${base}/ejb-module ejb-meta-inf=${ejb-module}/META-INF web-module=${base}/web-module web-classes=${web-module}/WEB-INF/classes src=${base}/src gen-src-folder=${base}/gen-src

build.xml Execute ANT: ant -f ./build.xml in the build folder


<?xml version="1.0"?> <project name="JspServMVC" default="all" basedir=".."> <property file="build.properties"/> <!-- =================================================================== --> <!-- Define the class path --> <!-- =================================================================== --> <path id="my.class.path"> <fileset dir="${ejb.libs}"> <include name="*.jar"/> </fileset> </path> <!-- =================================================================== --> <!-- Initialise --> <!-- =================================================================== --> <target name="init"> <tstamp> <format property="TODAY" pattern="d-MM-yy"/> </tstamp> </target> <!-- =================================================================== --> <!-- Prepares the directory structure --> <!-- =================================================================== --> <target name="prepare" depends="init"> </target>

<!-- =================================================================== --> <!-- Compiles Java source code --> <!-- =================================================================== --> <target name="compile" depends="prepare"> <echo>+---------------------------------------------------+</echo> <echo>| |</echo> <echo>| C O M P I L I N G S O U R C E S |</echo> <echo>| |</echo> <echo>+---------------------------------------------------+</echo> <javac destdir="${web-classes}" classpathref="my.class.path" debug="on" deprecation="on" optimize="off"> <src path="${src}"/> </javac> </target> <!-- =================================================================== --> <!-- Main --> <!-- =================================================================== --> <target name="jar"> <echo>Jaring ...</echo> <jar destfile="jspservmvc.war" basedir="${web-module}"/> </target> <!-- =================================================================== --> <!-- Deploy --> <!-- =================================================================== --> <target name="deploy"> <echo>Deploying WAR file to jBoss ...</echo> <copy file="jspservmvc.war" todir="${jboss.deploy}"/> </target> <target name="war" depends="compile, jar"/> <target name="all" depends="compile, jar, deploy"/> </project>

To see the result, click on http://localhost:8080/jspservmvc/ :

Vous avez tape:


Nom: Janaudy Prenom: Thierry Email:janaudy@jyperion.org Date de naissance: 05-11-1972 Adresse: Nimes

2. Session We can easily store some data for a specific user in a session object. In order to achieve this, use the HttpSession object. (You get it from HttpServletRequest. Using HttpSession, you will save the number of calls the user has made to the servlet:

Vous avez tape (3 fois):


Nom: Janaudy Prenom: Thierry Email:janaudy@jyperion.org Date de naissance: 05-11-1972 Adresse: Nimes

3. JSP -> Servlet via POST with cookies Let's introduce a dynamic behaviour by using JSP pages. Re-write the index.html file in an index.jsp. Modify the servlet so that for each request, a Cookie is saved on the client side. This Cookie contains the following information: the user name, the session id, and the last execution time of the request. Retrieve the stored information in the index.jsp and display them to obtain:

Nom : Prenom : Email : Date de naissance : Adresse :


Valider Annuler

Cookies = [Ljavax.servlet.http.Cookie;@cf6046

JSESSIONID : A42E267113E08B4DA3837C9C7515BC0D date : Thu Oct 09 09:15:47 CEST 2003 nom : janaudy sid : A42E267113E08B4DA3837C9C7515BC0D

4. MVC pattern: Model-View-Controller

This article (JavaWorld) explains you in details the MVC pattern.. Struts implements this pattern. Exercise Develop a HTML page mvc.html that contains a form like the one before. Develop a servlet that gets the HTTP POST request and that instantiates a JavaBean component to store the data. Using the RequestDispatcher class (from ServletContext), forward the request to a JSP page that will use this JavaBean instance in order to display its data using the JSP standard Tag Libs.

You might also like