servlet配置及域对象

一:ServletConfig(servlet配置信息)

  • 配置信息需要web.xml进行配置

  • 是以键值对形式配置 key=value

  • 在Servlet初始化时配置

  • 注意:

  • 1.每个Servlet都一个属于自己的ServletConfig对象

  • 2.ServletConfig对象内部维护一个map集合

  • public class Demo001 extends HttpServlet {
    // 获取方式1
    // 声明成员变量来保存ServletConfig
    private ServletConfig config;
    // 初始化Servlet配置信息
    @Override
    public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
    super.init(config);
    // 接收参数中的配置对象
    this.config = config;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //fun1();
    // 通过父类中方法 直接获取ServletConfig对象
    ServletConfig config2 = this.getServletConfig();
    String value = config2.getInitParameter(“shaoye”);
    System.out.println(value);
    }

    private void fun1() {
    // 获取ServletConfig中的值
    String value = this.config.getInitParameter(“shaoye”);
    System.out.println(value);
    // 遍历ServletConfig中的值
    Enumeration names = this.config.getInitParameterNames();
    while (names.hasMoreElements()) {
    // 获取所有name
    String name = names.nextElement();
    System.out.println(name + " = "
    + this.config.getInitParameter(name));
    }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}

二:域对象

  • 在一定范围内 有效的对象

  • ServletContext(application域)

  • 作用范围(最大范围的域对象):

  • 当前工程都有效 并且整个工程有且只有一个ServletContext对象

  • 相当于一个单例对象

  • 注意:所有的域对象都有共同的特点 内部维护了一个map集合

  • 所有域对象共有的方法:

  • 1.setAttribute

  • 2.getAttribute

  • 3.removeAttribute

  • 域对象的作用

  • 1.存值取值

  • 2.进行单例传值

  • 3.可以获取全局配置信息 web.xml

  • 4.可以获取项目中所有资源在服务器上的绝对路径 getRealPath

  • 5.可以进行请求转发

  • public class Demo002 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取context域方式1
    // 可以通过ServletConfig对象获取
    ServletContext application = this.getServletConfig().getServletContext();
    // 存值
    application.setAttribute(“name”, “xiaobai”);
    System.out.println(“保存值”);
    // 取出全局配置信息
    String value = application.getInitParameter("xioahei ");
    System.out.println(value);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}
三:测试application取值
public class Demo003 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//fun1();
	ServletContext application = this.getServletContext();
	//  打印b的绝对路径
	String path2 = application.getRealPath("/WEB-INF/classes/com/lanou3g/b.properties");
	//  打印c的绝对路径
	String path3 = application.getRealPath("/WEB-INF/c.properties");
	System.out.println(path2);
	System.out.println(path3);
}

private void fun1() throws FileNotFoundException, IOException {
	//  获取Context域方式2
	ServletContext application = this.getServletContext();
	//  获取服务器上的真实路径
	//  参数:使用服务器上相对于项目的相对路径
	//      / 表示服务器上项目名后面的斜杠
	String path1 = application.getRealPath("/WEB-INF/classes/a.properties");
	System.out.println(path1);
	//  读取文件打印value
	FileInputStream fis = new FileInputStream(path1);
	Properties properties = new Properties();
	properties.load(fis);
	System.out.println(properties.getProperty("key"));
	fis.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doGet(request, response);
}

}
获取请求转发器
参数: 转发的路径
注意:请求转发 只能转发站内的路径
并且传入的地址 相对于工程
http://localhost:8080/sh-web-02/demo07
请求转发注意
1.请求转发用户只发送了一次请求
2.网址没有发生变化(用户并不知道内部你怎么操作的)
3.只能转发站内

四:响应(response 响应回浏览器(用户))

  • 响应行

  • 响应状态码

  • 200(成功) 302(重定向) http协议1.1

  • 响应头

  • 告诉浏览器 要做什么

  • 告诉浏览器 要下载

  • 告诉浏览器 要什么编码格式

  • 响应体

  • 响应的内容
    public class Demo004 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //tomcat 默认编码格式 iso-8859-1
    response.setCharacterEncoding(“UTF-8”);
    //利用设置响应头 告诉浏览器 以什么编码格式来解析响应
    response.setHeader(“Content-type”, “test/html;charset=UTF-8”);
    //相当于上面两句二合一
    response.setContentType(“test/html;charset=UTF-8”);
    //利用response获取 字符流 和字节流
    PrintWriter writer = response.getWriter();
    writer.write(“wang”);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}
五:下载图片
public class Demo005 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 //获取图片在服务器上的真实路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/哈哈.png");
//通过file 类  来获取文件名
	File file=new File(path);
	String name = file.getName();
	System.out.println(name);
 //设置图片名的编码格式iso-8859-1
name =new String(name.getBytes(), "iso-8859-1");
//通过设置响应头  来告诉浏览器  给资源   供下载
//content-disposition attachment;filename=图片名
response.setHeader("content-disposition", "attachment;filename="+name);
//设置下载内容的格式(去web.xml中查找资源后缀的格式)
response.setHeader("Content-type", "image/png");


 //使用字节流读取图片
	FileInputStream fis=new FileInputStream(path);
//使用响应中的字节流 将图片写回浏览器
	ServletOutputStream sos = response.getOutputStream();
	byte[] b=new byte[1024];
	int len=0;
	while ((len=fis.read(b))!=-1) {
		//将图片写回浏览器
		sos.write(b, 0, len);
	}
	fis.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doGet(request, response);
}

}
通过响应response 请求重定向
可以进行站内重定向 相对于8080后的斜杠(需要带上工程名)
可以进行站外重定向
添加重定向的状态码
注意 1.重定向 发送两次请求(网址变了)
2.会执行完第一次请求的方法 在进行第二次请求

猜你喜欢

转载自blog.csdn.net/chenyuanshengboke/article/details/82989400