English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Eclipse JSP/Servlet Environment Setup

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 the JSP development environment. First, let's download the software packages separately:

Tomcat Download and Installation

You can download the corresponding package for your system (the following takes the Windows system as an example):

After downloading, extract the compressed package to the D disk (you can choose for yourself):

Note that the directory name cannot contain Chinese characters and spaces. The directory introduction is as follows:

  • bin: binary executable files. The most commonly used file isstartup.batIf it is a Linux or Mac system, the startup file is startup.sh.

  • conf: configuration directory. The most core file isserver.xml. You can change the port number in it. The default port number is8080, which means that this port number cannot be occupied by other applications.

  • lib: Library files. The directory where the jar packages required by Tomcat at runtime are located

  • logs: Logs.

  • temp: Temporary files generated, i.e., cache.

  • webapps: Web applications.Web applications are placed in this directory, and the browser can directly access them.

  • work: Class files compiled after compilation.

Next, 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 close the server, you can directly close the window above or enter Ctrl+C to stop the service.

Next, we enter http://localhost:8080/If the following interface pops up, it means that Tomcat is installed successfully and has started up:

Now let's test it on the browser:

Firstly, in D:\apache-tomcat-8.0.14New a jsp file in the \webapps\ROOT directory:

test.jsp file code is as follows:

<%@ page contentType="text/html;charset=UTF-8" %>
<%
out.print("JSP tutorial: http:")//www.oldtoolbag.com");
%>

Next, visit the address in the browser: http://localhost:8080/test.jsp, The output result is as follows:

Associate Tomcat and Eclipse

Eclipse J2After downloading EE, unzip it and it can be used, we open Java EE, select the menu bar Windows-->preferences (Eclipse on Mac system-->Preferences), pop up the following interface:

In the figure 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 installation directory of Tomcat, and select the Java environment we installed:

Click "Finish", complete the configuration.

Create example

Select "File-->New-->Dynamic Web Project", create a TomcatTest project:

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

Note that if you have already selected the Tomcat and JDK we installed earlier by default, you can skip this step.

Then, click finish, continue:

Project file structure:

Parsing of each directory in the figure above:

  • deployment descriptor: Description of the deployment.

  • Web App Libraries: You can put the packages you added here.

  • build: Place the compiled files into it.

  • WebContent: put the written page in.

Create a test.jsp file under the WebContent folder. As can be seen in the figure below, it has the default code:

<%@ 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>

Next, let's modify the code of the test.jsp file 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>Basic Tutorial Website</title>
</head>
<body>
<%
    out.println("Hello World!");
%>
</body>
</html>

Before the program runs, let's modify the browser options first:

Next, we run the project:

An error will pop up during runtime: (If there is no such error, please ignore)

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 unnecessary, because the server will automatically open the Tomcat server when the program runs. Therefore, we first manually close the Tomcat software, run the program again, and it will work. The console information is as follows:

Browser access http://localhost:8080/TomcatTest/test.jsp, and it will output the normal result:

Servlet Example Creation

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

located in the TomcatTest project /TomcatTest/Create the "HelloServlet" class under the "src" directory, the package is "com.w3codebox.test":

HelloServlet.java Code as follows:

package com.w;3codebox.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 set Chinese to display normally
        response.setCharacterEncoding("GBK");
        response.getWriter().write("JSP tutorial: http://www.oldtoolbag.com");
    }
    /**
     * @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);
    }
}

Create /TomcatTest/WebContent/WEB-INF/The web.xml file (if not present), the code is as follows:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  <servlet  
     <!-- Class name -->  
    <servlet-name>HelloServlet</servlet-name>  
    <!-- The package where -->  
    <servlet-class>com.w3codebox.test.HelloServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>HelloServlet</servlet-name>  
    <!-- The visited URL -->  
    <url-pattern>/TomcatTest/HelloServlet</url-pattern>  
    </servlet-mapping>  
</web-app>

Then restart Tomcat, and access http: through the browser://localhost:8080/TomcatTest/HelloServlet:

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