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);
    }
%>


Asked by: Marcus886 | Posted: 23-01-2022






Answer 1

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>");

Answered by: Lucas355 | Posted: 24-02-2022



Answer 2

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.

Answered by: Aida531 | Posted: 24-02-2022



Answer 3

A simple approach:

<%@page errorPage="Error.jsp" %>

<%
 String errorMessage = SomeClass.getInstance().doSomething();
 if (errorMessage != null) {
       throw new Exception(errorMessage); // Better throw the exception from doSomething()
 }
 pageContext.forward("index.jsp");
%>


Error.jsp
.........
<%@ page isErrorPage='true' %>
<%
out.print("Error!!!");  
out.print(exception.getMessage());
%>

Better approach:

Invoke the doSomething() from a servlet. Set your error page in web.xml

<error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/jsp/Error.jsp</location>
</error-page>

Answered by: Grace577 | Posted: 24-02-2022



Similar questions

java - JSP:forward Question

I am just getting started on web app development. I have an index.jsp that has just ONE line. &lt; jsp:forward page=&quot;landing.do?&quot;/&gt; What does the above line do? page=&quot;landing.do?&quot; actually refer to? what does the question mark &quot;?&quot; next to &quot;la...


java - jsp:forward tag forwards to Struts 2 action

I am migrating a Struts application to Struts 2. It was developed by Struts 1.2 four years ago. My question is: In my JSP, there is such a statement: &lt;jsp:forward page=&quot;/a.do&quot; /&gt; It works well in Struts 1, but does not work in Struts 2, it tells me HTTP 404 error when I was accessing this JSP file. However, if I access to http://lo...


java - sending parameter from JSP to PHP useing JSP:forward

I need to send a parameter named "Name" from JSP to an other php page.I have inserted the following code in my jsp but i don't know how to read this parameter in php. &lt;jsp:forward page="secondpage.php"&gt; &lt;jsp:param name="param1" value="&lt;%= Name %&gt;" /&gt; &lt;/jsp:forward&gt; I would be thankfull for every answer or suggestion.


java - jump from one jsp to another without using jsp:forward

I need to jump from first JSP page to second JSP page without using jsp:forward. The reason I want to do this is, whenever I refresh my second page I get a confirmation message asking "do you wish to re-submit form". Also the URL of the second page shows as first page's URL. Also I want this to happen without clicking anywhere. Below is what I have did. I need to do something else: &lt;jsp:forward page="hom...


java - Use jsp:forward to forward to another page

I am learning about jsp and servlet. In an online tutorial, he used &lt;jsp: forward&gt; to move to another jsp page. &lt;jsp:forward page=&quot;home&quot;&gt;&lt;/jsp:forward&gt; The directory tree: (eclipse) The directory tree I don't understand why when I run the program, the index.jsp file in the h...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top