首页 > 代码库 > 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}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。