JSP Tutorial #7 – Java Server Pages Tutorial – JSP Hello World

In this video, I show you how to create a Hello World program with JSP.

Please SUBSCRIBE to my YouTube channel.

 


 

# Video
1 Introduction
2 JSP Overview
3 Setting up your Development Environment - Overview
4 Installing Tomcat
5 Installing Eclipse
6 Connecting Eclipse to Tomcat
7 JSP Hello World
8 JSP Expressions
9 JSP Scriptlets
10 JSP Declarations
11 Importing Java Classes
12 JSP Built-In Objects
13 Including Files

Do You Need More Details?

  • How to read form data with JSP?
  • How to use cookies and sessions?
  • Develop MVC application with JSP and Servlets?
I have a premium course that shows you how to complete all of these tasks.

Read more about the premium course at http://www.luv2code.com/jsp

Follow luv2code with the links below:

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

JSP Tutorial Transcript:

Hey. In this video, I’m going to show you how to create a Hello World program with JSP. The common question is, “What is a JSP file?” Well, a JSP file is simply an HTML page with some Java code sprinkled in, and it basically gives you dynamic content that you can include in your page. In this diagram here, this is a basic JSP file. It has some HTML code, some Java code, you can mix in some more HTML code, and so on. The end result is that you’ll have an HTML page with content that’s generated by some Java code.

All right, so the next question is, “Where’s the JSP processed?” A JSP file is actually processed on the server, so it’s processed on your Tomcat server, your GlassFish and JBoss server. The results of the Java code is actually included in the HTML return to the browser. Here’s an example: We have a web browser. We make a request for a JSP page. It goes across. The JSP page is processed by your server, and then the results of that Java code will generate HTML, and those results are actually returned back to the web browser. It’s returned back to the browser as just plain HTML. In effect, the Java code can generate HTML that goes back to the web browser.

All right, so next where should we place the JSP file? The JSP file goes into your WebContent folder. You create a new Eclipse project, they have a WebContent folder, you place the file there. Then one requirement is that, the JSP file must have a .jsp extension. Here’s an example here we have on the slide. WebContent, helloworld.jsp. It’s just like any other normal HTML file that you place out there, like building a normal website. You can have multiple JSP files. You can place your JSP files in subdirectories, and so on, so it’s almost like building a website, with multiple HTML pages, except for here, we’ll have .jsp files, with that JSP extension.

So you’re probably wondering, “Hey, show me the code.” Here’s an example of the code. Very basic Hello World program. Again, remember, a JSP file is simply an HTML page with a small bit of Java code sprinkled in. Here we have our body, H3, Hello World of Java!, and then the time on the server is, and right here, this is where we have Java code. With JSP, you make use of the angle bracket percent, and inside of there you can put a JSP element. In this case, we’re going to put in what we call a JSP expression.

Use the equals symbol, so angle bracket percent equal, and then we could put in some Java code. This example, we create a new date object. This will give us a current timestamp, and then we include the output of this date object in the page. In effect, it’ll call the toString method on this data object, and include the result of that in this page that goes back to the browser.

I know I gave you a lot of information there. Let’s go ahead and switch over, and look at Eclipse. We’ll write this code, and then we’ll run it within Eclipse. We’re in Eclipse, so the first thing we need to do is we need to create a New Web Project. I’ll go up to my File menu, I’ll say New. I’ll choose Dynamic Web Project. This will bring up the dialog. We can enter our project name. Here, I’ll call it jspdemo. You can give any name for the project, but here I’m just keeping it simple.

Make sure the target run time is set up for Apache Tomcat 8, and you can keep all the other defaults with no problem, and then simply hit the Finish button. What this will do is this will create a new project for us, so we have jspdemo. If we expand this directory, we’ll see that we have a WebContent folder. Right now our WebContent folder is empty. There are no pages that are created.

Here, I’m going to create a new file. Under WebContent I’ll right click. I’ll say New File. I’ll call this helloworld.jsp. Once I’m happy with the file name, I’ll hit the Finish button.

Here’s our file. It’s empty right now, so I’ll just go ahead and create a very basic HTML page. Eclipse will help me out with some of the tags here. I’ll set up a body, then I’ll set up my H3, Hello World of Java. Then I’ll say, “The time on the server is …” This is where we’ll drop in some Java code. Use that angle bracket percent with an equals symbol, and I say, “new java.util.Date.” Again, it’s going to create a new date object with a current timestamp, and then the result of that timestamp will be included right here in this page, that’ll go back to the browser. That’s basically my Hello World JSP right now.

Let’s go ahead and run this example. What I’ll do is I’ll move over here to that file, helloworld.jsp. I’ll simply right click the file, and I’ll say, “Run as,” and I’ll choose Run on Server. Eclipse will prompt me first. It’ll say, “Choose your server.” We’re going to use the existing Tomcat 8 server, and in the bottom left we’ll check the box here to always use this server when running this project. From there, I can go ahead and hit the Finish button.

This will actually start on my Tomcat server, if it’s not running. Then it should execute this JSP page. Great! Here are the results. Hello World of Java! The time on the server is, and they drop in the timestamp. This is the actual current timestamp that I have on my server at the time of this recording. Note here, it created a new date object, and then the results of that date object were included in that page.

Now, what I’d like to do is copy this URL, and just drop it into a regular web browser. I’ll just right click. I’ll say, “Copy,” and then I’ll switch over to a web browser, like Firefox, and I’ll just drop in that URL. Just do a right click, and I’ll choose Paste. Then I’ll hit Enter, and there’s the JSP page inside of the browser.

Hello World of Java! The time on the server is … I can right click and take a look at the source. Here’s the source. This is kind of like a behind the scenes tour here. This is the source that came back to the browser. The time on the server is, and it has this information. The web browser really doesn’t know how this code was generated, it simply renders the response. That’s JSP in a nutshell.

We covered a very basic, Hello World example. We’re going to dig deeper, or dive deeper into JSP as go through in the following videos. That’s enough just to kind of get us started. We can see the basics, and we can check the box for Hello World. Stay tuned, a lot of good things in store in the following videos.

Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Share

Leave a Reply