一、Servlet3.0介绍
Servlet3.0提供了注解(annotation),使得不再需要在web.xml文件中进行Servlet的部署描述,简化开发流程。
二、开发Servlet3.0程序的所需要的环境
开发Servlet3.0的程序需要一定的环境支持。Tomcat7都提供了对Java EE6规范的支持。Tomcat需要Tomcat7才支持Java EE6,Tomcat7需要使用JDK1.6以上的版本。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
新建一个测试的Servlet
package mine.service;
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 TestServlet
*/
@WebServlet("/TestServlet.do")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Hello Servlet3.0");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Hello Servlet3.0");
}
}
代码中的 @WebServlet("/TestServlet.do") 就用注解方式配置了该Servlet的访问方式,即用/TestServlet.do 来访问这个Servlet,这样就不需要在web.xml中进行配置了。
标签:Servlet3.0,TestServlet,javax,HttpServlet,import,response From: https://blog.51cto.com/u_16160131/6473892