Servlet--ServletConfig和ServletContext

1.ServletConfig

  web容器启动后,会读取web.xml,根据其中每个servlet的设置,加载servlet类并实例化,并为每个servlet生成一个ServletConfig对象,而后调用Servlet接口的init()方法,将产生的ServletConfig对象当做参数传入。

  ServletConfig定义了getInitParameter()和getInitParameterNames()方法,可以取得设置Servlet时的初始参数。

  public class AddMessage extends HttpServlet {

    private String PARAM1;

    private String PARAM2;

 

    public void init() throws ServletException {

      super.init();

      PARAM1 = getServletConfig().getInitParameter("PARAM1");

      PARAM2 = getServletConfig().getInitParameter("PARAM2");

    }

    ...

  }

  GenericServlet类也定义了同样地方法,所以上例可改成:

  

  public class AddMessage extends HttpServlet {

    private String PARAM1;

    private String PARAM2;

 

    public void init() throws ServletException {

      super.init();

      PARAM1 = getInitParameter("PARAM1");

      PARAM2 = getInitParameter("PARAM2");

    }

    ...

  }

 

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

public class LoginServlet extends HttpServlet {
private String SUCCESS_VIEW;
private String ERROR_VIEW;

@Override
public void init() throws ServletException {
super.init();
SUCCESS_VIEW = this.getInitParameter("SUCCESS");
ERROR_VIEW = this.getInitParameter("ERROR");
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
String passwd = request.getParameter("passwd");
if("caterpillar".equals(name) && "123456".equals(passwd)) {
request.getRequestDispatcher(SUCCESS_VIEW)
.forward(request, response);
}
else {
request.getRequestDispatcher(ERROR_VIEW)
.forward(request, response);
}
}

}

 

2.ServletContext

  ServletContext是整个web应用运行后的代表对象,可以通过ServletConfig的getServletContext()方法来取得。

  getRequestDispatcher()方法可以取得RequestDispatcher对象,使用时路径参数的指定必须以“/”开头,表示应用的根目录(context root)。

  getResourceAsStream()方法读取应用中某个文件,指定路径时必须以“/”开头。

  getResourcePaths()列出某个目录下的文件。指定路径时必须以“/”开头。

  针对整个web应用的初始化参数可以在web.xml中设置为<context-param>。读取这些参数要通过ServletContextListener。如果想知道web应用何时初始化何时结束销毁,可以实现ServletContextListener,并在web.xml中设置<listener>,调用实现类的contextInitialized()或contextDestroyed()方法。当容器调用contextInitialized()或contextDestroyed()方法时,会传入ServletContextEvent,其封装了ServletContext,可以通过getServletContext()取得,之后可以进行初始参数的读取了。

  在整个web应用生命周期内,servlet所需共享的数据可以设置为ServletContext属性。

  

    <context-param>
<param-name>AVATAR</param-name>
<param-value>/avatars</param-value>
</context-param>
<listener>
<listener-class>AvatarInitializer</listener-class>
</listener>

 

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AvatarInitializer implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String avatars = context.getInitParameter("AVATAR");
context.setAttribute("avatars", avatars);
}

public void contextDestroyed(ServletContextEvent sce) {
}
}

package cc.openhome;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AvatarServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AvatorServlet</title>");
out.println("</head>");
out.println("<body>");
String avatarDir = (String) getServletContext().getAttribute("avatars");
Iterator<String> avatars = getServletContext()
.getResourcePaths(avatarDir).iterator();
while (avatars.hasNext()) {
String avatar = avatars.next().replaceFirst("/", "");
out.println("<img src=\"" + avatar + "\">");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}



 

posted @ 2011-10-23 17:11  liuping  阅读(335)  评论(0编辑  收藏  举报