淘先锋技术网

首页 1 2 3 4 5 6 7

运行环境:win10+jdk8+tomcat7+eclipse(jee-oxyen)

1.创建一个动态web项目

方法:File->New->Dynamic Web Project

如图:

方法2:File->New->Project,在窗口中找到Dynamic Web Project

如图:

2.设置动态Web

PrjectName:HelloWorldServlet

然后Next,Next,勾选上Generate web.xml,这个选项会在WebContent/WEB-INF下创建web.xml,这个文件也可以后面自己添加

3.创建Servlet

方法1:右键点击src目录打开菜单,选择Servlet,输入ClassName,点击Next,新窗口中URLImage下的路径就是之后的调用路径

最后自动生成的代码,注意@WebServlet("/HelloWorld"),这个是告诉Web容器这个Servlet的信息

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 HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @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);
	}

}

然后修改一下web.xml,将HelloWorld设为启动页

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloWorldServlet</display-name>
  <welcome-file-list>
    <welcome-file>HelloWorld</welcome-file>
  </welcome-file-list>
</web-app>

右键点击打开菜单选择Run->Run As Server

方法2:新建一个类HelloWorld2,继承HttpServlet,实现doGet接口,如下:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld2 extends HttpServlet {

	private String message;
	
    public HelloWorld2() {
        super();
        // TODO Auto-generated constructor stub
        message = "HelloWorld";
    }
    
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.getWriter().println("<h1>" + message + "</h1>");
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}

注意,这里没有@WebServlet,需要在web.xml做如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloWorldServlet</display-name>
  <welcome-file-list>
    <welcome-file>HelloWorld2</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>HelloWorld2</servlet-name>
  	<servlet-class>HelloWorld2</servlet-class>
  </servlet><servlet-mapping>
  	<servlet-name>HelloWorld2</servlet-name>
  	<url-pattern>/HelloWorld2</url-pattern>
  </servlet-mapping>
  
</web-app>

右键点击项目打开菜单选择Run->Run As Server

注:@WebServlet和url-pattern里面的“/”一定不要漏掉