1. Servlet3.0 Test and Annotation used
你可以从tomcat7中lib文件夹中找到servlet-api.jar
package com.goodfan.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 FirstServlet
*/
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* Default constructor.
*/
public FirstServlet() {
System.out.println("Servlet begin>>>>>>>>");
} /**
* @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);
} }
2.浏览器测试(此时,web.xml文件都没创建)
3. 让某些程序在web工程已启动时就能运行, 可以在WebServlet注解中将loadOnStartup, urlPatterns都配上.
@WebServlet(name="/FirstServlet",urlPatterns="/firstServlet",loadOnStartup=1)
public class FirstServlet extends HttpServlet {
4. 启动web