一、使用DI注入的方式
<a href="<%=request.getContextPath()%>/servletObj_1">DI注入的方式</a>
controller
package com.controller.servletobj; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class ServletObj_1 { @Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response; @Autowired private HttpSession session; @Autowired private ServletContext servletContext; @RequestMapping(value = "/servletObj_1") public ModelAndView exuecte() throws Exception { response.setContentType("text/html"); String contextPath = request.getContextPath(); String sessionID = session.getId(); String indexPath = servletContext.getRealPath("/index.jsp"); PrintWriter out = response.getWriter(); out.println("contextPath = " + contextPath+"<br/><br/>"); out.println("sessionID = " + sessionID+"<br/><br/>"); out.println("indexPath = " + indexPath+"<br/><br/>"); out.flush(); out.close(); return null; } }
二、形参传入的方式:ServletContext不能通过形参的方式注入,
<a href="<%=request.getContextPath()%>/servletObj_2">形参传入的方式</a>
controller
package com.controller.servletobj; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class ServletObj_2 { @RequestMapping(value = "/servletObj_2") public ModelAndView exuecte(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { response.setContentType("text/html"); /** * ServletContext不能通过形参的方式注入, * * 通过其他对象来进行获取。 */ ServletContext servletContext = request.getServletContext(); String contextPath = request.getContextPath(); String sessionID = session.getId(); String indexPath = servletContext.getRealPath("/index.jsp"); PrintWriter out = response.getWriter(); out.println("contextPath = " + contextPath + "<br/><br/>"); out.println("sessionID = " + sessionID + "<br/><br/>"); out.println("indexPath = " + indexPath + "<br/><br/>"); out.flush(); out.close(); return null; } }
-
ServletContext不能通过形参的方式注入,
三、拦截器的方式:只能注入ServletContextAware和ServletConfigAware接口
<a href="<%=request.getContextPath()%>/servletObj_3">拦截器的方式</a>
controller
package com.controller.servletobj; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.ServletConfigAware; import org.springframework.web.context.ServletContextAware; import org.springframework.web.servlet.ModelAndView; /** * 通过拦截器来注入,只能注入ServletContextAware和ServletConfigAware接口 * */ @Controller public class ServletObj_3 implements ServletContextAware, ServletConfigAware { private ServletContext servletContext; private ServletConfig servletConfig; @Override public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } @Override public void setServletConfig(ServletConfig servletConfig) { this.servletConfig = servletConfig; } @RequestMapping(value = "/servletObj_3") public ModelAndView exuecte(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { response.setContentType("text/html"); String contextPath = request.getContextPath(); String sessionID = session.getId(); String indexPath = this.servletContext.getRealPath("/index.jsp"); PrintWriter out = response.getWriter(); out.println("contextPath = " + contextPath + "<br/><br/>"); out.println("sessionID = " + sessionID + "<br/><br/>"); out.println("indexPath = " + indexPath + "<br/><br/>"); out.flush(); out.close(); return null; } }
时间: 2024-10-18 00:50:19