liu peng
liu peng

Reputation: 95

How should I include other jsp pages in a jsp page correctly?

I am now trying to make a small website with jsp, just like most index pages, the index page of my website will include some part: The top part(contain a logo and a menu), the main part, the bottom part. In order to avoid too many html labels fill in the index pages, maybe including pages will be a good idea.

After search, I know that there are two methods to include jsp pages:use <%@ include file=""%> or <jsp:include page=""> and I know there are some differences between them, but I still meet some problems with include pages.

if I have a index.jsp and top.jsp I want to include top.jsp in the index.jsp.

The index.jsp like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>title</title>
<style type="text/css">
  body {background-color:black;margin:0px;padding:0px;}
  #left,#right {width:10%;margin:0px;padding:0px;}
  #left,#center,#right {float:left;}
  #center {width:80%;}
  #top {height:150px;}
  #main {height:600px;background-color:white;}
</style>
</head>

<body>
<div id="left">&nbsp;</div>
<div id="center">
  <div id="top"><jsp:include page="top.jsp"/></div>
  <div id="main"></div>
  <div id="bottom"></div>
</div>
<div id="right">&nbsp;</div>
</body>
</html>

top.jsp like this:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>  
<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>
</body>
</html>

My questions are as follow:

1.no matter which method of the two I use, where I click "look the source code" in the browser,I get this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>title</title>
<style type="text/css">
  body {background-color:black;margin:0px;padding:0px;}
  #left,#right {width:10%;margin:0px;padding:0px;}
  #left,#center,#right {float:left;}
  #center {width:80%;}
  #top {height:150px;}
  #main {height:600px;background-color:white;}
</style>
</head>

<body>
<div id="left">&nbsp;</div>
<div id="center">
  <div id="top"><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>  
<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>
</body>
</html>
</div>
  <div id="main"></div>
  <div id="bottom"></div>
</div>
<div id="right">&nbsp;</div>
</body>
</html>

the code in the top.jsp include the content in the head appear in the index.jsp, I think maybe I make some mistake and it should like this.

2.If I only write some lebels like this:

<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>

Then if the code contains some characters that do not supported by "ISO-8859-1", myeclipse will report a wrong info dialog. So how should I include a jsp page correctly? Thank you!

Liu Peng

Upvotes: 1

Views: 19362

Answers (2)

Sudhakar Krishnan
Sudhakar Krishnan

Reputation: 850

Try this: this acts same like copy and paste.

in all pages add :

<%@page contentType="text/html" pageEncoding="UTF-8"%>

and in top and bottom that is header and footer page jsut the body content is enough. Dont type html tag only the matter is enough.

To import header and footer in main page do this code:

<html>
<head>
</head>
<body>
<jsp:include page="/head.jsp" />
my body content
<jsp:include page="/foot.jsp" />
</body>
</html>

you can also import in head for css and js.

Upvotes: 0

BalusC
BalusC

Reputation: 1108722

JSP won't remove the <html><head><body> for you during <jsp:include>. It just includes all its output unmodified. If you need auto-removal of irrelevant HTML elements, you should be using a templating-capable view technology, such as JSP's successor Facelets, or some 3rd party library like Velocity, Freemarker and so on.

Your top.jsp really needs to contain only the content which you really need to have to end up in the final HTML product at exactly the location where <jsp:include> is declared in the parent page. Just keep the <html><head><body> out from top.jsp.

As to character encoding issues, this is a different matter, unrelated to whether the JSP is included or not. You just need to add a <%@page pageEncoding="UTF-8" %> to top of every JSP to tell the container that it should process the JSP using the given character encoding. UTF-8 is the de facto standard which covers every single character the mankind is aware of. To prevent repeating the same line over every single JSP, add this to web.xml instead:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

Upvotes: 2

Related Questions