55

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,585
  • 14
  • 54
  • 67
PROXY
  • 619
  • 1
  • 5
  • 4
  • 2
    Why do you need a redirect? The usual way to do it is to submit to a servlet that forwards (not redirects) to a jsp. – Christoffer Hammarström Feb 11 '11 at 13:02
  • Submit the page as a form, via the action parameter. – access_granted Mar 29 '18 at 01:20
  • *(literally `lol`)*. As i was clicking the linking to view this page i thought: *Just wait, someone's going to comment "Why do you want to do that?"*. Why would would someone want to issue `303 See Other`? Golly, [i can't imagine](https://stackoverflow.com/a/58492986/12597). Stay classy SO. – Ian Boyd Jul 26 '22 at 21:01

6 Answers6

119
<%
    String redirectURL = "http://whatever.com/myJSPFile.jsp";
    response.sendRedirect(redirectURL);
%>
oopbase
  • 11,157
  • 12
  • 40
  • 59
  • 7
    Scriplet is a very bad decision. – user Nov 06 '12 at 16:42
  • 92
    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
  • Warning: This issues `302 Found` (HTTP standard requires browsers to keep the verb they were using, but every brower actually switches to `GET` when receiving `302` - which is why `302` was replaced.) If you want the browser to switch to `GET` you must send `303 See Other`. If you want the browser to keep the verb it was using (e.g. `PUT`), and just try again at the new URL, you need to send `307 Temporary Redirect`. – Ian Boyd Jul 26 '22 at 21:11
35

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

<c:redirect url="/home.html"/>
Community
  • 1
  • 1
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
12

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,082,665
  • 372
  • 3,610
  • 3,555
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
  • 12,507
  • 5
  • 54
  • 54
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
  • 3,050
  • 1
  • 27
  • 37
0

This should do the trick.

Click --> Submit Button on the First page

Add the below Code into Second Page, it will redirect to home.html page.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:redirect url="home.html"/>
</body>
</html>
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51