Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Eclipse JSP/Servlet Environment Setup

Eclipse JSP/Servlet Environment Setup

This article assumes you have installed the JDK. If not, you can refer to Java Development Environment Configuration。

We can use Eclipse to set up a JSP development environment. First, let's download the following software packages:


Tomcat Download and Installation

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

After downloading, extract the archive to the D drive (you can choose your own location):

Note that the directory name must not contain Chinese characters or spaces. The directories are described below:

Next, we can double-click startup.bat to start Tomcat, and the following interface appears:

At this point, the local server is set up. To shut down the server, you can simply close the window above, or type Ctrl+C in it to stop the service.

Next, we enter in the browser http://localhost:8080/, if the following interface appears, it means Tomcat was installed successfully and has started:

Let's test it in the browser now:

First, create a new jsp file in the D:\apache-tomcat-8.0.14\webapps\ROOT directory:

The code of the test.jsp file is as follows:



<%@ page contentType="text/html;charset=UTF-8" %>

<%

out.print("Runoob Tutorial : http://www.runoob.com");

%> 

Then access the following address in the browser http://localhost:8080/test.jsp, and the output is as follows:


Associating Tomcat with Eclipse

After downloading Eclipse J2EE, extract it and it is ready to use. We open Java EE and select Windows-->preferences from the menu bar (on Mac it is Eclipse-->Preferences), and the following interface appears:

In the figure above, click the "add" button, and the following interface appears:

In the options, select the corresponding Tomcat version, then click "Next", choose the Tomcat 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 the red-boxed area in the figure above, and the following interface appears:

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

Then, click finish to continue:

Project file structure:

The directories in the figure above are explained below:

Create a new test.jsp file under the WebContent folder. Its default code can be seen in the figure below:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Insert title here











Next, we modify the test.jsp file code as follows:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Runoob Tutorial





<%

    out.println("Hello World!");

%>





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

Next, we run the project:

When running, the following error appears: (if this error does not appear, ignore this)

The reason is that we previously clicked startup.bat in the Tomcat installation package, which manually started the Tomcat server. This is clearly unnecessary, because when the program runs, Eclipse will automatically start the Tomcat server. So we first manually close the Tomcat software, then run the program again, and it will work. The console output is as follows:

Access in the browser http://localhost:8080/TomcatTest/test.jsp, and the normal result is output:


Servlet Instance Creation

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

Create the "HelloServlet" class under the /TomcatTest/src directory of the TomcatTest project, with the package "com.runoob.test":

The code of HelloServlet.java is as follows:



package com.runoob.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 display Chinese normally

        response.setCharacterEncoding("GBK");

        response.getWriter().write("Runoob Tutorial:http://www.runoob.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 the /TomcatTest/WebContent/WEB-INF/web.xml file (if it does not exist); the code is as follows:



  

  

    

       

    HelloServlet  

      

    com.runoob.test.HelloServlet  

    

    

    HelloServlet  

      

    /TomcatTest/HelloServlet  

      

  

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

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