Professional Documents
Culture Documents
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
<!-- =================================================================== --> <!-- 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>
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:
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:
Cookies = [Ljavax.servlet.http.Cookie;@cf6046
JSESSIONID : A42E267113E08B4DA3837C9C7515BC0D date : Thu Oct 09 09:15:47 CEST 2003 nom : janaudy sid : A42E267113E08B4DA3837C9C7515BC0D
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.