三、通过ServletContext对象来获取web.xml文件中内容

1、该程序的作用:

1) 通过ServletContext对象来获取web.xml文件中内容
2) 在一个AServlet进行ServletContext属性的配置,在另外一个BServlet中进行读取
3) 修改欢迎页面的默认配置(默认配置在WEB-INF的跟目录下面)
4) 发生错误时如何进行统一的处理

 

AServlet类:

package com.bjpownernode.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import entity.User;

public class AServlet implements Servlet {
 private ServletConfig config;
 @Override
 public void init(ServletConfig config) throws ServletException {
  this.config=config;

 }

 @Override
 public ServletConfig getServletConfig() {
  return config;
 }

 @Override
 public void service(ServletRequest req, ServletResponse res)
   throws ServletException, IOException {
  ServletContext application=config.getServletContext();
  PrintWriter out=res.getWriter();
  out.print(application);
  out.print("<br>");
  Enumeration<String> names=application.getInitParameterNames();
  while(names.hasMoreElements()){
   String name=names.nextElement();
   String value=application.getInitParameter(name);
   out.print(name+":"+value);
   out.print("<br>");
  }
  String path=application.getRealPath("/index.html");
  out.println(path);
  User user=new User();
  user.setUserCode("12");
  user.setUserName("jason");
  //向ServletContext范围中存储User数据
  application.setAttribute("userObject", user);
  application.setAttribute("time",new Date());
 }

 @Override
 public String getServletInfo() {
  return null;
 }

 @Override
 public void destroy() {

 }

}

BServlet类

package com.bjpownernode.javaweb.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class BServlet implements Servlet {

 private ServletConfig config;
 @Override
 public void init(ServletConfig config) throws ServletException {
  this.config=config;
 }
 @Override
 public void service(ServletRequest req, ServletResponse res)
   throws ServletException, IOException {
  ServletContext application= config.getServletContext();
  //从ServletContext范围中读取数据
  Object obj1=application.getAttribute("userObject");
  Object obj2=application.getAttribute("time");
  res.setContentType("text/html");
  res.getWriter().print("--->"+obj1);
  res.getWriter().print("--->"+obj2);
 }
 @Override
 public ServletConfig getServletConfig() {
  // TODO Auto-generated method stub
  return config;
 }


 @Override
 public String getServletInfo() {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void destroy() {
  // TODO Auto-generated method stub

 }

}

User类

 

package entity;

 

public class User {
 private String userName;
 private String userCode;

 

 public String getUserName() {
  return userName;
 }

 

 public void setUserName(String userName) {
  this.userName = userName;
 }

 

 public String getUserCode() {
  return userCode;
 }

 

 public void setUserCode(String userCode) {
  this.userCode = userCode;
 }

 

 @Override
 public String toString() {
  return "User [userName=" + userName + ", userCode=" + userCode + "]";
 }

 

}

 

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试ServletContext接口</title>
</head>
<body>
 <a href="/prj-servlet05/a">AServlet</a>
 <a href="/prj-servlet05/b">BServlet</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
  <display-name>prj-servlet05</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- web.xml文件中配置上下文参数,这些信息被自动封装到ServletContext对象中 -->
  <context-param>
   <param-name>username</param-name>
   <param-value>admin</param-value>
  </context-param>
  <context-param>
   <param-name>password</param-name>
   <param-value>123</param-value>
  </context-param>
  <servlet>
   <servlet-name>a</servlet-name>
   <servlet-class>com.bjpownernode.javaweb.servlet.AServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>a</servlet-name>
   <url-pattern>/a</url-pattern>
  </servlet-mapping>
    <servlet>
   <servlet-name>b</servlet-name>
   <servlet-class>com.bjpownernode.javaweb.servlet.BServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>b</servlet-name>
   <url-pattern>/b</url-pattern>
  </servlet-mapping>
  <servlet>
   <servlet-name>test</servlet-name>
   <servlet-class>com.bjpownernode.javaweb.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>test</servlet-name>
   <url-pattern>/test01</url-pattern>
  </servlet-mapping>
  <error-page>
   <error-code>404</error-code>
   <location>/error/error.html</location>
  </error-page>
</web-app> 

error.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示</title>
</head>
<body>
 亲出错了哦,联系管理员吧!
</body>
</html>

 

posted @ 2017-01-15 14:19  爱笑的berg  阅读(292)  评论(0)    收藏  举报