给我们的web应用配置初始化参数如下:
获得参数的java代码如下:
package com.yd.serlvlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 ReadAttribute
*/
@WebServlet("/ReadAttribute")
public class ReadAttribute extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ReadAttribute() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获得上下文对象获得设置的属性
ServletContext servletContext=this.getServletConfig().getServletContext();
String name=(String) servletContext.getAttribute("name");
//显示到网页上
PrintWriter out=response.getWriter();
out.println("没有移除属性之前name属性的值是:"+name);
//移除name属性
servletContext.removeAttribute("name");
String name2=(String) servletContext.getAttribute("name");
out.println("移除属性之后name属性的值是:"+name2);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(3)实现servlet转发,通过ServletContext可以实现页面的转发,和我们的request.getRequestDispatcher("资源名").forward(request,response);的用法一样。可以共享request对象,也是在服务器中发生页面跳转,只能访问该web应用下的资源。
java代码如下:
package com.yd.serlvlet;
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 Forward
*/
@WebServlet("/Forward")
public class Forward extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Forward() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//直接转发
request.setCharacterEncoding("utf-8");
//给request对象添加属性
request.setAttribute("age", 18);
//通过servletcontext实现转发
this.getServletConfig().getServletContext().getRequestDispatcher("/GetInitParam").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.yd.serlvlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 GetInitParam
*/
@WebServlet("/GetInitParam")
public class GetInitParam extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetInitParam() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获得参数
ServletContext servletContext=this.getServletContext();
String name=servletContext.getInitParameter("name");
PrintWriter out=response.getWriter();
//获得请求对象的属性
Integer age=(Integer) request.getAttribute("age");
out.println("初始化参数name的值:"+name+"request的属性age:"+age);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行结果如下:
(4)因为之前我们就说过servletContext是用来管理整个web应用的,所以可以通过servletContext来读取web应用下的资源文件。但是这里需要注意的是读取的web应用资源只能是webcontent下的资源不能是src目录下的资源。要想读取src下的资源需要通过类加载器。并且获得src资源时一定要注意资源地址包含在哪个包下,资源之间用/隔开。否则就不能正确的访问到src下的文件。
java代码如下:
package com.yd.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.spi.orbutil.fsm.Input;
/**
* Servlet implementation class GetResource
*/
@WebServlet("/GetResource")
public class GetResource extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetResource() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应对象
response.setContentType("text/html;charset=utf-8");
//首先获得上下文对象
ServletContext servletContext=this.getServletContext();
//以流的形式获得资源
InputStream is=servletContext.getResourceAsStream("/properties/count.properties");
//将流作为参数放大属性对象中
Properties properties=new Properties();
properties.load(is);
//获得属性对象中的键值
PrintWriter out=response.getWriter();
out.println("获得属性对象中的值username:"+properties.getProperty("count")+"
");
//如果要获得src下资源需要使用类加载器
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("/com/yd/serlvlet/dbinfo.properties");
Properties properties2=new Properties();
properties2.load(inputStream);
System.out.println(properties2);
out.println("src获得属性对象中的值username:"+properties2.getProperty("username"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
文件内容截图如下:
运行结果如图:
(5)获得资源在服务器的绝对路径。这个对于项目部署的服务器中要找到一个资源是很有用的。
java代码如下:
package com.yd.serlvlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletContext;
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 GetRealPath
*/
@WebServlet("/GetRealPath")
public class GetRealPath extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetRealPath() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应对象的响应文件类型
response.setContentType("text/html;charset=utf-8");
//获取上下文对象
ServletContext servletContext=this.getServletContext();
//获得资源在服务器的位置
String path=servletContext.getRealPath("/image/context初始化参数.png");
//获得资源的url
URL url=servletContext.getResource("/image/context初始化参数.png");
PrintWriter out=response.getWriter();
out.println("服务器位置path="+path+"
");
out.println("url="+url.getPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行结果如下: