创建servlet
配置
运行
获取ServletConfig对象
ServletConfig的使用
ServletContext
SomeServlet 代码:
package com.kay.servlets;
import java.io.IOException;
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;
public class SomeServlet 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对象
ServletContext sc = config.getServletContext();
// 获取所有初始化参数名
Enumeration<String> names = sc.getInitParameterNames();
while(names.hasMoreElements()) {
String name = names.nextElement();
String value = sc.getInitParameter(name);
System.out.println(name + " = " + value);
}
// 设置域属性
sc.setAttribute("email", "xxx@126.com");
sc.setAttribute("mobile", "1234567");
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
OtherServlet 代码:
package com.kay.servlets;
import java.io.IOException;
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;
public class OtherServlet 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对象
ServletContext sc = config.getServletContext();
// 获取域属性的值
String mobile = (String) sc.getAttribute("mobile");
System.out.println("mobile = " + mobile);
// 重置域属性的值
sc.setAttribute("mobile", "7654321");
// 删除域属性
sc.removeAttribute("email");
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
ThirdServlet 代码:
package com.kay.servlets;
import java.io.IOException;
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;
public class ThirdServlet 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对象
ServletContext sc = config.getServletContext();
// 获取域属性的值
String mobile = (String) sc.getAttribute("mobile");
String email = (String) sc.getAttribute("email");
System.out.println("mobile = " + mobile);
System.out.println("email = " + email);
String contextPath = sc.getContextPath();
System.out.println("contextPath = " + contextPath);
String realPath = sc.getRealPath("/images");
System.out.println("realPath = " + realPath);
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}