Eclipse Jsp
This tutorial assumes you have already installed the JDK environment. If not, please refer to (#).
We can use Eclipse to set up the JSP development environment. First, let's download the necessary software packages:
* **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 based on your operating system (the following example uses Windows):
!(#)
After downloading, extract the compressed file to the D drive (you can choose another location):
!(#)
Note: The directory name must not contain Chinese characters or spaces. The directory structure is explained as follows:
* bin: Binary executable files. The most commonly used file here is **startup.bat**. For Linux or Mac systems, the startup file is **startup.sh**.
* conf: Configuration directory. The core file here is **server.xml**. You can modify settings like the port number here. The default port is 8080, meaning this port cannot be occupied by other applications.
* lib: Library files. This directory contains the JAR packages required for Tomcat to run.
* logs: Log files.
* temp: Temporarily generated files, i.e., cache.
* webapps: Web applications. **Web applications placed in this directory can be accessed directly via a browser.**
* work: Compiled class files.
Next, we can double-click **startup.bat** to start Tomcat. The following interface will appear:
!(#)
At this point, the local server has been set up. If you want to shut down the server, you can directly close the window above or press Ctrl+C inside it to stop the service.
Then, enter **http://localhost:8080/** in your browser. If the following interface appears, it indicates that Tomcat has been installed successfully and is running:
!(#)
Let's test it in the browser:
First, create a new JSP file in the D:apache-tomcat-8.0.14webappsROOT directory:
!(#)
The code for the test.jsp file is as follows:
Then, visit the address **http://localhost:8080/test.jsp** in your browser, and the output will be as follows:
!(#)
* * *
## Associating Tomcat with Eclipse
After downloading Eclipse J2EE, you can use it directly after extraction. Open Java EE, select the menu Windows-->Preferences (for Mac systems, it's Eclipse-->Preferences), and the following interface will appear:
!(#)
In the image above, click the "add" button, and the following interface will appear:
!(#)
In the options, select the corresponding Tomcat version, then click "Next", select the Tomcat installation directory, and choose the Java environment you installed:
!(#)
Click "Finish" to complete the configuration.
### Creating an Instance
Select "File-->New-->Dynamic Web Project" to create the TomcatTest project:
!(#)
!(#)
Click the red box in the image above, and the following interface will appear:
!(#)
Note: If the Tomcat and JDK you installed earlier are already selected by default, you can skip this step.
Then, click finish to continue:
!(#)
!(#)
Project file structure:
!(#)
Explanation of each directory in the image above:
* deployment descriptor: Deployment description.
* Web App Libraries: Packages you add yourself can be placed here.
* build: Compiled files are placed here.
* WebContent: Pages you write are placed here.
Create a new test.jsp file under the WebContent folder. You can see its default code in the image below:
Insert title here
Next, we modify the test.jsp file code as follows:
Before running the program, let's first modify the browser options:
!(#)
Then, we run the project:
!(#)
During execution, the following error may appear (if this error does not occur, 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 clearly redundant because when the program runs, Eclipse will automatically start the Tomcat server. Therefore, we first manually shut down the Tomcat software, then run the program again, and it will work. The console information is as follows:
!(#)
Access **http://localhost:8080/TomcatTest/test.jsp** in the browser to get the normal output:
!(#)
* * *
## Servlet Instance Creation
We can also use the above environment to create Servlet files. Select "File-->New-->Servlet":
!(#)
Create a "HelloServlet" class in the /TomcatTest/src directory of the TomcatTest project, with the package "com..test":
!(#)
The code for HelloServlet.java 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, IOException {// Use GBK to configure normal Chinese display in response.setCharacterEncoding("GBK");response.getWriter().write("οΌ");}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
Create the /TomcatTest/WebContent/WEB-INF/web.xml file (if it does not exist), with the following code:
HelloServlet com..test.HelloServlet HelloServlet /TomcatTest/HelloServlet
Then restart Tomcat and visit **http://localhost:8080/TomcatTest/HelloServlet** in the browser:
!(#)
Reference article: http://www.cnblogs.com/smyhvae/p/4046862.html
YouTip