Servlet3.0——Demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rubulai/article/details/80963685

1、tomcat7及以上版本的tomcat服务器才支持Servlet3.0

2、在创建Servlet3.0项目的时候可以不需要web.xml配置文件,改用注解的方式注册web组件(servlet、filter、listener)

3、web项目的默认页面是index.jsp,如果没有该页面的话在浏览器中需输入具体的jsp页面的地址方可进入请求页面,输入localhost:8080/会报404

4、Servlet3.0例子

index.jsp:有了该页面在浏览器中输入localhost:8080即可进入该页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<a href="hello">hello</a>
</body>
</html>

HelloServlet.java:@WebServlet指定了该servlet拦截的请求路径(/hello),在浏览器中输入localhost:8080/hello即可进入该Servlet的doGet方法

@WebServlet("/hello")
public class HelloServlet extends HttpServlet{

	private static final long serialVersionUID = -3279146918985815706L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("Hello ...");
	}

}

也就是说@WebServlet起到了注册Servlet的作用(类似于在web.xml中配置的<servlet>),同样地@WebFilter、@WebListener可以注册filter和listener

猜你喜欢

转载自blog.csdn.net/rubulai/article/details/80963685