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):

232104286524890.png

After downloading, unzip the compressed package to drive D (you can choose it yourself):

232104410586765.png

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:

232105392158264.png

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:

tomcat-index.jpg

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:

232106198557249.jpg

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:

test-jsp.jpg


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:

232111301681549.png

In the picture above, click the add button of "add", the following interface will pop up:

232111442933866.png

In the options, we select the corresponding Tomcat version, then click "Next", select the Tomcat installation directory, and select the Java environment we installed:

232112245587963.png

Click "Finish" , complete the configuration.

Create an instance

Select "File-->New-->Dynamic Web Project" to create the TomcatTest project:

232112541213100.png

302044303245040.png

Click on the red box in the picture above, and the following interface will pop up:

8998BEC1-D622-4BD4-A9E6-8B18D2A5F29C.jpg

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:

232113121219000.png

232113256216676.png

## Project file structure:

232113367466511.png

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.

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>php中文网</title>
</head>
<body>
<%
	out.println("Hello World!");
%>
</body>
</html>

Before running the program, we first modify the browser options:

testjsp1.png

Then we run the project:

runas.png

When running, the following error pops up: (If there is no such error, please ignore it)

232120047932694.png

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:

232120199803353.png

Browser accesshttp://localhost:8080/TomcatTest/test.jsp, the normal result will be output:

A72F19CD-4FEA-4AE3-8D91-43B34623EC37.jpg


Servlet instance creation

We can also use the above environment to create a Servlet file, select "File-->New-->Servlet" :

sns.png

Create the "HelloServlet" class in the /TomcatTest/src directory of the TomcatTest project, and the package is "com.php.test":

22D8CED0-F2DD-4554-BFBD-2B19D1685FB9.jpg

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:

test-jsp.jpg

Reference article: http://www.cnblogs.com/smyhvae/p/4046862.html