Servlet key notes

一.Servlet is what?

2. My own insights: is a Java program that runs on the server side, responds differently to the client's operations, and implements a piece of code for different functions. An implementable interface in Java.

3.字释意:Servletfor Server Applet Abbreviation (refer to Baidu translation)


二. Method in interface @@@ There are five methods

init in @1.Servlet for the server to execute when loading the servlet. The server can be configured to load the servlet when the server or client first accesses the servlet. Init() is not repeated, no matter how many clients access the servlet, and init() is executed only once.

package javax.servlet;

public interface Servlet {
    public void init(ServletConfig config) throws ServletException;

    public ServletConfig getServletConfig();
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException;

    public String getServletInfo();

    public void destroy();


1.1  init()

getServletConfig() is used to return the initialization parameters and

1.2  getServletConfig()


as an interface

This interface is mainly to provide some environment information when the servlet is initialized 为一个接口

 * A servlet configuration object used by a servlet container to pass
 * information to a servlet during initialization.
public interface ServletConfig {

     * Returns the name of this servlet instance. The name may be provided via
     * server administration, assigned in the web application deployment
     * descriptor, or for an unregistered (and thus unnamed) servlet instance it
     * will be the servlet's class name.
     * @return the name of the servlet instance
    public String getServletName();

     * Returns a reference to the {@link ServletContext} in which the caller is
     * executing.
     * @return a {@link ServletContext} object, used by the caller to interact
     *         with its servlet container
     * @see ServletContext
    public ServletContext getServletContext();

     * 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);

     * 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();

this method of this interface @ @@@

Mainly used to get a servlet environment

此接口的此方法 ServletContext usage (public ServletContext getServletContext();主要 用来获取一个servlet环境ServletContext is also an interface, this environment has a lot of methods (ServletContext),此环境可以用来存储一些全局信息。如下:

, this environment can be used to store some global information. As follows:

//Get ServletContext HttpSession session = request.getSession(); ServletContext application = session.getServletContext(); / / Save the value Note the first one is String, the second is Object application.setAttribute(String name,Object value); / / value Object obj = application.getAttribute(String name); / / Remove the attribute application.removeAttribute(name);Commonly available

1Get the environment path 

2Get the resource

3Set the resource

4Remove the resource

5Get Forwarding jumper (RequestDispatcher is also an interface with forword and include methods)

The most commonly used method: for accepting requests

and responding to requests

   1 public String getContextPath();
   2 public Object getAttribute(String name);
   3 public void setAttribute(String name, Object object);
   4 public void removeAttribute(String name);
   5 public RequestDispatcher getRequestDispatcher(String path);

1.3 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;

is usually using its abstract class req GenericServlet(This class inherits res

Servlet, ServletConfigGenericServlet(此类继承了)的子类HttpServlet


service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}方法

1.4 public String getServletInfo();

1.5 public void destroy();

public class LogOut extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		response.sendRedirect(request.getContextPath() + "/JSPCON/Login.jsp");