Servlet随笔

Posted

tags:

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

1.Servlet和ServletConfig的关系,这里GenericServlet实现了ServletConfig接口,却引用了一个对应ServletConfig类型实例,这里用到了代理模式.通过init(ServletConfig config)方法初始化类引用.
  1 public abstract class GenericServlet implements Servlet, ServletConfig,
  2         java.io.Serializable {
  3 
  4     private static final long serialVersionUID = 1L;
  5 
  6     private transient ServletConfig config;
  7 
  8     /**
  9      * Does nothing. All of the servlet initialization is done by one of the
 10      * <code>init</code> methods.
 11      */
 12     public GenericServlet() {
 13         // NOOP
 14     }
 15 
 16     /**
 17      * Called by the servlet container to indicate to a servlet that the servlet
 18      * is being taken out of service. See {@link Servlet#destroy}.
 19      */
 20     @Override
 21     public void destroy() {
 22         // NOOP by default
 23     }
 24 
 25     /**
 26      * Returns a <code>String</code> containing the value of the named
 27      * initialization parameter, or <code>null</code> if the parameter does not
 28      * exist. See {@link ServletConfig#getInitParameter}.
 29      * <p>
 30      * This method is supplied for convenience. It gets the value of the named
 31      * parameter from the servlet‘s <code>ServletConfig</code> object.
 32      *
 33      * @param name
 34      *            a <code>String</code> specifying the name of the
 35      *            initialization parameter
 36      * @return String a <code>String</code> containing the value of the
 37      *         initialization parameter
 38      */
 39     @Override
 40     public String getInitParameter(String name) {
 41         return getServletConfig().getInitParameter(name);
 42     }
 43 
 44     /**
 45      * Returns the names of the servlet‘s initialization parameters as an
 46      * <code>Enumeration</code> of <code>String</code> objects, or an empty
 47      * <code>Enumeration</code> if the servlet has no initialization parameters.
 48      * See {@link ServletConfig#getInitParameterNames}.
 49      * <p>
 50      * This method is supplied for convenience. It gets the parameter names from
 51      * the servlet‘s <code>ServletConfig</code> object.
 52      *
 53      * @return Enumeration an enumeration of <code>String</code> objects
 54      *         containing the names of the servlet‘s initialization parameters
 55      */
 56     @Override
 57     public Enumeration<String> getInitParameterNames() {
 58         return getServletConfig().getInitParameterNames();
 59     }
 60 
 61     /**
 62      * Returns this servlet‘s {@link ServletConfig} object.
 63      *
 64      * @return ServletConfig the <code>ServletConfig</code> object that
 65      *         initialized this servlet
 66      */
 67     @Override
 68     public ServletConfig getServletConfig() {
 69         return config;
 70     }
 71 
 72     /**
 73      * Returns a reference to the {@link ServletContext} in which this servlet
 74      * is running. See {@link ServletConfig#getServletContext}.
 75      * <p>
 76      * This method is supplied for convenience. It gets the context from the
 77      * servlet‘s <code>ServletConfig</code> object.
 78      *
 79      * @return ServletContext the <code>ServletContext</code> object passed to
 80      *         this servlet by the <code>init</code> method
 81      */
 82     @Override
 83     public ServletContext getServletContext() {
 84         return getServletConfig().getServletContext();
 85     }
 86 
 87     /**
 88      * Returns information about the servlet, such as author, version, and
 89      * copyright. By default, this method returns an empty string. Override this
 90      * method to have it return a meaningful value. See
 91      * {@link Servlet#getServletInfo}.
 92      *
 93      * @return String information about this servlet, by default an empty string
 94      */
 95     @Override
 96     public String getServletInfo() {
 97         return "";
 98     }
 99 
100     /**
101      * Called by the servlet container to indicate to a servlet that the servlet
102      * is being placed into service. See {@link Servlet#init}.
103      * <p>
104      * This implementation stores the {@link ServletConfig} object it receives
105      * from the servlet container for later use. When overriding this form of
106      * the method, call <code>super.init(config)</code>.
107      *
108      * @param config
109      *            the <code>ServletConfig</code> object that contains
110      *            configuration information for this servlet
111      * @exception ServletException
112      *                if an exception occurs that interrupts the servlet‘s
113      *                normal operation
114      * @see UnavailableException
115      */
116     @Override
117     public void init(ServletConfig config) throws ServletException {
118         this.config = config;
119         this.init();
120     }
121 
122     /**
123      * A convenience method which can be overridden so that there‘s no need to
124      * call <code>super.init(config)</code>.
125      * <p>
126      * Instead of overriding {@link #init(ServletConfig)}, simply override this
127      * method and it will be called by
128      * <code>GenericServlet.init(ServletConfig config)</code>. The
129      * <code>ServletConfig</code> object can still be retrieved via
130      * {@link #getServletConfig}.
131      *
132      * @exception ServletException
133      *                if an exception occurs that interrupts the servlet‘s
134      *                normal operation
135      */
136     public void init() throws ServletException {
137         // NOOP by default
138     }
139 
140     /**
141      * Writes the specified message to a servlet log file, prepended by the
142      * servlet‘s name. See {@link ServletContext#log(String)}.
143      *
144      * @param msg
145      *            a <code>String</code> specifying the message to be written to
146      *            the log file
147      */
148     public void log(String msg) {
149         getServletContext().log(getServletName() + ": " + msg);
150     }
151 
152     /**
153      * Writes an explanatory message and a stack trace for a given
154      * <code>Throwable</code> exception to the servlet log file, prepended by
155      * the servlet‘s name. See {@link ServletContext#log(String, Throwable)}.
156      *
157      * @param message
158      *            a <code>String</code> that describes the error or exception
159      * @param t
160      *            the <code>java.lang.Throwable</code> error or exception
161      */
162     public void log(String message, Throwable t) {
163         getServletContext().log(getServletName() + ": " + message, t);
164     }
165 
166     /**
167      * Called by the servlet container to allow the servlet to respond to a
168      * request. See {@link Servlet#service}.
169      * <p>
170      * This method is declared abstract so subclasses, such as
171      * <code>HttpServlet</code>, must override it.
172      *
173      * @param req
174      *            the <code>ServletRequest</code> object that contains the
175      *            client‘s request
176      * @param res
177      *            the <code>ServletResponse</code> object that will contain the
178      *            servlet‘s response
179      * @exception ServletException
180      *                if an exception occurs that interferes with the servlet‘s
181      *                normal operation occurred
182      * @exception IOException
183      *                if an input or output exception occurs
184      */
185     @Override
186     public abstract void service(ServletRequest req, ServletResponse res)
187             throws ServletException, IOException;
188 
189     /**
190      * Returns the name of this servlet instance. See
191      * {@link ServletConfig#getServletName}.
192      *
193      * @return the name of this servlet instance
194      */
195     @Override
196     public String getServletName() {
197         return config.getServletName();
198     }
199 }

 

以上是关于Servlet随笔的主要内容,如果未能解决你的问题,请参考以下文章

Servlet随笔

20160928随笔

Servlet随笔

Servlet随笔

servlet相关随笔,API

JavaWeb学习随笔