Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Specific knowledge is found by pursuing your genuine curiosity and passion rather than whatever is hot right now.
-Naval Ravikant
Programming    [Java] 
2018-05-09 21:35:23

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/78

Just like before, let's take it easy and enjoy a few beautiful photos first.

/Images/20170904/001.jpg

/Images/20170904/002.jpg

/Images/20170904/003.jpg

/Images/20170904/004.jpg

/Images/20170904/005.jpg

/Images/20170904/006.jpg

/Images/20170904/007.jpg

Before everything else, let me list some information of my computer for you.

  • OS: Windows 10 Home edition
  • CPU: Intel(R) Core(TM) i5-4460S CPU @ 2.90GHz
  • RAM: 4.00 GB
  • Type: 64 bit

To setup the development environment for Java Servlet, it involves 3 steps:

  1. Setting up Java Development Kit
  2. Setting up Web Server – Tomcat
  3. Setting up IDE – Eclipse

Note: The 3rd step is optional. Technically, you can write, deploy, and run a Java Servlet without any IDE help.

 

Setting up Java Development Kit

1. Before starting to install the JDK, you may want to know whether the JDK has been installed in your computer or not, and if the answer is yes, what version it is.

Open the command window and run the following command:

java –version

If you have installed JDK and configured the Java home path properly, it will show the version below the command, here is the result in my computer:

Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。

C:\Users\dell>java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

C:\Users\dell>

2. Download the JDK from http://www.oracle.com/technetwork/java/javase/downloads
I chose the 64-bit version of the JDK 8: "jdk-8u144-windows-x64.exe"

3. Right click on the executable “jdk-8u144-windows-x64.exe”; select “Run as administrator” on the popped menu, and install the JDK to your computer.

4. After the installing program was finished, redo the first step and make sure you can see the version of the JDK you have just installed.

 

Setting up Web Server – Tomcat

1. Download the latest Tomcat from http://tomcat.apache.org/download-90.cgi.

I chose the version 9.0: "apache-tomcat-9.0.0.M22.exe".

2. Right click on the executable “apache-tomcat-9.0.0.M22.exe”; select “Run as administrator” on the popped menu, and install Tomcat to your computer.

3. After the installing program was finished, open you favorite web browser and navigate to http://localhost:8080/, make sure you can see the main page.

 

Setting up IDE – Eclipse

1. Download the latest Eclipse For Java from http://www.eclipse.org.

I chose the version Oxygen Release (4.7.0): "eclipse-inst-win64.exe"

2. Right click on the executable “eclipse-inst-win64.exe”; select “Run as administrator” on the popped menu, and install Eclipse to your computer.

3. To create a Java servlet program in Eclipse, we need to install a plugin first.

    a. Open Eclipse, select "Help -> Eclipse Marketplace…" from the menu, a dialog popped up.

/Images/20170904/01.jpg

    b. Search with "Tomcat" in the "Elipse Project", you will find a plugin whose name is "Eclipse Java EE Developer Tools 3.9".

    c. Click "Install" to install the plugin.

 

Create an example Java servlet program

1. Re-run Eclipse, and select "File -> New -> Project…" from the menu, a dialog popped up.

/Images/20170904/02.jpg

2. Choose the item "Web -> Dynamic Web Project", click "Next >" to continue.

/Images/20170904/03.jpg

3. Type in the Project name. You may have noticed that there is no Target runtime items yet. So we have to create one.

4. Click the button "New Runtime...", a new dialog popped up.

/Images/20170904/04.jpg

5. Select the "Apache Tomcat v9.0", tick the option "Create a new local server", and click "Next >" to continue.

/Images/20170904/05.jpg

6. Click "Browse..." to select the Tomcat installation directory, select the JRE version, and click "Finish" to create a Server Runtime.

/Images/20170904/06.jpg

7. Select the runtime which was just created, and click "Next >" to continue.

/Images/20170904/07.jpg

8. Click "Next >" to continue.

/Images/20170904/08.jpg

9. Tick "Generate web.xml deployment descriptor", and click "Finish", a new project will be created successfully.

10. In the step No. 7, we have a chance to modify the "Default output folder" for the Java builder, but I did not do it. I'll do it now.

/Images/20170904/09.jpg

    a. Right click on the "HelloWorld" project, select "Properties" on the popped menu.

/Images/20170904/10.jpg

    b. Switch to "Java Build Path" tab, change the "Default output folder" to the ".../WEB-INF/classes" directory, and click "Apply and Close" to save the settings.

11. Create a Java Servlet: HWServlet.

/Images/20170904/11.jpg

    a. Right click on the "HelloWorld" project, select "New -> Other..." on the popped menu.

/Images/20170904/12.jpg

    b. Select "Web -> Servlet" and click "Next >" to continue.

/Images/20170904/13.jpg

    c. Type in the package name and the class name, choose "javax.servlet.http.HttpServlet" as its superclass, and click "Next >" to continue.

/Images/20170904/14.jpg

    d. Click "Next >" to continue.

/Images/20170904/15.jpg

    e. Click "Finish", Eclipse will help us to create a new servlet. Here is the source code:

package com.casperlee.examples.HelloWorld;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HWServlet
 */
@WebServlet("/HWServlet")
public class HWServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HWServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

12. Run and test the servlet.

/Images/20170904/16.jpg

    a. Right click on the servlet class, select "Run As -> Run on Server" from the popped menu.

/Images/20170904/17.jpg

    b. Select the Tomcat server, tick the "Always use this server when running this project", and click "Next >" to continue.

/Images/20170904/18.jpg

    c. Click the "Finish" to run the servlet on the Tomcat server.

/Images/20170904/19.jpg

    d. Oops!

13. Fix the error and run again.

    a. Open the command window and run the following command:

netstat –ano | grep 8080

/Images/20170904/20.jpg

Note: "grep" is actually a Linux command which cannot be recognized by Windows, if you have not done anything of mapping Linux commands to Windows. But fortunately, Windows also has a similar command: "findstr". You can run the following command instead:

netstat -ano | findstr 8080

Special thanks to Damon, who was my coworker, for his reading of this article and finding this issue.

    b. Here we can see the port 8080 is being listened by the process whose PID is 5288.

/Images/20170904/21.jpg

    c. Open “Windows Task Manager” and find the process whose PID is 5288.

    d. Click “End Process” to end the process Tomcat9.exe

    e. Repeat the step No. 12

/Images/20170904/22.jpg

14. It works!