JSP redirecting, AND stop further processing ...

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Hi,

I'm looking for a way of redirecting in jsp ...

I know I can use the following instruction:

<%
response.sendRedirect("errors/error_001.html"); //Just an example
%>

The problem is that before the browser redirects, the complete jsp code is executed ... that means if I have, for example, any action on my database after the redirect command, it get's executed, and then when all jsp processing completes, the redirection is made.

I need something that just flushes the response buffer AND stops ANY jsp processing right after the redirect command.

if after the redirection I put "return", like:

<html>
(...)
<%
if(...) {
response.sendRedirect("errors/error_001.html"); //Just an example
return;
}
out.println("Some text here");
%>
(...)
</html>

I receive the following error:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /inc/GBL_Global.jsp
Generated servlet error:
C:\Program Files\Apache Tomcat 5.0\work\Catalina\localhost\porthos\org\apache\jsp\index_jsp.java:64: unreachable statement
out.println("Some text here");
^
1 error


Any ideas appreciated ... I'm searching all around the web, but with no solution so far ...

Thanks.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
tried?

if()
redirect
else
print...

Also, I think redirection only works using headers so you can't print thigns before you use it.
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
As an example, I have a bunch of jsp's, and all of them start in the same way:

<%@page session = "true" language = "java" import = "java.util.*, (...)"%>

<%@ include file="subdir/some_file.jsp"%>

I want to implement a general validation, like checking for the existence of a config file somewhere on the server. If the file exists, it executes normally, if it doesn't, then it redirects to another page, displaying an error message.

I want to make the validation on the "some_file.jsp", because since it's being included by all jsp files (it has some general info there), the validation would work through all my pages.

I would like to do just one change, and I would prefer not to include a general "if" statement on the jsp's, to avoid changing all of them.

I really would prefer a way of stop jsp processing any further ... I even thought of using
<script language="javascript">
top.location="errors/error_001.html";
</script>

... but since it's client javascript, that doesn't serve my intents at all ... I need to say java to "stop and return to client". The code below will be doing some database changes, so I really need to avoid it.

Do I have any solution besides making a big if/else?

Thanks
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Don't know if you know all this but one of the keys to fixing compilation errors like you're describing is understanding how a jsp is compiled. All your java code inside plain <% %> braces is pasted together into one big method and that is compiled directly by the same compiler that does your regular java. If you want to see how it looks you can usually find the java files that have been produced in some "work" directory somewhere. On Tomcat it's usually "\work\Catalina\localhost\appname\org\apache\jsp". Whatever is in there must be valid real java to compile so you can frequently tell your problem by reading that (or post it here if it's not too big).

Also, once you get that cleared up you're probably going to run into the problem that amdfanboy mentioned. By leaving space between your <%@ page %> and <%@ include %> tags you are sending output to the user (part of the http body) and, depending on how the underlying buffering works you may not be able to send headers anymore. One way to fix that is to not have any space (it all has to be on the same line because the \n counts too). Your some_file.jsp would also not be allowed to produce any output whatsoever.

There are alternatives to response.sendRedirect(). Using the <jsp:forward/> tag does a server side redirect and you don't need to worry about using return; then.

One other thing (sorry for the essay here...): speaking from experience, I would strongly advise against using <%@ include %> to bring in any page that contains dynamic code of any kind. I've had to maintain some horribly complex pages that used this and it was an absolute nightmare. You're really reducing yourself to php style development and jsp/servlets are so much better than that.

One way to do this is <jsp:include />. It's similar to <jsp:forward/> except that processing returns after the other page is done. The major advantage there is that variable names are not shared between the two pages as they would be with <%@ include %>. That may seem like an inconvenience at first but it's something you really need when the pages get big.

My recommended solution though, wouldn't use jsp at all to do this checking. Jsps are meant for formatting output, not containing business logic. A much better idea would be to implement a filter. This is a class that intercepts the request for this page and decides whether or not to allow it to go on. You write it in standard java and configure it in web.xml so the jsp pages themselves never have to be changed when you want to alter the nature of the check. If you aren't familiar with filters and would like to use them I'd be happy to elaborate.
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Thanks for the replies, Kamper I'll have a check on those jsp tags ... meanwhile, would you elaborate or give some link to tutorial on those filter feature?

I really want to have a shot on another way of programming in jsp ... I'm a new comer on the java world, and I'm learning a lot each day ... that stuff you talk seems like a good practice to go for ... I would give it a look, if you point me the way.

Thanks
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I learned about filters from a book so I can't really show you what I learned from. There is a book called More Servlets which I haven't read but looks promising. A few chapters are available as pdfs online and the one on filters is one of them. You can find it on that website.

Another source of information is the official Sun tutorial for J2EE. It's pretty thick reading though.