我试图访问JSP中的查询参数。直接使用request.getParameter( )
不起作用,但使用pageContext.request.getParameter( )
按预期工作。似乎隐含的request
对象不是直接可用的,尽管文档建议。
我可以如图所示解决这个问题,但想知道为什么这不能按预期工作。
我的JSP代码如下:
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test.jsp</title>
</head>
<body>
<jsp:text>param.username=${param.username}</jsp:text>
<br/>
<jsp:text>pageContext.request.getParameter("username")=${pageContext.request.getParameter("username")}</jsp:text>
<br/>
<jsp:text>request.getParameter("username")=${request.getParameter("username")}</jsp:text>
<br/>
<c:choose>
<c:when test="${empty request}">
<jsp:text>request is empty!</jsp:text>
</c:when>
<c:otherwise>
<jsp:text>request is NOT empty!</jsp:text>
</c:otherwise>
</c:choose>
<br/>
Using a scriptlet, request.getParameter("username")=<%= request.getParameter("username") %>
<br/>
Using a "jsp:expression" tag, request.getParameter("username")=<jsp:expression>request.getParameter("username")</jsp:expression>
<br/>
</body>
</html>
使用http://localhost:8080/jsp_fun/test.jsp?username=roy
。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test.jsp</title>
</head>
<body>
param.username=roy
<br/>
pageContext.request.getParameter("username")=roy
<br/>
request.getParameter("username")=
<br/>
request is empty!
<br/>
Using a scriptlet, request.getParameter("username")=roy
<br/>
Using a "jsp:expression" tag, request.getParameter("username")=roy
<br/>
</body>
</html>
如果仔细查看生成的HTML并将其与JSP进行比较,您可以看到request.getParameter( )
不起作用,pageContext.request.getParameter( )
起作用。同样,检查请求是否为空表示它是空的/ null,因此其余的结果是一致的(如果错误和烦人,恕我直言)。
作为参考,我的代码部署到Tomcat 8.0.32,我通过Maven使用以下JSTL库:
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
在Tomcat的work / Catalina目录中查看生成的java代码,我看到以下有趣的代码行,显示了引用请求对象的不同方式,具体取决于我的源JSP:
out.write(" param.username=");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param.username}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("\n");
out.write(" <br/>\n");
out.write(" pageContext.request.getParameter(\"username\")=");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.request.getParameter(\"username\")}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("\n");
out.write(" <br/>\n");
out.write(" request.getParameter(\"username\")=");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${request.getParameter(\"username\")}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("\n");
out.write(" <br/>\n");
out.write(" ");
if (_jspx_meth_c_005fchoose_005f0(_jspx_page_context))
return;
out.write("\n");
out.write(" <br/>\n");
out.write(" Using a scriptlet, request.getParameter(\"username\")=");
out.print( request.getParameter("username") );
out.write("\n");
out.write(" <br/>\n");
out.write(" Using a \"jsp:expression\" tag, request.getParameter(\"username\")=");
out.print(request.getParameter("username"));
有关为什么我无法按预期直接引用request
对象的任何想法?
答案 0 :(得分:0)
没关系 - 查看http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html#bnaij处的真实文档,请求对象不应该直接可用 - 它应该是pageContext对象的子级。我正在查看的教程说明请求对象可以直接访问是错误的。