How To Redirect from JSP to Servlet?

Redirecting from a JSP page to a servlet is a common task in Java web applications. This process allows you to control the flow of your web application and pass information from one component to another. In this blog post, we will explore different methods to achieve this redirection.

Methods to Redirect from JSP to Servlet

There are two primary methods to redirect from a JSP page to a servlet:

  1. Client-side Redirection (Using JavaScript)
  2. Server-side Redirection (Using form action or HttpServletResponse.sendRedirect)

Client-side Redirection

Client-side redirection can be achieved using JavaScript. This method is straightforward but not recommended for critical redirections since it relies on the client's browser to execute the JavaScript code.

Example: Using JavaScript

In this example, we'll create a simple JSP page that uses JavaScript to redirect to a servlet.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Client-side Redirect</title>
    <script type="text/javascript">
        function redirectToServlet() {
            window.location.href = "myServlet";
        }
    </script>
</head>
<body>
    <h1>Client-side Redirect</h1>
    <button onclick="redirectToServlet()">Redirect to Servlet</button>
</body>
</html>

In this example:

  • A button is provided to trigger the redirection.
  • The redirectToServlet JavaScript function changes the window.location.href to the servlet's URL pattern (myServlet).

Server-side Redirection

Server-side redirection is more reliable and commonly used. It can be achieved by either submitting a form to the servlet or using the HttpServletResponse.sendRedirect method.

Example 1: Using a Form Action

In this example, we use a form with an action attribute that points to the servlet.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Form Redirect</title>
</head>
<body>
    <h1>Form Redirect</h1>
    <form action="myServlet" method="post">
        <input type="submit" value="Redirect to Servlet">
    </form>
</body>
</html>

In this example:

  • A form is used with the action attribute set to the servlet's URL pattern (myServlet).
  • When the form is submitted, the request is sent to the servlet.

Example 2: Using HttpServletResponse.sendRedirect

Another way to redirect from a JSP page to a servlet is by using the HttpServletResponse.sendRedirect method within a servlet.

Step 1: Create a JSP Page

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Server-side Redirect</title>
</head>
<body>
    <h1>Server-side Redirect</h1>
    <form action="redirectServlet" method="post">
        <input type="submit" value="Redirect to Servlet">
    </form>
</body>
</html>

Step 2: Create a Servlet to Handle the Redirection

package com.example;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("targetServlet");
    }
}

Step 3: Create the Target Servlet

package com.example;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/targetServlet")
public class TargetServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().println("<h1>You have been redirected!</h1>");
    }
}

In this example:

  • The JSP form submits a POST request to redirectServlet.
  • The RedirectServlet handles the request and uses response.sendRedirect("targetServlet") to redirect to targetServlet.
  • The TargetServlet responds with a message confirming the redirection.

Conclusion

Redirecting from a JSP page to a servlet can be achieved using both client-side and server-side methods. Client-side redirection is easy to implement with JavaScript, but server-side redirection is more reliable and commonly used in web applications. By using form actions or the HttpServletResponse.sendRedirect method, you can efficiently control the flow of your web application.

For more detailed information on HttpServletResponse, refer to the Jakarta Servlet API documentation.

Related Servlet Posts

Comments