javabean属性应用的简单例子

关键词:

传递数据时出现中文乱码的解决办法:在获取值得页面写上:<%request.setCharacterEncoding("UTF-8")%>

<jsp:useBean>

<jsp:setProperty>

<jsp:getProperty>

一,javabean类:

 1 package com.lyq.bean;
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6     private String sex;
 7     private String add;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public int getAge() {
15         return age;
16     }
17     public void setAge(int age) {
18         this.age = age;
19     }
20     public String getSex() {
21         return sex;
22     }
23     public void setSex(String sex) {
24         this.sex = sex;
25     }
26     public String getAdd() {
27         return add;
28     }
29     public void setAdd(String add) {
30         this.add = add;
31     }
32     
33 }

二,初始页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'personindex.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <form action="personreg.jsp" method="post">
27     <table align="center" border="1" width="400" height="200">
28     <tr>
29         <td align="center" colspan="2">添加用户信息</td>
30     </tr>
31     <tr>
32         <td align="center">姓名:</td>
33         <td align="center"><input type="text" name="name"/></td>
34     </tr>
35     <tr>
36         <td align="center">年龄:</td>
37         <td align="center"><input type="text" name="age"/></td>
38     </tr>
39     <tr>
40         <td align="center">性别:</td>
41         <td align="center"><input type="text" name="sex"/></td>
42     </tr>
43     <tr>
44         <td align="center">住址:</td>
45         <td align="center"><input type="text" name="add"/></td>
46     </tr>
47     <tr>
48         <td colspan="2" align="center"><input type="submit" value="提交"/></td>
49     </tr>
50     </table>
51     
52     
53     </form>
54 
55   </body>
56 </html>


三,接受结果页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'personreg.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26   <%request.setCharacterEncoding("UTF-8"); %>
27     <jsp:useBean id="person" class="com.lyq.bean.Person" scope="page">
28         <jsp:setProperty name="person" property="*"></jsp:setProperty>
29     </jsp:useBean>
30     <table align="center" width="400" border="1">
31         <tr>
32             <td align="center">姓名:</td>
33             <td><jsp:getProperty property="name" name="person"/></td>
34         </tr>
35         <tr>
36             <td align="center">年龄:</td>
37             <td><jsp:getProperty property="age" name="person"/></td>    
38         </tr>
39         <tr>
40             <td align="center">性别:</td>
41             <td><jsp:getProperty property="sex" name="person"/></td>
42         </tr>
43         <tr>
44             <td align="center">地址:</td>
45             <td><jsp:getProperty property="add" name="person"/></td>
46         </tr>
47     
48     </table>
49   </body>
50 </html>
原文地址:https://www.cnblogs.com/speaklessdomore/p/3676615.html