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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • I don't get what is the behavior you expect, and the actual behavior? – gd1 Sep 30 '12 at 14:57
  • @gd1 : I expect my pages to display utf characters correctly when I include the `<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>` line in my top.jsp. What I see is garbage (like `Σελίδα Î±Ï€Î»Î¿Ï Î§Ïήστη`) unless I add the directive in each and every jsp as seen in myjsp.jsp – Mr_and_Mrs_D Sep 30 '12 at 15:01
  • When you do @include, the included JSP is just parsed in the context of the including JSP, just like the #include directive of the C language, so you shouldn't put the <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> everywhere but only in my.jsp (the main file). I don't see why you get those strange characters. – gd1 Sep 30 '12 at 15:05
  • @gd1 : I _want_ to include the @page directive in all my pages - the strange thing is that _when included_ does nothing (garbage) - when I put it in each and every jsp works as expected (no garbage). The <%@ page session="false"%> seems to work OK when included (in `top.jsp`) - so what is the difference between those 2 ? How can I include the directive in all my jspS ? Should I use `jsp:include` ? – Mr_and_Mrs_D Sep 30 '12 at 15:27

2 Answers2

15

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.

gd1
  • 11,300
  • 7
  • 49
  • 88
  • Thanks - in each jsp I include the `top.jsp` - do you think it would suffice to add the `@page` there ? – Mr_and_Mrs_D Sep 30 '12 at 16:36
  • Maybe you can, but it is not very conventional and may bring future issues: what happens if you include a "topmost.jsp" script before including your top.jsp? You will have to move the "@page" directive from top.jsp to topmost.jsp... If I were you, I would put the "@page" directive as the first line of the main script. Seems natural. – gd1 Sep 30 '12 at 16:47
  • Hmm - I do not have a "main script" - maybe my way of doing things is wrong ? i do not quite get you - I will try to put the directive in top.jsp and see if it works (that's why I asked in the first place) - _it did not_ but now I went ahead and deleted the BOM from all files - will keep you posted. – Mr_and_Mrs_D Sep 30 '12 at 16:54
  • Your main script is the first you mentioned in your question, my.jsp. Please, do try to put the "@page" directive as the first line of my.jsp before doing anything else :) Then move it to top.jsp (if you really feel like doing so) and see what changes. PS: deleting the BOM is a good idea – gd1 Sep 30 '12 at 16:58
  • Well - in myjsp works - the problem is that I have a lot of myjsps like this one - those are the "pages" in the app - I hoped I could include the `@page` at the `top.jsp` and then all pages would include this directive - I thought that first the compiler "pastes" the text from all the included files inside the myjsp.jsp and then compiles - apparently it is not so ? Try it for yourself - copy the files and replace "CONTENT" in myjsp with greek, chinese and the like - put `@page` only in the top.jsp - you will get the ~Σελίδα απλ` - deleting the BOM did not help. – Mr_and_Mrs_D Oct 01 '12 at 02:35
  • This is interesting -- this afternoon I'll give it a try, for sure. However, it's 100% NORMAL to have the page declaration on every (non included) page. – gd1 Oct 01 '12 at 05:40
  • I am still interested - namely I want to know why inclusion in the `top.jsp` of the `@page` directive _is not enough_ – Mr_and_Mrs_D Oct 05 '12 at 13:13
  • OK, sorry, I had no time. In the meanwhile, did it work putting the @page directive in the main jsp? – gd1 Oct 05 '12 at 13:44
  • Yes it did work (_I also had to put it to all the included files that contained utf chars_, as mentioned before - the blah at bottom.jsp is unicode for instance). I am on tomcat 7. Was it not supposed to work if included only once ? – Mr_and_Mrs_D Oct 13 '12 at 12:28
  • How are they not JSPs when they have directives and .jsp extensions? Could I rename the file to .html and still be able to use directives in it? I get your point though that included files are nested in the calling jsp. – Philip Rego Dec 10 '17 at 22:19
-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"%>
iCrazybest
  • 2,935
  • 2
  • 24
  • 24