Eclipse Jsp
This article assumes you have already installed the JDK environment. If not, you can refer to (#).
We can use Eclipse to set up the JSP development environment. First, let's download the following software packages respectively:
* **Eclipse J2EE๏ผ**[http://www.eclipse.org/downloads/](http://www.eclipse.org/downloads/)
* **Tomcat๏ผ**[http://tomcat.apache.org/download-70.cgi](http://tomcat.apache.org/download-70.cgi)
* * *
## Tomcat Download and Installation
You can download the corresponding package according to your system (taking the Windows system as an example below):
!(#)
After downloading, extract the compressed package to the D drive (you can choose yourself):
!(#)
Note that the directory name must not contain Chinese characters or spaces. The directory introduction is as follows:
* bin๏ผBinary execution files. The most commonly used file inside is **startup.bat**, if it is a Linux or Mac system, the startup file is **startup.sh**.
* conf: Configuration directory. The most core file inside is **server.xml**. You can change the port number, etc. in it. The default port number is 8080, which means this port number cannot be occupied by other applications.
* lib๏ผLibrary files. The directory where the jar packages required for tomcat runtime are located.
* logs๏ผLogs
* temp๏ผTemporarily generated files, i.e., cache
* webapps๏ผWeb applications. **Web applications placed in this directory can be accessed directly by the browser**
* work๏ผCompiled class files.
Then we can double-click startup.bat to start Tomcat, and the following interface will pop up:
!(#)
At this point, the local server has been set up. If you want to close the server, you can directly close the above window, or type Ctrl+C inside to stop the service.
Then we enter **http://localhost:8080/** in the browser, if the following interface pops up, it means tomcat is installed successfully and started:
!(#)
Now let's test it in the browser:
First, create a new jsp file in the D:apache-tomcat-8.0.14webappsROOT directory:
!(#)
The test.jsp file code is as follows:
Then visit the address **http://localhost:8080/test.jsp** in the browser, the output result is as follows:
!(#)
* * *
## Associating Tomcat with Eclipse
After downloading Eclipse J2EE, just extract it and use it. We open Java EE, select the menu bar Windows-->preferences (For Mac system, it is Eclipse-->Preferences), and the following interface pops up:
!(#)
In the above figure, click the "add" button, and the following interface pops up:
!(#)
In the options, we select the corresponding Tomcat version, then click "Next", select the Tomcat installation directory, and select the Java environment we installed:
!(#)
Click "Finish" to complete the configuration.
### Create Instance
Select "File-->New-->Dynamic Web Project" to create the TomcatTest project:
!(#)
!(#)
Click the red box part in the figure above, and the following interface pops up:
!(#)
Note that if the Tomcat and JDK we installed earlier have been selected by default, you can skip this step.
Then, click finish to continue:
!(#)
!(#)
Project file structure:
!(#)
Analysis of each directory in the above figure:
* deployment descriptor๏ผDeployment description.
* Web App Libraries๏ผPackages you add can be placed inside.
* build๏ผPut the compiled files inside.
* WebContent๏ผPut the written pages inside.
Create a new test.jsp file under the WebContent folder. You can see its default code in the figure below:
Insert title here
Then we modify the test.jsp file code as shown below:
Before running the program, let's modify the browser options first:
!(#)
Then we run the project:
!(#)
When running, the following error pops up: (If you do not have this error, please ignore it)
!(#)
The reason is that we previously clicked startup.bat in the Tomcat installation package, which manually started the Tomcat server. This is obviously redundant because Eclipse will automatically start the Tomcat server when the program runs. So we first manually close the tomcat software, and then run the program again, and it will work. The console information is as follows:
!(#)
Visit **http://localhost:8080/TomcatTest/test.jsp** in the browser, and the normal result can be output:
!(#)
* * *
## Servlet Instance Creation
We can also use the above environment to create Servlet files, select "File-->New-->Servlet":
!(#)
Create the "HelloServlet" class under the /TomcatTest/src directory of the TomcatTest project, with the package "com..test":
!(#)
The HelloServlet.java code is as follows:
package com..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
YouTip