1,pubic void setAttribute(String name,*Object value*):向域中存储数据,指定名称
2,public *Object* getAttribute(String name):获取域对象中指定name的值
3,public void removeAttribute(String name):删除域对象中指定name的值
生命周期:
servletContext的作用:
<body>
<a href="${pageContext.request.contextPath}/ContextServlet">存储数据</a>
<a href="${pageContext.request.contextPath}/Context1Servlet">获取数据</a>
<a href="${pageContext.request.contextPath}/Context2Servlet">删除数据</a>
</body>
step2:
创建ContextServlet:
@WebServlet("/ContextServlet")
public class ContextServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//告诉浏览器响应的数据类型
response.setContentType("text/html;charset=utf-8");
//获取servletContext域对象,方法是从父类中继承过来的
ServletContext sc = getServletContext();
//存放数据到域对象中
sc.setAttribute("wp","666");
response.getWriter().write("存储成功!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
step3:
创建Context1Servlet:
@WebServlet("/Context1Servlet")
public class Context1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//告诉浏览器响应的数据类型
response.setContentType("text/html;charset=utf-8");
//获取servletContext域对象,方法是从父类中继承过来的
ServletContext sc = getServletContext();
//获取servletContext域对象中的数据
String wp = (String) sc.getAttribute("wp");
response.getWriter().write("获取到的数据是:"+wp);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
step4:
创建Context2Servlet:
@WebServlet("/Context2Servlet")
public class Context2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//告诉浏览器响应的数据类型
response.setContentType("text/html;charset=utf-8");
//获取servletContext域对象,方法是从父类中继承过来的
ServletContext sc = getServletContext();
//删除域对象中name为“wp”的数据
sc.removeAttribute("wp");
response.getWriter().write("删除成功!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
结果:当点击存储数据时将数据存入servletContext域对象中,此时点击获取数据,显示数据666,当点击删除数据之后,再点击获取数据显示null。
<context-param>
<param-name>wp</param-name>
<param-value>66</param-value>
</context-param>
<context-param>
<param-name>wpp</param-name>
<param-value>666</param-value>
</context-param>
step2:
创建Deno1Servlet:
@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取servletContext域对象
ServletContext sc = getServletContext();
//获取wp的取值
String wp = sc.getInitParameter("wp");
System.out.println(wp);
//获取所有的配置参数
Enumeration<String> ipn = sc.getInitParameterNames();
//遍历
while (ipn.hasMoreElements()){
String s = ipn.nextElement();
System.out.println(sc.getInitParameter(s));
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
输出结果:
66
666
66
@WebServlet("/Demo2Servlet")
public class Demo2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取servletContext域对象
ServletContext sc = getServletContext();
//获取真实路径
String realPath = sc.getRealPath("/images/5.jsp");
System.out.println("真实路径:"+realPath);
//输出结果:真实路径:E:\idea工作区间\javaweb129\out\artifacts\MyTest_war_exploded\images\5.jpg
//getrealpath()方法不管路径存不存在,都可以获取到真实路径
//需要注意的是,此图片的路径在web下的images文件夹下
InputStream is = sc.getResourceAsStream("/images/5.jpg");
ServletOutputStream os = response.getOutputStream();
int len;
byte[] b=new byte[1024];
while ((len=is.read(b))!=-1){
os.write(b,0,len);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
生命周期:
//此代码在tomcat的conf目录的web.xml中
<session-config>
<session-timeout>30</session-timeout>
</session-config>
服务器关闭时:服务器关闭时,我们可以通过钝化(将session数据保存到硬盘中)和活化(将session数据回复到session中)
作用范围:一次会话中,涉及的servlet和jsp都可以使用
生命周期:
HttpServletRequest的作用:
@WebServlet("/Demo3Servlet")
public class Demo3Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向httpservletrequest域对象中存储数据
request.setAttribute("data","一对数据");
//转发到Demo4Servlet
request.getRequestDispatcher("Demo4Servlet").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
step2:
在此域对象中获取到了转发来的数据
@WebServlet("/Demo4Servlet")
public class Demo4Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取域中的数据
String data = (String) request.getAttribute("data");
//输出:一对数据
System.out.println(data);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/Demo5Servlet")
public class Demo5Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod();
System.out.println("浏览器端的请求方式是:"+method);
String requestURI = request.getRequestURI();
System.out.println("浏览器端的uri地址是:"+requestURI);
StringBuffer requestURL = request.getRequestURL();
System.out.println("浏览器端的url地址是:"+requestURL);
String protocol = request.getProtocol();
System.out.println("浏览器端的协议及其版本号是:"+protocol);
String contextPath = request.getContextPath();
System.out.println("虚拟地址是:"+contextPath);
String remoteAddr = request.getRemoteAddr();
System.out.println("浏览器端的ip地址是:"+remoteAddr);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
结果:
浏览器端的请求方式是:GET
浏览器端的uri地址是:/mytest/Demo5Servlet
浏览器端的url地址是:http://localhost:8080/mytest/Demo5Servlet
浏览器端的协议及其版本号是:HTTP/1.1
虚拟地址是:/mytest
浏览器端的ip地址是:0:0:0:0:0:0:0:1
-获取请求消息头内容
常用方法:
1,public string getHeader(String name):获取指定name的消息头
2,public long getDateHeader(String name):获取取值为毫秒值的消息头
3,public int getIntHeader(String name):获取取值为整数类型的消息头
4,public enumeration getHeaderNames():获取所有的请求消息头
5,public enumeration getHeaders(String name):获取指定name的有多个值的消息头
-获取请求正文内容
注意!只有post方式才有请求正文,请求数据类型必须是application/x-www-form-urlencoded
常用方法:
1,public servletinputstream getInputstream():以字节输入流的形式获取正文
2,public bufferedreader getreader():以字符流的形式获取正文
3,public String getparameter(String name):获取指定name的参数值
4,public String[] getparametervalues(String name):获取指定name的参数值(多个值)
5,public enumeration
6,public map
生命周期:
创建:当对jsp的请求开始时创建
销毁:当响应结束后销毁
作用范围:整个页面(是四大域对象中作用范围最小的)
作用:
获取其他八大内置对象
getException()返回Exception。
getPage()返回Page。
getRequest()返回request。
getResponse()返回response。
getServletConfig()返回config。
getServletContext()返回application。
getSession()返回session。
getOut()返回out
作为域对象
可以在当前界面操作其他三大域对象
setAttribute(String key,String value,int 哪个域) :
getAttribute(String key,int 哪个域)
removeAttribute(String key,int 哪个域)
findAttribute(String key) :依次从jsp的四个域中从小到大(域的范围)挨个查找指定的key.若找到直接返回,终止查找,若没 有返回null