打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何编写一个Servlet处理类并设置其初始化参数
一.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)@Documentedpublic @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() */语言方法
59613ZaIv
z3qrs「免费教你」自媒体怎么赚钱「日赚千元」
74232010-05-19 06:38:32
//作用与urlPatterns相同,指定Servlet处理的URL但不能与urlPatterns同时出现 String[] value() default {}; /** * @return array of URL patterns to which this Filter applies */ //指定Servlet处理的URL String[] urlPatterns() default {}; /** * @return load on startup ordering hint */ //指定Servlet的加载顺序 int loadOnStartup() default -1; /** * @return array of initialization params for this Servlet */ //指定初始化参数类型是@webinitparam注解数组 WebInitParam[] initParams() default {}; /** * @return asynchronous operation supported by this Servlet */ boolean asyncSupported() default false; /** * @return small icon for this Servlet, if present */ String smallIcon() default ""; /** * @return large icon for this Servlet, if present */ String largeIcon() default ""; /** * @return description of this Servlet, if present */ String description() default ""; /** * @return display name of this Servlet, if present */ String displayName() default "";}
三.获取初始化参数
调用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();
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
通过源码详解 Servlet
JavaWeb框架系列-Servlet篇(2)
getServletContext()方法详解
Servlet(二)
Servlet基础知识详解
JSP的3个编译指令,7个动作指令,9个内置对象
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服