Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsp:forward in Java without using JSP tag?

Is there a pure-Java equivalent to <jsp:forward page="..." /> that I can use within a <% ... %> block?

For example, I currently have a JSP page something like this:

<%
    String errorMessage = SomeClass.getInstance().doSomething();
    if (errorMessage != null) {
        session.setAttribute("error", errorMessage);
%>
<jsp:forward page="error.jsp" />
<%
    } else {
        String url = response.encodeRedirectURL("index.jsp");
        response.sendRedirect(url);
    }
%>

Having to break the <% ... %> block to use the jsp:forward is ugly and makes it harder to read due to indentation, among other things.

So, can I do the forward in the Java code without use the JSP tag?

Something like this would be ideal:

<%
    String errorMessage = SomeClass.getInstance().doSomething();
    if (errorMessage != null) {
        session.setAttribute("error", errorMessage);
        someObject.forward("error.jsp");
    } else {
        String url = response.encodeRedirectURL("index.jsp");
        response.sendRedirect(url);
    }
%>
like image 233
Chris Carruthers Avatar asked Oct 28 '08 23:10

Chris Carruthers


People also ask

How do I forward one jsp to another?

To forward a request from one page to another JSP page we can use the <jsp:forward> action. This action has a page attribute where we can specify the target page of the forward action. If we want to pass parameter to another page we can include a <jsp:param> in the forward action.

What is the difference between jsp forward action tag and include action tag?

In short include, action is used to include contents of another Servlet, JSP, or HTML files, while the forward action is used to forward the current HTTP request to another Servlet or JSP for further processing.

What is the use of jsp forward?

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource.

Which tag is used to send the control from one jsp to the other?

JSP forward action tag is used for forwarding a request to the another resource (It can be a JSP, static page such as html or Servlet). Request can be forwarded with or without parameter. In this tutorial we will see examples of <jsp:forward> action tag.


2 Answers

The someObject you are looking for is pageContext.

This object is implicitly defined in JSP, so you can use it like this:

pageContext.forward("<some relative jsp>");
like image 87
Adam Avatar answered Nov 04 '22 06:11

Adam


You really should try and avoid scriplets if you can, and in your case, a lot of what you are doing can be replaced with JSTL code. The following replacement for your example is much cleaner, IMO:

<%
  // Consider moving to a servlet or controller/action class
  String errorMessage = SomeClass.getInstance().doSomething();
  pageContext.setAttribute("errorMessage", errorMessage);
%>
<c:choose>
  <c:when test="${not empty errorMessage}">
    <c:set var="error" scope="session" value="${errorMessage}" />
    <jsp:forward page="error.jsp" />
  </c:when>
  <c:otherwise>
    <c:redirect url="index.jsp" />
  </c:otherwise>
</c:choose>

Ideally, you'd modify error.jsp so that the error message doesn't even need to be set in the session, but I didn't want to change your design too much.

like image 28
Jack Leow Avatar answered Nov 04 '22 07:11

Jack Leow