Eclipse JSP/Servlet Environment Setup
This article assumes you have already installed the JDK environment. If not, you can refer to Java Development Environment Configuration.
We can use Eclipse to set up a JSP development environment. First, let's download the following software packages:
- Eclipse J2EE: http://www.eclipse.org/downloads/
- Tomcat: http://tomcat.apache.org/download-70.cgi
Tomcat Download and Installation
You can download the appropriate package based on your system (the following example is for Windows):
After downloading, unzip the package to the D drive (or any other location you prefer):
Note that the directory name should not contain Chinese characters or spaces. Here is an introduction to the directories:
- bin: Binary executable files. The most commonly used file is startup.bat. For Linux or Mac systems, the startup file is startup.sh.
- conf: Configuration directory. The most critical file is server.xml, where you can change the port number, etc. The default port number is 8080, which should not be occupied by other applications.
- lib: Library files. The directory where the jar files required for Tomcat to run are located.
- logs: Log files.
- temp: Temporary files, i.e., caches.
- webapps: Web applications. Web applications placed in this directory can be accessed directly by the browser.
- work: Compiled class files.
Next, we can double-click startup.bat to start Tomcat, which will bring up the following interface:
At this point, the local server has been set up. To shut down the server, you can close the window or type Ctrl+C inside to stop the service.
Then, enter http://localhost:8080/ in the browser. If the following interface appears, it means Tomcat has been installed and started successfully:
Let's test it on the browser:
First, create a new jsp file in the directory D:\apache-tomcat-8.0.14\webapps\ROOT:
The test.jsp file code is as follows:
<%@ page contentType="text/html;charset=UTF-8" %>
<%
out.print("tutorialpro.org : http://www.tutorialpro.org");
%>
Then, access the address http://localhost:8080/test.jsp in the browser, and the output should be as follows:
Integrating Tomcat with Eclipse
After downloading Eclipse J2EE, unzip it and open Java EE. Select Windows-->preferences (Eclipse-->Preferences for Mac systems), which will bring up the following interface:
In the above image, click the "add" button, which will bring up the following interface:
In the options, select the corresponding Tomcat version, then click "Next", choose the Tomcat installation directory, and select the Java environment we installed:
Click "Finish" to complete the configuration.
Creating an Instance
Select File-->New-->Dynamic Web Project, and create a project named TomcatTest:
Expand the red box in the above image, which will bring up the following interface:
Note that if the default selection is already our previously installed Tomcat and JDK, you can skip this step.
Then, click finish to continue:
The project file structure:
In the above image, the directories are explained as follows:
- deployment descriptor: Description of the deployment.
- Web App Libraries: Additional packages can be placed here.
- build: Directory for compiled files.
- WebContent: Directory for the pages you write.
Create a new test.jsp file under the WebContent folder. The default code for it is shown below:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
Next, we modify the test.jsp file code as shown below:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>tutorialpro.org</title>
</head>
<body>
<%
out.println("Hello World!");
%>
</body>
</html>
Before running the program, let's modify the browser options:
Next, we run the project:
During runtime, the following error pops up: (If you don't encounter this error, please ignore)
The reason is that we previously clicked on the startup.bat in the Tomcat installation package, which manually opened the Tomcat server. This is unnecessary because when the program runs, Eclipse will automatically start the Tomcat server. So, we manually shut down the Tomcat software and run the program again. The console information is as follows:
Visit http://localhost:8080/TomcatTest/test.jsp in the browser to get the normal output:
Creating a Servlet Example
We can also use the above environment to create a Servlet file by selecting "File-->New-->Servlet":
Create the "HelloServlet" class under the /TomcatTest/src directory of the TomcatTest project, with the package "com.tutorialpro.test":
HelloServlet.java code is shown below:
package com.tutorialpro.test;
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 HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Use GBK to display Chinese characters correctly
response.setCharacterEncoding("GBK");
response.getWriter().write("tutorialpro.org:http://www.tutorialpro.org");
}
/**
* @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);
}
Create the /TomcatTest/WebContent/WEB-INF/web.xml
file (if not already present) with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<!-- Class name -->
<servlet-name>HelloServlet</servlet-name>
<!-- Package location -->
<servlet-class>com.tutorialpro.test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<!-- URL to access -->
<url-pattern>/TomcatTest/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
Then restart Tomcat and access http://localhost:8080/TomcatTest/HelloServlet in the browser.
Reference article: http://www.cnblogs.com/smyhvae/p/4046862.html