码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet3.0 Test

时间:2016-05-17 11:27:13      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/rocky-fang/p/5500587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!