首页 > 代码库 > Java web 常见对象的取值方法整理

Java web 常见对象的取值方法整理

一、从request中取值:


1.取param:


servlet:  request.getParameter()
               request.getParameterValues()

jsp脚本:request.getParameter()

                  request.getParameterValues()

jstl/el:  ${param.name} 

             ${paramaValues.names[0]}



struts ognl:<s:property value=http://www.mamicode.com/"#parameters.ParamName[0]"/>
   


2.取attribute:


servlet: request.getAttribute();


jsp脚本:request.getAttribute();


jstl/el:${attrName}(依次查找request、session和application中的属性,找到为止)   
          ${requestScope.attrName}


struts ognl:<s:property value=http://www.mamicode.com/"#attrName">(依次查找request、session和application中的属性,找到为止)
            <s:property value=http://www.mamicode.com/"#request.attrName">


二、从servletContext(application)中取值:


1.取InitParam:


servlet: getServletContext().getInitParam("parmaName");
              getServletConfig().getServletContext.getInitParam("paramName");
     (getServletConfig().getInitParam("parmaName")和getInitParam("parmaName")是取得当前servlet中的initParam);


jsp脚本:application.getInitParam("paramName");
         config.getServletContext().getInitParam("paramName");
         config.getInitParam("paramName");


jstl/el: ${initParam.paramName}



2.取attribute:



servlet: getServletContext().getAttribute("attrName");
              getServletConfig().getServletContext.getAttribute("attrName");


jsp脚本:application.getAttribute("attrName");


jstl/el:${attrName}(依次查找request、session和application中的属性,找到为止) 
  ${applicationScope.attrName}
struts ognl:<s:property value=http://www.mamicode.com/"#attrName">(依次查找request、session和application中的属性,找到为止)
            <s:property value=http://www.mamicode.com/"#application.attrName">


三、从session中取值:


取attribute:



servlet: request.getSession().getAttribute("attrName");


jsp脚本:session.getAttribute("attrName");


jstl/el: ${attrName}(依次查找request、session和application中的属性,找到为止)
           ${sessionScope.attrName}


struts ognl:<s:property value=http://www.mamicode.com/"#attrName">(依次查找request、session和application中的属性,找到为止)
            <s:property value=http://www.mamicode.com/"#session.attrName">


四、从page中取值:


取attribute:


jsp脚本:pageContext.getAttribute(name);


jstl/el: ${pageContext.attrName}



五、从cookie中取值:


servlet: request.getCookies();


jsp脚本:request.getCookies();


jstl/el: ${cookie.cookieName.value}