JSP Tutorial #16 – Including Files with JSP



JSP Tutorial #16 – Including Files with JSP

JSP Tutorial #16 - Including Files with JSP

FULL COURSE: JSP, Servlets and JDBC (80+ videos)
http://www.luv2code.com/jsp (SPECIAL DISCOUNT)

—-

This JSP tutorial series will help you quickly get up to speed with JSP.

Download Tutorial Source Code: http://www.luv2code.com/downloads/youtube-jsp/jsp-tutorial-source-code-lite.zip

—-

View more videos on the playlist: JSP Tutorial: https://goo.gl/fFVMrj

Closed-Captioning and English subtitles available for this JSP Tutorial.

—-

Follow luv2code for more JSP tutorial:

Website: http://www.luv2code.com
YouTube: http://goo.gl/EV6Kwv
Twitter: http://goo.gl/ALMzLG
Facebook: http://goo.gl/8pDRdA

If you liked my JSP tutorial, then join my mailing list: Get exclusive access to new Java tutorials.

– http://www.luv2code.com/joinlist

Questions or problems about this JSP tutorial? Post them in the comments section below.

Want to suggest a video for my JSP tutorial? Leave a comment below. I’m always looking for new video ideas.

Let me know what video you’d like for me to create.

Premium JSP Course

Need More Details on JSP?
– See my Premium JSP and Servlets course (80+ videos)
– http://www.luv2code.com/jsp

JSP Tutorial Transcript

Hello. In this video, I’m going to show you how to use JSP for including other files. The common used place for including files is including standard headers and footers. Say for example you’re building a website and you want to have the same header and footer information displayed on every page. You can simply create the header and footer in separate files and include them in your JSP files. All right, so when you include a file you can include either HTML files or JSP files. It’s totally up to you. At the top here, I do a jsp:include and page=”my-header.html”. A similar thing here at the bottom, I can do a jsp:include page=”my-footer.jsp”. That’s it. Very simple, very straightforward.

All right so let’s go ahead and try this out, so let’s move into Eclipse. What I’d like to do is to continue to use that existing project, jspdemo. Let’s move down to the web content folder, and what I want to do first is create those files I’m going to include. First I’m going to create the header files. I’ll say “New”, “File” and I’ll give the file name of “my-header.html”, so that’s the header that we’re going to include. When you’re happy with the file name here, click finish. For the header, I simply want to give the name “JSP Tutorial”, so I’m going to set up H1, and I’ll make the alignment for it to be center. Then inside the tag here, I’ll just say “JSP Tutorial”, so this will be including on all pages for my given website that I’m just making up right now. I’ll go ahead and save this file, so this is our header file that we have set up right now.

Now let’s over and let’s create our footer. So again, on WebContent I’ll do a right-click, I’ll say “New”, “File”, and I’ll call it “my-footer.jsp”. In the footer we actually want to add some JSP coding. Once we’re happy with the file name, go ahead and click finish. For here, for the footer, for every page I want to give the time the page was last modified. Or, we’re just going to kind of make this up, actually. So here’s I’ll just say, “Last Modified” or “Last Updated” and I’ll just drop in the actual date timestamp. That way all of our pages every time a user will visit our website. Now I’ll just hit the save button.

Okay, great. So now at this point, we have our header complete and we also have our footer complete. Now we can go ahead and create our main page that’s actually going to include these files. I move over to the WebContent folder, I’ll do a right-click, I’ll say “New”, “File”, and I’ll call this, “homepage.jsp”. Once we’re happy with the name here, we’ll click finish. On this homepage, what we’re going to do is we’re actually going to include information from the headers and the footers, and also we’ll put some of our regular content here. I do the standard work here for our HTML in our body. Now what I want to do is import or include the header. So I’ll say jsp:include, I give page=”my-header.html”. Now I can enter some regular content that I’ll have on my homepage. Here I’m just going to put in some filler stuff, just some blah blah blah. Just to you know, placeholders here. Then I’ll copy this line x number of times, just to kind of fill it up a little bit. So that’s our content. Let’s go ahead and include our footer. I’ll say jsp:include page=”my-footer.jsp”, because our footer is a jsp file. Again, it an go either way. HTML or JSP, depending on how you want to build out your pages. That’s it. I can go ahead and save this file and then I can go ahead and run it.

All right so I can move over to this file homepage.jsp. I can do a right-click, then I can say, “Run As” and then “Run on Server”. Okay, great. This looks really good. Up top we have our header file that we included, JSP Tutorial.

[snip] for complete JSP tutorial transcript, select “More … Transcript”

Comments are closed.