JSP——useBean

在前面已介绍了include动作标志和forward跳转解决乱码的方法。在此介绍表单乱码问题,与前面的方法不同了。

//在表单处理页面写useBean。

留言板示例:有两个bean类,分别为:Word.java和myTools.java

在word.jsp页面代码:

<%@ page contentType="text/html;charset=gb2312"%>
<html>
 <body>
 <form action="doword.jsp">
 <table>
  <tr><th>留言者:</th><td><input type="text" name="author"/></tr>
  <tr><th>留言标题:</th><td><input type="text" name="title"/></td></tr>
  <tr><th>留言内容:</th><td><textarea rows="5" cols="30" name="content"></textarea></td></tr>
  <tr><td colspan="2"><input type="submit" value="提交"/></td></tr>
 </table>
 </form>
 </body>
</html>

doword.jsp代码:

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="cn.myMindView.bean.*" %>
<jsp:useBean id="myword" class="cn.myMindView.bean.Word" scope="request">
<jsp:setProperty  name="myword" property="*"/>
</jsp:useBean>
<jsp:forward page="show.jsp"></jsp:forward>

 

show.jsp代码:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="cn.myMindView.bean.*" %>
<jsp:useBean id="myword" class="cn.myMindView.bean.Word" scope="request"/>
<html>
 <body>
 <table>
  <tr><th>留言者:</th><td><%=myTools.toChinese(myword.getAuthor()) %></tr>
  <tr><th>留言标题:</th><td><%=myTools.toChinese(myword.getTitle()) %></td></tr>
  <tr><th>留言内容:</th><td><textarea rows="5" cols="30" readonly>
  <%=myTools.toChinese(myword.getContent()) %></textarea></td></tr>
 </table> 
 </body>
</html>

总结:useBean的scope范围只有都为request时才能在show页面中取到表单中的值。

<jsp:useBean>标志被执行时首先会在scope指定的范围内查找指定的bean实例。默认值为page,若该实例已存在,则引用这个实例。在上面留言板的例子里show页面现在reques范围内寻找myword这个实例,发现有这个实例,于是就直接使用myword.getName()方法。若没有实例,则会用new方法重新创建一个。

 

转载于:https://www.cnblogs.com/mymindview/p/3497474.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值