【求助】JSP表单提交时几种情况的差别

頑笑 頑笑 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了一个简单的JSP,测试页面提交,代码如下:

<html>
<head>
<base href="<%=basePath%>">

<title>My JSP '2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<%
request.setAttribute("attributeText", "attributeText");
%>
<body>
<%=request %>
<form action="CheckRequest?urlText=urlText" method="post">
<input type="text" name="formText" value="formText">
<input type="submit" value="提交">
</form>
</body>
</html>

然后在对应的servlet里面打印request中的值,代码如下:

System.out.println(request);
System.out.println(request.getParameter("urlText"));
System.out.println(request.getParameter("formText"));
System.out.println(request.getAttribute("attributeText"));

结果是:
org.apache.catalina.connector.RequestFacade@114382d
urlText
formText
null
这里的request和jsp页面中的request明显是同一个,可是为什么request.getAttribute("attributeText")的值是null呢?这是问题一。

还有第二个问题:如果将form的提交方式改为get方式,那么servlet中打印结果又是:
org.apache.catalina.connector.RequestFacade@114382d
null
formText
null
这又是为什么呢?

希望能有高手解答,我真的很想知道一些原理方面的东西,先谢谢了。
...全文
评论 21
收藏 已收藏
分享
返回顶部
頑笑
Web 开发 81,107 成员 341,720 内容