<%@ page pageEncoding=“UTF-8”%> ignored when included from another jsp

只愿长相守 提交于 2019-12-03 16:59:14

问题


I have code (now in github) like :

my.jsp (a generic jsp - all my jspS follow this pattern more or less) :

<%@ include file="include/top.jsp" %>
<title>THE TITLE</title>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="include/head.jsp" %>
<%@ include file="include/no_menu.jsp" %>
CONTENT
<%@ include file="include/bottom.jsp" %>

where :

top.jsp :

<%@ page session="false"%>
<%@ include file="tag_libs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

head.jsp :

<link href="${pageContext.request.contextPath}/css/twoColFixLtHdr.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container"><!-- closes in bottom -->
        <div class="header"><!-- closes in menu -->
            <p>
                <a href="home"> <img src="${pageContext.request.contextPath}/images/logo7.jpg"
                    alt="Ted 2012 Logo" name="Ted 2012 Logo" id="Ted_2012_Logo"
                    style="background: display:block; padding: 5px 20px; margin-left: 150px; border-style: solid" /></a>
            </p>
            <hr />

no_menu.jsp :

</div>
<div class="content">

bottom.jsp :

        </div>
        <div class="footer">
            <p>
                blah
            </p>
        </div>
    </div>
</body>
</html>

As you see - or you can take my word for it - the tags balance correctly. My question is - why can't I include the

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

directive in my top.jsp file ? Believe me it does nothing. Should I worry that the <%@ page session="false"%> is similarly ignored ?

Thanks


回答1:


You shoudn't need to put the @page directive in each included JSP file. Strictly speaking, they are not JSPs, they are text files being included into a JSP. @include is equivalent to cutting and pasting the text from your included page right into your main JSP. It's like the #include directive in C.

Please try to put <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> as the very first line of your main JSP file (i.e. the one which includes the others), and don't put it anywhere else.
Hope it helps.




回答2:


Setting Content-Type in the HTTP Header

This is the best way to set Content-Type for an individual page because it is highest on the Precedence Rules list. The HTTP header value for the web page hosting your FeedSweep widget can be set in any one of the following server side scripting languages:

.NET

Content type and charset are set on the response object. To set the charset, use:

  • Response.ContentType = "text/html; charset=UTF-8";

Perl

Output the correct header before any part of the actual page. After the last header, use a double linebreak.

  • print "Content-Type: text/html; charset=utf-8\n\n";

Python

Use the same solution as for Perl (except that you don't need a semicolon at the end).

  • print "Content-Type: text/html; charset=utf-8\n\n"

PHP

Use the header() function before generating any content.

  • header('Content-type: text/html; charset=utf-8');

Java Servlets

Use the setContentType method on the ServletResponse before obtaining any object (Stream or Writer) used for output.

  • resource.setContentType ("text/html;charset=utf-8");

If you use a Writer, the Servlet automatically takes care of the conversion from Java Strings to the encoding selected.

JSP

Use the page directive:

  • <%@ page contenttype="text/html; charset=UTF-8"%>

Output from out.println() or the expression elements (<%= object%>) is automatically converted to the encoding selected. Also, the page itself is interpreted as being in this encoding.

ASP

Content type and charset are set on the response object. To set the charset, use:

  • <%Response.charset="utf-8"%>


来源:https://stackoverflow.com/questions/12662172/page-pageencoding-utf-8-ignored-when-included-from-another-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!