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:
Eclipse J2EE: http://www.eclipse.org/downloads/
Tomcat: http://tomcat.apache.org/download-70.cgi
You can download the corresponding package according to your system (the following takes Windows system as an example):
After downloading, unzip the compressed package to the D drive (you can choose it yourself):
Note that the directory name cannot contain Chinese characters or spaces. The catalog is introduced as follows:
bin: Binary executable 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: logs
temp: Temporarily generated files, i.e. cache
webapps: web applications. The web application is placed in this directory and the browser can directly access it.
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. 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.14webappsROOT directory:
The test.jsp file code is as follows:
<%@ page contentType="text/html;charset=UTF-8" %><%out.print("w3cschool tutorial: http://www.w3cschool.cn");%>
Then access the address http://localhost:8080/test.jsp in the browser, the output result is as follows:
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), and the following interface pops up:
In the picture above, click the "add" button and 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" to complete the configuration.
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 you can skip this step if the Tomcat and JDK we installed earlier have been selected by default.
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 yourself can be placed in them.
build: put the compiled file.
WebContent: put into the written page.
Create a new test.jsp file in the WebContent folder. Its default code can be seen in the image 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>
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>w3cschool tutorial</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:
When the browser accesses http://localhost:8080/TomcatTest/test.jsp , normal results will be output:
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.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 {//Use GBK Set Chinese to display normally response.setCharacterEncoding("GBK");response.getWriter().write("w3cschool tutorial: 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 stubdoGet(request, response);}}
Then restart Tomcat and access http://localhost:8080/TomcatTest/HelloServlet with the browser: