注解开发Servlet

自动注解开发Servlet

  • 代码展示:
package com.lichee.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
    自动注解配置Servlet
    @WebServlet("Servlet路径")
 */
@WebServlet("/servletDemo1")
public class ServletDemo1 extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet执行了...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

注解详解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
    //指定Servlet的名称。等效于web.xml文件<servlet>标签下的<servlet-name>
    String name() default "";
	//用于映射Servlet。等效于<url-pattern>
    String[] value() default {};
    String[] urlPatterns() default {};
	//指定Servlet的加载时机。等效于<load-on-startup>
    int loadOnStartup() default -1;
	//指定Servlet的初始化参数。等效于<init-param>
    WebInitParam[] initParams() default {};
	//指定Servlet是否支持异步
    boolean asyncSupported() default false;
	//指定Servlet的小图标
    String smallIcon() default "";
	//指定Servlet的大图标
    String largeIcon() default "";
	//指定Servlet的描述信息
    String description() default "";
	//指定Servlet的显示名称
    String displayName() default "";
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐