Eclipse JSP/Servlet
This article assumes that you have installed the JDK environment. If not, please refer to Java Development Environment Configuration.
We can use Eclipse to build a JSP development environment. First, we download the software packages respectively:
Eclipse J2EE: http://www .eclipse.org/downloads/
Tomcat: http://tomcat.apache.org/download-70.cgi
Tomcat download and installation
You can download the corresponding package according to your system (the following takes the Window system as an example):
After downloading, unzip the compressed package to drive D (you can choose it yourself):
Note that the directory name cannot contain Chinese characters or spaces. The directory introduction is as follows:
#bin: binary execution file. The most commonly used file is startup.bat. If it is a Linux or Mac system, the startup file is startup.sh.
conf: Configuration directory. The core file inside is server.xml. You can change the port number etc. inside. The default port number is 8080, which means that this port number cannot be occupied by other applications.
lib: library file. The directory where the jar package required when tomcat is running
logs: Log
temp: Temporarily generated file, that is, cache
webapps: web applications. The web application is placed in this directory and the browser can directly access the
work: the class file after compilation.
Then we can double-click startup.bat to start Tomcat, and the following interface will pop up:
At this time, the local server has been set up Woke up. If you want to shut down the server, you can directly close the window above, or enter Ctrl+C to disable the service.
Then we enter http://localhost:8080/ in the browser. If the following interface pops up, it means that tomcat is successfully installed and started:
Let’s test it on the browser now:
First create a new jsp file in the D:\apache-tomcat-8.0.14\webapps\ROOT directory:
test.jsp file code is as follows:
<%@ page contentType="text/html;charset=UTF-8" %> <% out.print("php中文网 : http://www.php.cn"); %>
Then access the address http://localhost:8080/test.jsp in the browser, the output result is as follows:
Associate Tomcat with Eclipse
After Eclipse J2EE is downloaded, unzip it and you can use it. We open Java EE and select Windows-->preferences in the menu bar (Eclipse-->Preferences for Mac system), The following interface will pop up:
In the picture above, click the add button of "add", the following interface will pop 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" , complete the configuration.
Create an instance
Select "File-->New-->Dynamic Web Project" to create the TomcatTest project:
Click on the red box in the picture above, and the following interface will pop up:
Note that if the Tomcat and Tomcat we installed before have been selected by default JDK can skip this step.
Then, click finish and continue:
## Project file structure: Analysis of each directory in the above figure:- deployment descriptor: description of deployment.
- Web App Libraries: The packages you add can be placed in them.
- build: Put the compiled file.
- WebContent: Put into the written page.
<%@ 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>
Then we modify the test.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>php中文网</title> </head> <body> <% out.println("Hello World!"); %> </body> </html>
Before running the program, we first modify the browser options:
Then we run the project:
When running, the following error pops up: (If there is no such error, please ignore it)
The reason is that we previously clicked on startup.bat in the Tomcat installation package, which manually opened the Tomcat server. This is obviously redundant, because when the program is running, eclipse will automatically open the Tomcat server. So we first manually shut down the tomcat software and run the program again, and that's it. The console information is as follows:
Browser accesshttp://localhost:8080/TomcatTest/test.jsp, the normal result will be output:
Servlet instance creation
We can also use the above environment to create a Servlet file, select "File-->New-->Servlet" :
Create the "HelloServlet" class in the /TomcatTest/src directory of the TomcatTest project, and the package is "com.php.test":
HelloServlet.java The code is as follows:
package com.php.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("php中文网:http://www.php.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