开发者

Can I import my own jsp page into another jsp page..?

开发者 https://www.devze.com 2023-03-31 19:59 出处:网络
I have prepared a static html p开发者_运维知识库age using netbeans. Can I import that page into another page so as to not re-write the code again n again and then make the respective changes.You can i

I have prepared a static html p开发者_运维知识库age using netbeans. Can I import that page into another page so as to not re-write the code again n again and then make the respective changes.


You can incorporate a JSP page into another by using include directive like this -

<%@ include file="/path/to/yourfile.jsp" %>

or by using <jsp:include> standard action like this -

<jsp:include page="/path/to/yourfile.jsp"/>

From the above two approaches, the first one will cause the contents of yourfile.jsp to be included at page-translation time. That is, when the page is translated into a full-fledged servlet class, contents of yourfile.jsp will be included in the servlet. So this inclusion will happen only once, at page translation time which occurs only on the first user request after your app is up.

If you use the second approach, then on every user request the response from the yourfile.jsp will be included at run-time, rather than at page-translation time.

When you use include directive, you basically copy and paste the content of the target file into the main file. If the target file contains any tag or EL which generated dynamic content, then these will also become a part of the main file and they will execute accordingly and will generate dynamic content. No problem there.

But this approach has some limitations. For example, a page which has been included using include cannot change the response status code or set headers which means you can't call addCookies() or some other header-setting methods from yourfile.jsp. You won't get an error if you do this though, you just won't get what you are hoping for. If you use <jsp:include> then all of these can be done in the included page and they will work accordingly.

Another important distinction exists between these two approaches. Suppose you want to include a file which has a little context-sensitive texts that change depending on the page into which they are being included. With the include approach, you won't be able to accomplish this elegantly. But with the <jsp:include> approach, you can do this -

<jsp:include page="/path/to/yourfile.jsp">
    <jsp:param name="myContextSensitiveText" value="Context Sensitive!!" />
</jsp:include>

which means you are specifying a new request parameter for yourfile.jsp, which you can then access from that file and render it accordingly -

${param.myContextSensitiveText} - Context Insensitive Text!!

with the include approach, you won't be able to accomplish this.

Which one should you use will purely depend on your design choice.

You should remember another thing - both of these approaches will include the contents of the target file into the main file, although in different way. So if both of them contains html elements like <html> or <body>, then you will end up with two <html> and two <body> elements in the final-rendered page, which won't be valid.

For more information, you can take a look at here and here.

Edit

There is a third way to include a page using JSTL. You can use import JSTL tag for this purpose -

<%-- You need to declare this at the top of your jsp page--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...............
...............

<c:import url="/path/to/yourfile.jsp" />

This approach works in exactly the same way as <jsp:include>, except it's a bit more powerful. You can include contents from a page which is outside of your application directory, even outside of your container too! As an example -

<c:import url="http://www.google.com" />

this line will include the HTML content of the google's home page.

If you need to pass parameters to your included page like <jsp:include>, then you can use param tag -

<c:import url="/path/to/yourfile.jsp">
    <c:param name="myContextSensitiveText" value="Context Sensitive!!" />
</c:import>

and access it in the same way -

${param.myContextSensitiveText} - Context Insensitive Text!!


Yes you can import the jsp page in another jsp.

      <jsp:include page="/jsp/old.jsp"/>

Including Content in a JSP Page


You can do it in these ways,

<%@ include file="/path/to/yourfile.jsp" %>

or

<jsp:include page="/path/to/yourfile.jsp"/>

or

 <c:import url="/path/to/yourfile.jsp" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号