Latest web development tutorials

Eclipse JSP / Servlet environment to build

This article assumes that you have installed the JDK environment, if not installed, refer to the Java development environment configuration .

We can use the Eclipse development environment to build JSP, first we look at each download package:


Download and install Tomcat

You can (hereinafter to Window system for example) depending on your system, download the corresponding package:

After downloading, unpack the archive to the D drive (you can choose):

Note that the directory names can not have Chinese and spaces. Directory as follows:

  • bin: binary executable file. The most commonly used inside the file is startup.bat, if it is Linux or Mac system startup file startup.sh.
  • conf: configuration directory. Inside the core file is server.xml. You can change the port number on the inside. The default port number is 8080, that is, the port number can not be used by another application.
  • lib: library files. Directory tomcat runtime required jar package is located
  • logs: Logs
  • temp: temporary files generated, namely cache
  • webapps: web application. Place this web browser application can directly access the directory
  • work: after compiling class files.

Then we can double startup.bat start Tomcat, pop up the following interface:

This time, the local server has been built up. If you want to shut down the server, you can just close the window above, or in which the input Ctrl + C prohibited services.

Then we enter the browserto http: // localhost: 8080 /, if the following interface pops up, showing tomcat successful installation and start up:

We are now testing the browser look at it now:

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

test.jsp file code is as follows:

<%@ page contentType="text/html;charset=UTF-8" %>
<%
out.print("本教程 : http://www.w3big.com");
%> 

Then access the browser addresshttp: // localhost: 8080 / test.jsp, output results are as follows:


The associated Tomcat and Eclipse

Eclipse J2EE download, decompression can be used, we open Java EE, select the menu bar Windows -> preferences (Mac system Eclipse -> Preferences), pop up the following interface:

The figure above, click on the "add" button to add pop up the following interface:

In the options, we select the corresponding version of Tomcat, then click "Next", select the Tomcat installation directory, and we choose to install Java environment:

Click "Finish", to complete the configuration.

Create an instance

Select "File -> New -> Dynamic Web Project", created TomcatTest items:

Opening the red box in the figure above, the following interface pops up:

Note If you have selected by default we previously installed Tomcat and JDK you can skip this step.

Then, click finish, continued:

Project file structure:

Each directory figure above analysis:

  • deployment descriptor: deployment description.
  • Web App Libraries: add their own packages can be placed inside.
  • build: Add files compiled after.
  • WebContent: written into the page.

Create a new file in the WebContent test.jsp folder. In the figure below you can see it in 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>

Then we modify the next test.jsp file code is 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>本教程</title>
</head>
<body>
<%
	out.println("Hello World!");
%>
</body>
</html>

Before running the program, we have to modify your browser options:

Then we run the project:

Run-time menu, the following error :( Without this error, please ignore)

The reason is that we've clicked Tomcat installation package startup.bat, such an action would manually open the Tomcat server, this is obviously unnecessary, because the program is running, eclipse will automatically open the Tomcat server. So we have to manually turn off the tomcat software, run the program again on the line. Console information is as follows:

Browser tohttp: // localhost: 8080 / TomcatTest / test.jsp, you can output a normal result:


Servlet instance creation

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

Located TomcatTest project / TomcatTest / Create "HelloServlet" category under the src directory for the package "com.w3big.test":

HelloServlet.java code as follows:

package com.w3big.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("本教程:http://www.w3big.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/web.xml file (if not), 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>  
     <!-- 类名 -->  
    <servlet-name>HelloServlet</servlet-name>  
    <!-- 所在的包 -->  
    <servlet-class>com.w3big.test.HelloServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>HelloServlet</servlet-name>  
    <!-- 访问的网址 -->  
    <url-pattern>/TomcatTest/HelloServlet</url-pattern>  
    </servlet-mapping>  
</web-app>  

Then restart Tomcat, browser to accesshttp: // localhost: 8080 / TomcatTest / HelloServlet:

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