<%@ page pageEncoding=“UTF-8”%> ignored whe

2019-03-18 18:55发布

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

2条回答
Emotional °昔
2楼-- · 2019-03-18 19:11

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.

查看更多
成全新的幸福
3楼-- · 2019-03-18 19:16

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"%>
查看更多
登录 后发表回答