Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendRedirect() doens't work

Tags:

java

jsp

servlets

I looked for a solution on this forum, but i didn't find anything that suits my problem.

I have a very simplce code

a jsp page

<html>
<body>
<jsp:include page="/servletName"/>
</body>
</html>

and a servlet

@WebServlet("/servletName")
public class reindirizzaController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public reindirizzaController() {
        super();        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String redirectURL = "http://www.google.it";
      response.sendRedirect(redirectURL);       
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

No redirect is done. I'm stuck in the jsp page and i get no error. I also tried to add return; after the response.

like image 958
MDP Avatar asked Apr 28 '26 17:04

MDP


1 Answers

Since you are calling the servlet through Include, it does not make you redirect. It simply ignores.

From docs of include()

includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

like image 171
Suresh Atta Avatar answered Apr 30 '26 08:04

Suresh Atta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!