Simple jsp code (showing current time)

I haven’t post since forever 😦 i felt so bad last several days. So I thought I must write at least one post today.

Today we will write a simple jsp code that makes a webpage showing current time and date.

First, open eclipse and make a new jsp project called “now”.

Snip20150509_2

Then you will see a screen like this.

Snip20150509_3

2. Write down this code.

Snip20150509_5

3. Save it to the tomcat directory. 

* I made a folder called “practice” in the tomcat directory. Then I saved this now.jsp inside practice.

4. Start tomcat! 

If you don’t remember how to start tomcat, go to terminal.

Go to the directory you installed tomcat by entering $cd ___.

If you are at tomcat folder, do $cd bin, and do ./startup.sh 🙂

Snip20150509_6

4. Enter this into URL:

http://localhost:8080/practice/now.jsp

It should not be “practice” . It should be name of your folder.

Snip20150509_7

You should be able to see something like this! 😀

It will differ depending on your country.

How to make a new project in eclipse

1. Open eclipse we downloaded.

Snip20150505_12

Go to file > new > project.

2.

Snip20150505_13

Find ‘Web’ and click ‘Dynamic Web Project’.

Snip20150505_14

Name the project to anything you want. I named it Test1.

Then click New Runtime.

3.

Snip20150505_15

Find the right tomcat version and do finish!
Snip20150505_16    You will see this on the left of the eclipse screen.

Right click ‘Web Content’

Snip20150505_17

New > JSP file!
Snip20150505_18

Name the file to anything.

Click Finish! You are done 🙂

How to install JSP & JRE & Tomcat 8 & Eclipse EE version

Whoa. I spent almost 2 hours just preparing development environment for JSP. :0

I first downloaded latest version JDK 8.0.21, which was fairly simple. You can download it in here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Snip20150505_1

You also have to download JRE!

JDK is development tool and JRE is runtime environment. If you only install JRE, you can run the java application but you can’t actually develop a java application. For example, if JDK is not installed, then you can’t compile the java source code. Also it is very rare to install just JRE. Usually people install JDK & JRE together! 🙂

After that, I downloaded web container, Tomcat (version 8). — This was the part that took me the longest time 😦

I looked at the website carefully, remembering JDK and Tomcat go as a pair. So it is important to pick a right version to download! It turns out Tomcat 8 is suitable to JDK 8.0.21. So I downloaded tomcat 8.

You can download Tomcat in here: http://tomcat.apache.org/

Snip20150505_2

Snip20150505_3

If you are mac user, download tar.gz file! 🙂

Next is the hardest part!

After downloading, you will see the file “apache-tomcat-8.0.21.tar.gz” Snip20150505_4

unzip it. Then, tomcat is installed.

Snip20150505_5I renamed it to tomcat to make it more convenient to run! 🙂

Go to the terminal.

go to the directory where tomcat is installed. For me, it was Downloads.

Do $pwd — this shows your location.

Snip20150505_6 if you want to move to different folder, you can do so by first going to home directory.

Do $~ . Then move to the folder by doing $cd _____. e.g $cd Desktop, $cd NewFolder.

If you are at the folder tomcat is installed, Do $tomcat or $apache-tomcat-8.0.21 if you didn’t rename the folder.

Snip20150505_7

Then go to bin folder by typing cd bin.

Then executes startup.sh by ./startup.sh — Downloads tomcat. 

After that, you can check if tomcat is installed successfully by going to this website:

http://localhost:8080

Snip20150505_8

If this screen shows, it means you successfully installed tomcat! 😀

3. Install code editor

You can download any code editor, but I chose eclipse.

You can download eclipse here:http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/index.html

i struggled finding right one to download, which also took me forever.

But you can trust me. This one will work! 🙂

Snip20150505_9

This will take long time to download. When you are done, go to the eclipse folder.Snip20150505_10 and run Snip20150505_11!

What do we need before start developing web application?

In order to develop a web application using JSP and Servlet, you need at least three programs:

  • JDK
  • Web Container
    • Executes JSP and servlet.
    • Tomcat, GlassFish, jetty
  • Code editor
    • need it to write Java source code
    • You can use notepad, but it is recommended to use IDE
      • UltraEdit, acroEdit.. or Eclipse, NetBeans