Eclipse JSP/Servlet environment
This article assumes that you have a JDK environment installed, and if not, you can refer to the Java development environment configuration.
We can use Eclipse to build a JSP development environment, and first we'll download the packages separately:
- Eclipse J2EE: http://www.eclipse.org/downloads/
- Tomcat: http://tomcat.apache.org/download-70.cgi
Tomcat downloads the installation
You can download the corresponding package based on your system (here's an example of a Window system):
After downloading, unzip the package to the D disk (you can choose from your own):
Note that directory names cannot have Chinese and spaces. The catalog is described below:
- bin: Binary execution file. The most commonly used file is the startup .bat if the Linux or Mac system boot file is startup.sh.
- conf: Configure the directory. T he core file inside is the server .xml. Y ou can change the port number and so on inside. The default port number is 8080, which means that it cannot be consumed by other applications.
- lib: Library files. The directory where the jar package is required for the tomcat runtime
- Logs: Logs
- temp: The temporaryly generated file, the cache
- Webapps: Web applications. Web apps placed in this directory can be accessed directly by the browser
- work: Compiled class file.
Then we can double-click on the startup .bat start Tomcat and pop up the following interface:
By this time, the local server had been set up. If you want to shut down the server, you can either close the window above directly or enter the Ctrl-C disable service inside.
Then we enter the http://localhost:8080/ browser, and if the following interface pops up, the tomcat installation is successful and started:
Let's test it on the browser now:
Start by creating a new jsp file in the D:-apache-tomcat-8.0.14-webapps-ROOT directory:
The .jsp file code is as follows:
<%@ page contentType="text/html;charset=UTF-8" %> <% out.print("w3cschool教程 : http://www.w3cschool.cn"); %>
Then access the address in the browser http://localhost:8080/test.jsp, and the output is as follows:
Associate Tomcat with Eclipse
Once eclipse J2EE is downloaded, the decompression is ready to use, and we open Java EE, select the menu bar Windows-gt;preferences (Mac system for Eclipse-gt; preferences), and pop up the following interface:
In the image above, click the add button for "add" and the following interface pops up:
In the options, we select the corresponding version of Tomcat, then click "Next", select Tomcat's installation directory, and select the Java environment we installed:
Click "Finish" to complete the configuration.
Create an instance
Select File--New--Dynamic Web Project to create the TomcatTest project:
Click on the red box section of the image above and pop up the following interface:
Note This step can be skipped if we have selected Tomcat and JDK previously installed by default.
Then, click finish to continue:
Engineering file structure:
Each directory resolution in the image above:
- deploy descriptor: Description of the deployment.
- Web App Libraries: You can put your own bag in it.
- Build: Put in the compiled file.
- WebContent: Put it on the written page.
Create a new test file under the WebContent .jsp folder. Y ou can see its default code in the following image:
<%@ 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>
Let's then modify the .jsp file code as follows:
<%@ 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>w3cschool教程</title> </head> <body> <% out.println("Hello World!"); %> </body> </html>
Before the program runs, let's modify the browser options:
Then we run the project:
At runtime, the following error pops up: (If this error is not, ignore it)
The reason is that we previously clicked on the startup .bat in the Tomcat installation package, which manually opened the Tomcat server, which is obviously redundant because eclipse automatically turns on the Tomcat server while the program is running. S o let's manually turn off the tomcat software and run the program again. The console information is as follows:
Browser access http://localhost:8080/TomcatTest/test.jsp to output normal results:
Servlet instance creation
We can also use the above environment to create a servlet file and select "File--New--Servlet":
Create the "HelloServlet" class under the TomcatTest project/TomcatTest/src directory, packaged "com.youj.test":
HelloServlet.java code looks like this:
package com.youj.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 { // 使用 GBK 设置中文正常显示 response.setCharacterEncoding("GBK"); response.getWriter().write("w3cschool教程:http://www.w3cschool.cn"); } /** * @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); } }
Then restart Tomcat and the browser accesses http://localhost:8080/TomcatTest/HelloServlet:
Reference article: http://www.cnblogs.com/smyhvae/p/4046862.html