Ways to reference other pages in a JSP page

Source: Internet
Author: User

First look at this title .... Everyone's feeling must be good 2 ah ... Bloggers must say that the JSP dynamic Reference (jsp:include) and static reference (@include) . The article about the two differences is already rotten street. A search a lot of. Bloggers have come to introduce these antiques. Decisive Alt+f4 ...

If that's what you really think ... Then please don't hurry to close the page ... Today I'm going to share my own understanding, a little bit of something to add to the bad street ... (later words will be more verbose)

Let's start by explaining why we're going to discuss this topic.

When I write my own blog program (blogv2.labofjet.com), the directory in the program needs to read the data in the database, and this directory is to be referenced by multiple pages. What to do at this time.

I used a static reference at that time, in the JSP page through the <% @include file= "menu.jsp"%> reference the directory of JSP page fragments (a separate JSP for the directory), The disadvantage is that when you jump to a different page from a different controller, as long as the page contains the HTML fragment of the directory, the controller needs to call the directory's DAO method and then set the value of the request so that the directory page is displayed properly.

Then I thought, I think it would be best to write a servlet that accesses the URL of the servlet to get the HTML fragment of the directory, and then on a different page, just loading the URL is equivalent to loading the HTML fragment of the directory. The advantage is that different controllers no longer need to invoke the DAO in the directory, as long as there is a dedicated directory controller to handle the directory of DAO and set the request and then return to the Directory page. Then I thought of jsp:include.

Then I went to Baidu a bit bad street things. Most articles have a meaning, Jsp:include can refer to JSP and compile dynamically. Blablabla ... But none of them suggests that Jsp:include could reference a servlet? We are not going to put logic in the JSP, usually put in the servlet and then jump to the JSP bar. I found it to be possible after the experiment. But there are some places to compare the pit daddy ... Need to pay attention to ...

Jsp:include Reference JSP when fully OK, is the content of most articles on the Internet.

when Jsp:include references a servlet, the servlet needs to be configured in Web. XML, or eclipse will give a wavy red line.

I do not know if you see the above summary has produced a new problem: now we write programs with some framework, such as SPRINGMVC, Instead of being configured separately for each Servlet in Web. XML, we configure these controllers (Servlets) by SPRINGMVC our own configuration files. So can jsp:include still use it?

The answer is yes .... But it's a pit. The pit will still be on the red line in Eclipse, and Eclipse doesn't think it can find the servlet for the URL, but it's actually accessible.

There is also a small problem, maybe I did not set the right, every time I jsp:include the console will be reported Java.io.IOException:Stream closed this anomaly, I think it's possible that other servlets invoke the out Close method automatically when referencing other servlets, causing the contents of the page containing the other page not to be output properly .... I didn't go on with the problem .... I think there is a solution, but not the point I want to discuss in this article.

So now there are 2 small problems: An exception, an Eclipse Red line error ... Although it can be used, but people are very uncomfortable.

so just find a way to replace it. That's Jstl's c:import.

This tag is actually super simple ... For example, I set the URL of the directory HTML fragment to/menu, and I'm in the page containing the directory as long as

<c:import var= "Data" url= "/menu"/>${data}

It's OK. The catalog is included in the other pages. Isn't it super simple?!

Feel the world is really beautiful in a moment .....

Ways to reference other pages in a JSP page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.