0

I've create a jsp page like this:

 <%  Object myName  = session.getAttribute("name"); 
     if(myName == null)
     {   String redirectURL = "http://www.google.com";  
         response.sendRedirect(redirectURL); 
     }
 %>
 <html>  ... some content ... </html>

The content inside html tage is around 100k. Now when I try to compile and run it in netbean/glassfish, i got an error message:

constant string too long
out.write("<html>.......</html>

I read it somewhere that there's a 64k limit. but i am not using "out.write" function at all, the jsp section is really small as you can see. How did this happen and how do I solve this?

What I want to achieve is first check if the session is valid, if not then redirect. if so, then show content. The content is quite large and static but i have no control over it :(. anyway i can achieve this?

neo
  • 2,461
  • 9
  • 42
  • 67
  • That's a pretty big page; how can you possibly maintain it? That aside, a JSP page is compiled to a servlet--it doesn't matter that you don't *explicitly* call write; how do you think the output is produced? Take a look at the generated file. – Dave Newton Jan 10 '13 at 17:00
  • As per your edit, this problem has nothing to do with whether to use redirect or not. To see it yourself, remove that whole `<% %>` block and you'll see that the problem still persists. My answer is also telling that the strange `redirect` logic has got nothing to do with this all (by the **"Unrelated"** statement). – BalusC Jan 10 '13 at 17:33

1 Answers1

1

but i am not using "out.write" function at all

JSP is internally using it. You know, JSP file is during "JSP compile" step converted to a Java class extending HttpServlet and everything ends up as Java code. Checkout the generated code in server's work folder to see it yourself.

Use runtime JSP includes using <jsp:include> to split out large fragments into separate JSP files.

E.g.

<body>
    <jsp:include src="/WEB-INF/header.jsp" />
    <jsp:include src="/WEB-INF/menu.jsp" />
    <p>Content</p>
    <jsp:include src="/WEB-INF/footer.jsp" />
</body>

Or, if that doesn't suit the concrete functional requirement, an alternative, provided that the HTML content is really static (i.e. it does not contain any JSP scriptlets, tags, expressions, etc), is to put the HTML content in its own some.html file and reference it by JSTL <c:import>.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<body>
    <c:import url="some.html" />
</body>

This way it doesn't end up as part of JSP source code.


Unrelated to the concrete problem, your redirect logic is missing a return statement. When executing the redirect, all the remaining JSP code is otherwise still invoked. Also, if you're repeating this scriptlet over all JSP files, you'd question if you can't better use a servlet filter for the job.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC. a quick question: " it does not contain any JSP scriptlets, tags, expressions, etc" Can it contain javascript expression? – neo Jan 10 '13 at 17:52
  • Uh, JavaScript is not part of Java/JSP. It's just part of HTML. You know, Java/JSP is a HTML code producer. HTML runs in webbrowser, not webserver. So does JavaScript. Also, the term "javascript expression" does not make any sense. – BalusC Jan 10 '13 at 18:02