Spring Page Redirection Example

In this following example show how to write a simple web based application which makes use of redirect to transfer a http request to another another page. To start with it, let us have working STS IDE in place and follow the following steps to develop a Dynamic Form based Web Application using Spring Web Framework:

Step 1: Create a Dynamic Web Project with a name SpringMVCRedirectApp and create a package com.dineshonjava.controller under the src folder in the created project.

Step 2: Add below mentioned Spring 3.0 libraries and other libraries into the folder WebRoot/WEB-INF/lib

  • commons-logging-1.1.1.jar
  • org.springframework.asm-3.0.0.jar
  • org.springframework.beans-3.0.0.jar
  • org.springframework.context-3.0.0.jar
  • org.springframework.core-3.0.0.jar
  • org.springframework.expression-3.0.0.jar
  • org.springframework.web.servlet-3.0.0.jar
  • org.springframework.web-3.0.0.jar
  • spring-web.3.0.0.jar

 Step 3: Create a Java class WebRedirectController under the com.dineshonjava.controller package.

Step 4: Create Spring configuration files Web.xml and sdnext-servlet.xml under the WebRoot/WEB-INF/ folder.

Step 5: Create a sub-folder with a name views under the WebRoot/WEB-INF folder. Create a view file welcome.jsp and final.jsp under this sub-folder.

Step 6: The final step is to create the content of all the source and configuration files name sdnext-servlet.xml under the sub-folder WebRoot/WEB-INF/config and export the application as explained below. Software versions used to run the sample code:

  • Spring 3.0
  • Java 1.6
  • Tomcat 7
  • JSTL 1.2
  • STS Java EE IDE
                                              Web Application Structure:

WebRedirectController.java

package com.dineshonjava.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Dinesh Rajput
 *
 */
@Controller
public class WebRedirectController {
   @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String welcome() {
 return "welcome";
    }
    
   @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirect() {
 return "redirect:finalPage";
    }
    
    @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
    public String finalPage() {
     return "final";
   }
}

Spring Web configuration file web.xml

<web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name>SpringMVCHelloWorld</display-name>
  <welcome-file-list>
  <welcome-file>/</welcome-file>
 </welcome-file-list>
 
    <servlet>
        <servlet-name>sdnext</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sdnext</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

another Spring Web configuration file sdnext-servlet.xml

<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven></mvc:annotation-driven>

 
<context:component-scan base-package="com.dineshonjava.controller">
</context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
 <property name="prefix" value="/WEB-INF/views/"></property>
 <property name="suffix" value=".jsp"></property>
</bean>

</beans>

Following is the content of Spring view file welcome.jsp. This will be a landing page, this page will send a request to access redirect service method which will redirect this request to another service method and finally a final.jsp page will be displayed.
welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>
<head>
 
 <title>Insert title here</title>
</head>
<body>
 <h2>
Spring Page Redirection</h2>
Click below button to redirect the result to new page
<form:form action="/sdnext/redirect" method="GET">
<table><tbody>
<tr>    <td><input type="submit" value="Redirect Page" /></td>   </tr>
</tbody></table>
</form:form>
</body>
</html>

Following is the content of Spring view file final.jsp. This is the final redirected page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
 <head>
  
  <title>>Spring Page Redirection</title>
 </head>
 <body>
  <h2>
Redirected Page</h2>
</body>
</html>

Once you are done with creating source and configuration files, export your application. Right click on your application and use Export > WAR File option and save your SpringMVCRedirectApp.war file in Tomcat’s web apps folder.

Now start your Tomcat server and make sure you are able to access other web pages from web apps folder using a standard browser. Now try a URL http://localhost:8080/sdnext/welcome and you should see the following result if everything is fine with your Spring Web Application:

Now click on “Redirect Page” button to submit the form and to get final redirected page. You should see the following result if everything is fine with your Spring Web Application:

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago