A simple test application for JSTL 

Joined:
08/13/2009
Posts:
164

March 23, 2010 03:50:11    Last update: August 18, 2010 21:59:52
This is a simple web application with a single servlet and a single JSP page. It is intended to be a test bed for JSTL tags. You may want to store all syntax, rules, and exceptions in your head, but in my opinion nothing beats a simple test program that allows you play with it all you want. So here it is (build with Maven).

Prerequisites:


Steps:
  1. The directory layout
    ./pom.xml
    ./src
    ./src/main
    ./src/main/java
    ./src/main/java/jstl
    ./src/main/java/jstl/demo
    ./src/main/java/jstl/demo/JSTLDemoServlet.java
    ./src/main/resources
    ./src/main/webapp
    ./src/main/webapp/home.jsp
    ./src/main/webapp/index.jsp
    ./src/main/webapp/WEB-INF
    ./src/main/webapp/WEB-INF/web.xml
    

  2. Contents of pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>jstl-demo</groupId>
        <artifactId>jstl-demo</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>jstl-demo Maven Webapp</name>
        <url>http://maven.apache.org</url>
    
        <repositories>
    	<repository>
    	    <id>maven2-repository.dev.java.net</id>
    	    <name>Java.net Repository for Maven</name>
    	    <url>http://download.java.net/maven/2/</url>
    	    <layout>default</layout>
    	</repository>
        </repositories>
    
        <dependencies>
    	<dependency>
    	    <groupId>javax</groupId>
    	    <artifactId>javaee-api</artifactId>
    	    <version>6.0-SNAPSHOT</version>
    	    <scope>provided</scope>
    	</dependency>
    
    	<dependency>
    	    <groupId>javax.servlet</groupId>
    	    <artifactId>jstl</artifactId>
    	    <version>1.2</version>
    	    <scope>compile</scope>
    	</dependency>
        </dependencies>
    
        <build>
    	<finalName>jstl-demo</finalName>
        </build>
    </project>
    

  3. JSTLDemoServlet
    package jstl.demo;
    
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class JSTLDemoServlet extends HttpServlet {
        public void service(HttpServletRequest req,
    		        HttpServletResponse resp) 
    		throws IOException, ServletException {
    	req.setAttribute("world", "John Doe");
    	RequestDispatcher d = req.getRequestDispatcher("home.jsp");
    	d.forward(req, resp);
        }
    }
    

  4. The contents of home.jsp:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>JSTL Test Page</title>
    </head>
    
    <body>
    Hello <c:out value="${world}"/>!
    </body>
    </html>
    

  5. index.jsp
    <html>
    <head>
    <title>Index</title>
    </head>
    
    <body>
    <h2>Hello World!</h2>
    
    <script type="text/javascript">
        window.location = "demo";
    </script>
    </body>
    </html>
    

  6. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
    	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	 version="2.5">
        <display-name>Simple JSTL Demo Application</display-name>
        <servlet>
            <servlet-name>jstldemo</servlet-name>
            <servlet-class>jstl.demo.JSTLDemoServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>jstldemo</servlet-name>
            <url-pattern>/demo</url-pattern>
        </servlet-mapping>
    </web-app>
    


Share |
| Comment  | Tags