ServletContext的代碼
import java.io.File;
import java.io.IOException;
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 ContextDemo1
?*/
@WebServlet("/ContextDemo5")
public class ContextDemo5 extends HttpServlet {
?? ?private static final long serialVersionUID = 1L;
????? ?
??? /**
???? * @see HttpServlet#HttpServlet()
???? */
??? public ContextDemo5() {
??????? super();
??????? // TODO Auto-generated constructor stub
??? }
?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//獲取文件的服務器路徑
?? ??? ?String realPath = context.getRealPath("/b.txt");//訪問web目錄下的資源路徑
?? ??? ?System.out.println(realPath);
?? ??? ?File file=new File(realPath);
?? ??? ?String c = context.getRealPath("/WEB-INF/c.txt");//訪問WEB-INF目錄下的資源路徑
?? ??? ?System.out.println(c);
?? ??? ?String a = context.getRealPath("/WEB-INF/classes/a.txt");//訪問src目錄下的資源路徑
?? ??? ?System.out.println(a);
?? ?}
?? ?/**
?? ? * @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 cn.itcast.web.servletcontext;
import java.io.IOException;
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 ContextDemo1
?*/
@WebServlet("/ContextDemo4")
public class ContextDemo4 extends HttpServlet {
?? ?private static final long serialVersionUID = 1L;
????? ?
??? /**
???? * @see HttpServlet#HttpServlet()
???? */
??? public ContextDemo4() {
??????? super();
??????? // TODO Auto-generated constructor stub
??? }
?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//獲取數(shù)據(jù)
?? ??? ?Object msg = context.getAttribute("msg");
?? ??? ?System.out.println(msg);
?? ??? ?
?? ?}
?? ?/**
?? ? * @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 cn.itcast.web.servletcontext;
import java.io.IOException;
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 ContextDemo1
?*/
@WebServlet("/ContextDemo3")
public class ContextDemo3 extends HttpServlet {
?? ?private static final long serialVersionUID = 1L;
????? ?
??? /**
???? * @see HttpServlet#HttpServlet()
???? */
??? public ContextDemo3() {
??????? super();
??????? // TODO Auto-generated constructor stub
??? }
?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//設置數(shù)據(jù)
?? ??? ?context.setAttribute("msg", "haha");
?? ??? ?
?? ?}
?? ?/**
?? ? * @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);
?? ?}
}
標簽: