版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Servletconfig:在servlet的配置文件web.xml中,可以使用多个标签,作为servlet初始化的参数
配置是在相应的类中去配置的
package com.j2ee.servlet;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServerlt extends HttpServlet{
private ServletConfig sc;
public void init(ServletConfig config) throws ServletException{
this.sc=config;
//遍历init的所有内容
Enumeration<String> e=this.sc.getInitParameterNames();
while(e.hasMoreElements()) {
String name=e.nextElement();
String value=this.sc.getInitParameter(name);
System.err.println(name+"---"+value);
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//b)servlet的config初始化
if (userName==null || userName.equals("")) {
//从config中默认取值
userName=this.sc.getInitParameter("name");
password=this.sc.getInitParameter("password");
}else {
System.out.println("已经取到其他值了");
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2 Servletcontext
Servletcontext代表当前应用,web容器在启动的时候,会给每个应用都创建一个servletcontext对象,context的引用被config持有(维护),可以通过config.getContext的方式获取context对象,由于所有servlet都使用同一个context,通常用来做通讯使用,可称为context域
2.1 在配置中给整个web应用做配置
2.2 e) Context如何实现两个servlet的跳转功能?RequestDispatcher