Spring Boot w/ JSP

Java, maven, programming, Spring Boot

Project Setup

From the CLI or http://start.spring.io/, initialize a project with “web” capability.  What ends up happening is addition of the dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Start with this directory structure. Be sure the controller (and any other Spring beans) are located under where the Application class (JspDemoApplication in this example) is.

...
  src/
    main/
      java/
        org/
          van/
            jspdemo/
              JspDemoApplication.java
              controller/
                DemoController.java
      resources/
        application.properties
      webapp/
        WEB-INF/
          jsp/
            demoindex.jsp

Controller and View

Add a controller file DemoController.java (see tree above for location):

...

@Controller
@RequestMapping("/jspdemo")
class DemoController { 
  @RequestMapping("/index")
  public String index(@RequestParam("name") String name, Model model) {
    model.addAttribute(name.toUpperCase());
    return "demoindex";  // view/JSP name
  }
}

Then the view file demoindex.jsp (see tree above for location). NOTE that the webapp/WEB-INF/jsp subdirectory needs to be created.

<html>
  <div>
    Hello, ${name}.
  </div>
</html>

Add support for JSP

Add these dependencies:

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <scope>provided</scope>
</dependency>

Add these to application.properties:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

SampleServlet: Java web app starter

Java, jetty, maven, programming

Prerequisite

Maven 3.x installed – try http://maven.apache.org/download.cgi
Important: earlier versions of Maven, such as 2.x, won’t work correctly

Set-up

Locate a subdirectory and run:

mvn archetype:generate

When prompted, use maven-archetype-quickstart as the archetype to use.  Enter the groupId, artifactId, version, etc.

Add dependencies and plugins into pom.xml

A relatively useless pom.xml file is generated.  Modify it to include some dependencies and plugins:

<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>org.van</groupId>
<artifactId>sampleweb</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>sampleweb</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

<build>
<finalName>sampleweb</finalName>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
</configuration>
</plugin>

</plugins>
</build>
</project>

Add web.xml per Java Servlet specs

Create the subdirectory src/main/webapp/WEB-INF/ and add into it <strong?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">
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>org.van.SampleServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/SampleServlet/*</url-pattern>
</servlet-mapping>
</web-app>

Create SampleServlet.java

Under src/main/java/, delete the App.java generated and add the servlet file under the appropriate package subdirectory (e.g. “org/van/SampleServlet.java”):

package org.van;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SampleServlet extends HttpServlet {
/*
Redirects to some other page. Sample usage:
http://localhost:8080/SampleServlet?destination=http://www.therealvan.com
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {

String dest = req.getParameter("destination");
resp.sendRedirect(dest);
}
}

Run

Just invoke the Jetty run command:

mvn jetty:run

Zip of starter project

Here is a zip of the starter project to extract wherever to get started:
sampleweb.zip

Details

The sample project uses the following:

  • Java Servlet 2.5 API
  • Maven compiler plugin
  • jetty-maven-plugin