Servlet3.0 Test

Posted rocky-fang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet3.0 Test相关的知识,希望对你有一定的参考价值。

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

 

以上是关于Servlet3.0 Test的主要内容,如果未能解决你的问题,请参考以下文章

基础加强

servlet3.0无web.xml

WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

Servlet3.0的动态

Servlet3.0的注解自定义原生Listener监听器实战

Servlet3.0学习总结——基于Servlet3.0的文件上传