博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(四)springmvc之获取servlet原生对象
阅读量:5887 次
发布时间:2019-06-19

本文共 4485 字,大约阅读时间需要 14 分钟。

一、使用DI注入的方式

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;@Controllerpublic 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+"
"); out.println("sessionID = " + sessionID+"
"); out.println("indexPath = " + indexPath+"
"); out.flush(); out.close(); return null; }}

 

二、形参传入的方式:ServletContext不能通过形参的方式注入,

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;@Controllerpublic 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 + "
"); out.println("sessionID = " + sessionID + "
"); out.println("indexPath = " + indexPath + "
"); out.flush(); out.close(); return null; }}
  • ServletContext不能通过形参的方式注入,

 

三、拦截器的方式:只能注入ServletContextAware和ServletConfigAware接口

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接口 * */@Controllerpublic 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 + "
"); out.println("sessionID = " + sessionID + "
"); out.println("indexPath = " + indexPath + "
"); out.flush(); out.close(); return null; }}

 

转载于:https://www.cnblogs.com/shyroke/p/7773250.html

你可能感兴趣的文章
在jsp中嵌入javascript代码执行对html的影响方式
查看>>
redhat安装opencv
查看>>
十进制与其他进制转换
查看>>
web端测试小知识
查看>>
8.30 牛客OI赛制测试赛1 F题 子序列
查看>>
.NET中<asp:MultiView>选项卡控件的用法
查看>>
为什么用bower 安装bootstrap而不用npm来安装?
查看>>
通过游戏来学习CSS的Flex布局
查看>>
Firefly加入OPEN AI LAB生态计划,推出AI开源主板
查看>>
递归函数实现方法
查看>>
[转]Windows7 64bit下配置Apache+PHP+MySQL
查看>>
CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问
查看>>
给trac的ticket添加提交时字段验证
查看>>
nodejs安装-配置
查看>>
Node.js学习-1
查看>>
今天你的应用崩溃了么?
查看>>
项目中的*签到*小功能!
查看>>
iOS 获取cell.accessoryView自定义视图以及点击事件
查看>>
java 考试试题
查看>>
[caffe(一)]使用caffe训练mnist数据集
查看>>