🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Methods to Redirect from JSP to Servlet
There are two primary methods to redirect from a JSP page to a servlet:
- Client-side Redirection (Using JavaScript)
- 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
redirectToServletJavaScript function changes thewindow.location.hrefto 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
actionattribute 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
RedirectServlethandles the request and usesresponse.sendRedirect("targetServlet")to redirect totargetServlet. - The
TargetServletresponds 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
- What is a Servlet in Java?
- Servlet Life Cycle
- Servlet Interface Example
- GenericServlet Class Example
- HttpServlet Class Example Tutorial
- HttpServlet doGet() Method Example
- HttpServlet doPost() Method Example
- @WebServlet Annotation Example
- @WebInitParam Annotation Example
- @WebListener Annotation Example
- @WebFilter Annotation Example
- @MultipartConfig Annotation Example
- How to Return a JSON Response from a Java Servlet
- Servlet Registration Form + JDBC + MySQL Database Example
- Login Form Servlet + JDBC + MySQL Example
- Servlet JDBC Eclipse Example Tutorial
- JSP Servlet JDBC MySQL CRUD Example Tutorial
- Servlet + JSP + JDBC + MySQL Example
- Registration Form using JSP + Servlet + JDBC + Mysql Example
- Login Form using JSP + Servlet + JDBC + MySQL Example
- JSP Servlet Hibernate CRUD Example
- JSP Servlet Hibernate Web Application
- Hibernate Registration Form Example with JSP, Servlet, MySQL
- Login Form using JSP + Servlet + Hibernate + MySQL Example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment