JSP常量字符串太长 [英] JSP constant String too long

查看:245
本文介绍了JSP常量字符串太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个像这样的jsp页面:

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>

html tage内的内容大约为100k.现在,当我尝试在netbean/glassfish中编译并运行它时,我收到一条错误消息:

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>

我读到有64k限制的地方.但是我根本不使用"out.write"功能,您可以看到jsp部分确实很小.这是怎么发生的,我该如何解决?

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?

推荐答案

但是我根本不使用"out.write"功能

JSP在内部使用它.您知道,JSP文件在"JSP编译"步骤中转换为扩展HttpServlet的Java类,并且一切最终都以Java代码结尾.在服务器的工作文件夹中检出生成的代码,以便自己查看.

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.

使用运行时JSP包括使用<jsp:include>将大片段拆分为单独的JSP文件.

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

例如

<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>

或者,如果这不符合具体的功能要求,那么,如果HTML内容确实是 static (即,它不包含任何JSP脚本,标记,表达式等),则可以选择一种替代方法. ,就是将HTML内容放入其自己的some.html文件中,并通过 JSTL <c:import>进行引用.

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>

这样,它就不会最终成为JSP源代码的一部分.

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

无关与具体问题无关,您的重定向逻辑缺少return语句.执行重定向时,所有其余的JSP代码仍将被调用.另外,如果您要重复此 scriptlet 在所有JSP文件中,您会质疑是否最好使用 servlet过滤器进行工作.

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.

这篇关于JSP常量字符串太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆