码迷,mamicode.com
首页 > 编程语言 > 详细

(四)springmvc之获取servlet原生对象

时间:2017-11-02 18:13:55      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:int   this   value   pack   http   lan   html   dex   接口   

一、使用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;
    }

}

 

(四)springmvc之获取servlet原生对象

标签:int   this   value   pack   http   lan   html   dex   接口   

原文地址:http://www.cnblogs.com/shyroke/p/7773250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!