⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0039.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>Servlet和JSP的通信</strong></big></p>

<p><span class=smallFont>
以下的代码片段演示了一个servlet,它例示(instantiates)一个bean并且用由浏览器发送的Form数据将它初始化。此后bean被加入request,而这一要求被送到JSP页面(Bean1.jsp),JSP通过request取得并做进一步处理。<br>
<br>
public void doPost (HttpServletRequest request,<br>
HttpServletResponse response) {<br>
<br>
try {<br>
govi.FormBean f = new govi.FormBean();<br>
String id = request.getParameter(&quot;id&quot;);<br>
f.setName(request.getParameter(&quot;name&quot;));<br>
f.setAddr(request.getParameter(&quot;addr&quot;));<br>
f.setAge(request.getParameter(&quot;age&quot;));<br>
//use the id to compute <br>
//additional bean properties like info <br>
//maybe perform a db query, etc.<br>
// . . .<br>
f.setPersonalizationInfo(info);<br>
request.setAttribute(&quot;fBean&quot;,f);<br>
getServletConfig().getServletContext().getRequestDispatcher<br>
(&quot;/jsp/Bean1.jsp&quot;).forward(request, response);<br>
} catch (Exception ex) {<br>
. . .<br>
}<br>
}<br>
<br>
在将它从通过useBean操作中的缺省的请求范围,即scope首次执行后,即可在JSP文件的Bean1.jsp处理fBean。 <br>
<br>
&lt;jsp:useBean id=&quot;fBean&quot; class=&quot;govi.FormBean&quot; scope=&quot;request&quot;/&gt;<br>
&lt;jsp:getProperty name=&quot;fBean&quot; property=&quot;name&quot; /&gt;<br>
&lt;jsp:getProperty name=&quot;fBean&quot; property=&quot;addr&quot; /&gt;<br>
&lt;jsp:getProperty name=&quot;fBean&quot; property=&quot;age&quot; /&gt;<br>
&lt;jsp:getProperty name=&quot;fBean&quot; property=&quot;personalizationInfo&quot; /&gt;&nbsp;

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -