53

I have to design several pages in jsp. After clicking on the submit button on the first page, the page should be automatically redirected to the second page.

Can you help with a quick example or a link to a tutorial that demonstrates how to implement this?

River
  • 8,031
  • 13
  • 51
  • 64
PROXY
  • 599
  • 1
  • 4
  • 4

5 Answers5

118
<%
    String redirectURL = "http://whatever.com/myJSPFile.jsp";
    response.sendRedirect(redirectURL);
%>
oopbase
  • 10,869
  • 12
  • 38
  • 59
  • 7
    Scriplet is a very bad decision. – user Nov 06 '12 at 16:42
  • 85
    It's more helpful to say what's good than to say what's bad. What should be used instead of a scriptlet? – twiz Jun 12 '13 at 22:20
  • 5
    This was my first result in google for "redirect jsp". This is the correct answer for those people who came looking for how to always redirect one page to another (ex. to map /index.jsp -> /myapp/index.jsp, put this in to /index.jsp and make redirectURL = "/myapp/index.jsp"). Constantine is correct that this is a bad way to redirect a submit button. The right way for a button is to either use a
    tag or javascript, depending on your architecture.
    – Ryan Shillington Nov 01 '13 at 15:27
  • 3
    Just a heads up - this worked for me, but I had to remove any newlines after `return;`. It ended up looking like this: `response.sendRedirect(redirectURL);%>` (not even a newline after `%>`) – Wisco crew Mar 24 '14 at 19:43
  • Actually, I spoke too soon. Having `response.sendRedirect(redirectURL);\nreturn;\n%>` works for me too. – Wisco crew Mar 24 '14 at 20:11
  • The admin at whatever.com must be thinking _"where do all these myJSPFile.jsp calls come from?"_ – Stu Thompson Nov 09 '17 at 16:13
  • @StuThompson True. And I'm still wondering why people find this post in 2017 :-). – oopbase Nov 09 '17 at 16:23
32

This answer also contains a standard solution using only the jstl redirect tag:

<c:redirect url="/home.html"/>
Community
  • 1
  • 1
SpaceTrucker
  • 12,426
  • 6
  • 54
  • 97
11

Just define the target page in the action attribute of the <form> containing the submit button.

So, in page1.jsp:

<form action="page2.jsp">
    <input type="submit">
</form>

Unrelated to the problem, a JSP is not the best place to do business stuff, if you need to do any. Consider learning servlets.

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
2

Hello there: If you need more control on where the link should redirect to, you could use this solution.

Ie. If the user is clicking in the CHECKOUT link, but you want to send him/her to checkout page if its registered(logged in) or registration page if he/she isn't.

You could use JSTL core LIKE:

<!--include the library-->
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>

<%--create a var to store link--%>
<core:set var="linkToRedirect">
  <%--test the condition you need--%>
  <core:choose>
    <core:when test="${USER IS REGISTER}">
      checkout.jsp
    </core:when>
    <core:otherwise>
      registration.jsp
    </core:otherwise>
  </core:choose>
</core:set>

EXPLAINING: is the same as...

 //pseudo code
 if(condition == true)
   set linkToRedirect = checkout.jsp
 else
   set linkToRedirect = registration.jsp

THEN: in simple HTML...

<a href="your.domain.com/${linkToRedirect}">CHECKOUT</a>
T04435
  • 10,027
  • 3
  • 49
  • 51
1

Extending @oopbase's answer with return; statement.

Let's consider a use case of traditional authentication system where we store login information into the session. On each page we check for active session like,

/* Some Import Statements here. */

if(null == session || !session.getAttribute("is_login").equals("1")) {
    response.sendRedirect("http://domain.com/login");
}

// ....

session.getAttribute("user_id");

// ....
/* Some More JSP+Java+HTML code here */

It looks fine at first glance however; It has one issue. If your server has expired session due to time limit and user is trying to access the page he might get error if you have not written your code in try..catch block or handled if(null != session.getAttribute("attr_name")) everytime.

So by putting a return; statement I stopped further execution and forced to redirect page to certain location.

if(null == session || !session.getAttribute("is_login").equals("1")) {
    response.sendRedirect("http://domain.com/login");
    return;
}

Note that Use of redirection may vary based on the requirements. Nowadays people don't use such authentication system. (Modern approach - Token Based Authentication) It's just an simple example to understand where and how to place redirection(s).

Hardik Thaker
  • 2,970
  • 1
  • 23
  • 35