Servlet------>ServletConfig和ServletContext

原理图:

之一--------->servletConfig

有些时候,有些参数不适合写死,而且初始化servlet要用,可以通过这个头来调用servletConfig

例如:serlet数据库的配置信息,servlet采用什么码表(utf-8),servlet配置文件是哪个

在web.xml中修改成如下:

在servlet中获取:

实际开发是这样写的:

 如果配置了多个init-param:

如何获得呢?

 应用的例子:

struts就是一个大大的servlet,所以他的web.xml中是这样的:通过servletconfig来配置要读取的xml

 之一--------->servletContext(context域对象)

首先要了解这个servletContext代表当前web应用

这个对象意味着这个web应用下的servlet可以通过他来共享信息

比如说servlet1用setAttibute存东西

servlet2用getAttibute取东西

还有很多方法可以去网上看文档,记住这个对象的含义最重要

如何调用:

ServletConfig和这个servletContext是兄弟,所以可以这样ServletConfig.getServletContext

package cn.itcaste.web.servlet;

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

/**
 * Servlet implementation class ServletDemo6
 */
@WebServlet("/ServletDemo6")
public class ServletDemo6 extends HttpServlet {
	//ServletContext事例
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到servletcontext方式一
		this.getServletConfig().getServletContext();
		//得到servletcontext方式二
		this.getServletContext();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}

 servletcontext细节一实现数据共享demo

启动tomcat先访问7,把data注入然后再取出来

package cn.itcaste.web.servlet;

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

/**
 * Servlet implementation class ServletDemo7
 */
@WebServlet("/ServletDemo7")
public class ServletDemo7 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String	data="aaaa";
	this.getServletContext().setAttribute("data1", data);
	}

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

}
package cn.itcaste.web.servlet;

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

/**
 * Servlet implementation class ServletDemo8
 */
@WebServlet("/ServletDemo8")
public class ServletDemo8 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String value=(String) this.getServletContext().getAttribute("data1");
	System.out.println(value);
	
	}

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

}

 结果:控制台输出aaaa

 servletcontext细节二:

在web.xml配置好web应用的初始化参数,加载web 应用会自动封装到servletcontext对象里

 servletcontext细节三:servlet的转发

首先转发的含义是,你找我借钱,我没有,我去找其他人帮你(相当于发一次请求)

重定向是,你找我借钱,我没有,我告诉你谁有,你自己去找她(相当于发两次请求)

 先做一个实例感受一下什么是转发,但是数据是不可以通过servletcontext传递的!!!!,servlet把数据准发给jsp,jsp来显示并且处理

ServletDemo10:
package cn.itcaste.web.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletDemo10
 */
@WebServlet("/ServletDemo10")
public class ServletDemo10 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String data="aaaa";
		this.getServletContext().setAttribute("data", data);
		
		RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");
		rd.forward(request,response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
<%
	String data=(String) application.getAttribute("data");//application就是serletcontext!
	out.write(data);
%> 
</h1>
</body>
</html>

 demo结果:

 servletcontext细节四:用servletcontext读取properties文件-------1)

重点在于:InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/kms.properties");//获取properties内容。      这个地址是编译后的地址src文件夹就会消失,变成在WEB-INF文件夹中的classes里!

 properties位置的几种可能:

/WEB-INF/classes/kms.properties--------       》

/WEB-INF/classes/cn/itcaste/kms.properties------>

/kms.properties------------------------------------------>

  servletcontext细节四:用servletcontext读取properties文件-------2)

传统的io流属于相对路径,是相对于java虚拟机,目前也就是tomcat里的bin文件夹,所以在用之前要取绝对路径(这个方法的好处在于可以取到资源名字)

既往不恋,未来不迎,当下不杂
原文地址:https://www.cnblogs.com/SnowingYXY/p/6611950.html