一.Servlet继承体系Servlet接口:总接口,定义了Servlet的不同生命周期行为 ServletConfig接口:得到对应Servlet的各种参数 GennericServlet类:是一个抽象类,实现了大部分需要的Servlet方法,大多数Servlet行为都在这里实现 HttpServlet类:抽象类,基于HTTP请求的特性,继承自GennericServlet,实现了自己的service()方法 MyTestServlet:自定义处理类,实现具体的doGet().doPost()方法 //使用注解设置初始化参数 @WebServlet(value = "/test",loadOnStartup = 0) public class MyTestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } } 二.初始化Servlet参数使用注解初始化@WebServlet@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface WebServlet { /** * @return name of the Servlet */ //指定Servlet名字,默认为包名加上类名 String name() default ""; /** * A convenience method, to allow extremely simple annotation of a class. * * @return array of URL patterns * @see #urlPatterns() */
三.获取初始化参数调用ServletConfig接口中的getInitParameter(String name)方法获取指定的参数值,返回String/** * Returns a <code>String</code> containing the value of the named * initialization parameter, or <code>null</code> if the parameter does not * exist. * * @param name * a <code>String</code> specifying the name of the * initialization parameter * @return a <code>String</code> containing the value of the initialization * parameter */ public String getInitParameter(String name); 调用ServletConfig接口中的getInitParameterNames()方法获取全部参数,返回Enumeration<String>/** * Returns the names of the servlet's initialization parameters as an * <code>Enumeration</code> of <code>String</code> objects, or an empty * <code>Enumeration</code> if the servlet has no initialization parameters. * * @return an <code>Enumeration</code> of <code>String</code> objects * containing the names of the servlet's initialization parameters */ public Enumeration<String> getInitParameterNames(); |
|
来自: 昵称70680357 > 《待分类》