Monday, November 21, 2011

JSP - HelloWorld

In your HelloWorld project, create a directory /web-content/jsp
In the /jsp directory, create a file HelloWorld.jsp

Write (or copy/paste) the following in this file:
 <html>  
      <head>  
      </head>  
      <body>  
           <p>Hello World!</p>  
      </body>  
 </html>  


As you can see, this JSP is no different from an HTML page.

Update your Ant:dist target in build.xml to include this JSP in the war:
 <project name="HelloWorld" xmlns:ivy="antlib:org.apache.ivy.ant" >  
     
   <target name="resolve" description="--> retrieve dependencies with ivy">  
     <ivy:retrieve />  
   </target>  
   
      <target name="init" depends="resolve">  
           <mkdir dir="classes"/>  
           <mkdir dir="target"/>  
      </target>  
   
      <target name="compile" depends="init">  
           <javac srcdir="." destdir="classes">  
                <classpath>  
                 <pathelement location="lib/servlet-api-2.5.jar"/>  
                </classpath>  
           </javac>  
      </target>  
   
      <target name="dist" depends="compile">  
           <war destfile="target/HelloWorld.war" webxml="web.xml">  
                 <classes dir="classes"/>  
                 <lib dir="lib"/>  
                 <fileset dir="web-content"/>  
           </war>  
      </target>  
 </project>  


Run the Ant:dist target to create a new HelloWorld.war

Deploy it to Tomcat (you may need to stop the server, delete the exploded HelloWorld.war then restart).

Point your broser to:
http://localhost:8080/HelloWorld/jsp/HelloWorld.jsp

You should see 'Hello World!' - this time rendered by the JSP.

Note that because the /jsp directory is outside the WEB-INF, it is a public directory - so we are able to directly access its contents (HelloWorld.jsp)

No comments: