惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
W
WeLiveSecurity
人人都是产品经理
人人都是产品经理
S
Security Affairs
Google Online Security Blog
Google Online Security Blog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
博客园 - Franky
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
博客园 - 聂微东
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
The Cloudflare Blog
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
Troy Hunt's Blog
O
OpenAI News
博客园 - 司徒正美
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
I
Intezer
T
Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
H
Hacker News: Front Page
T
Tailwind CSS Blog
V
V2EX
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Archives - TechRepublic
Security Archives - TechRepublic
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News

博客园 - 倾听-静轩水月

Mysql优化笔记 Arthas使用 vue2.0和vue3.0同时使用 零基础尝试mybatis-plus读写分离 零基础尝试搭建docker和nacos环境 零基础尝试mysql主从复制 mybatis-plus 批量插入示例 找回windows应用商店 linux 开放端口 小白零基础在 Centos 7 中安装 mysql http转向https 内存不够导致编译报错:Information:java: java.lang.OutOfMemoryError: GC overhead limit exceeded docker常用命令 CentOS7使用命令行安装Oracle11GR2 使用Xshell连接VMware上的Linux虚拟机 mysql免安装版初次使用 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 javaweb学习--jsp
javaweb学习--javabean
倾听-静轩水月 · 2018-09-13 · via 博客园 - 倾听-静轩水月

阅读电子书《Java Web从入门到精通》密码:461c,学习JavaWeb基础知识

JavaBean类似于.net的实体类,但是规则上稍复杂一些,能实现的功能也多一些

一、介绍

1、规则

 1 package com.lyq.bean;
 2 
 3 /**
 4  * 新闻(乱码处理)
 5  * @author Administrator
 6  *
 7  */
 8 public class News {
 9     private String title;
10     private String content;
11 
12     public String getTitle() {
13         return this.title;
14     }
15 
16     public void setTitle(String title) {
17         this.title = title;
18     }
19 
20     public String getContent() {
21         return this.content;
22     }
23 
24     public void setContent(String content) {
25         this.content = content;
26     }
27 }

View Code

可无构造函数,每个属性都是私有,使用setXX赋值getXX取值。若有默认值,可自行添加构造函数处理

2、使用

在jsp页面中使用以下代码实例化使用 

1 <jsp:useBean id="自定义id" class="javabean类的全路径"></jsp:useBean>

View Code

以下代码赋值

1 <jsp:setProperty property="属性名" name="自定义的javabean的Id" />

View Code

 以下代码取值

1 <jsp:getProperty property="属性值" name="自定义的javabean的id" />

View Code

View Code

 完成代码为

1 <form action="news_release.jsp" method="post">
2         <b>新闻发布</b><br />
3     标题:<input name="title" id="title" /><br />
4     内容:<textarea name="content" id="content" rows="8" cols="40"></textarea><br />
5     <input type="submit" value="发布" />
6 </form>

View Code

1 <jsp:useBean id="news" class="com.lyq.bean.News"></jsp:useBean>
2 <jsp:useBean id="encoding" class="com.lyq.bean.CharactorEncoding"></jsp:useBean>
3 <jsp:setProperty property="*" name="news" />
4 <div><%= encoding.toString(news.getTitle()) %></div><br />
5 <div><%= encoding.toString(news.getContent()) %></div><br />

View Code

 1 package com.lyq.bean;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 
 5 /**
 6  * 乱码处理类
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class CharactorEncoding {
12     /**
13      * 构造方法
14      */
15     public CharactorEncoding() {
16 
17     }
18 
19     /**
20      * 对字符串进行转码处理
21      * 
22      * @param str
23      *            要转码的字符串
24      * @return 编码后的字符串
25      */
26     public String toString(String str) {
27         String text = "";
28         if (str == null || "".equals(str))
29             return text;
30         try{
31             text = new String(str.getBytes("ISO-8859-1"), "UTF-8");
32         }catch(UnsupportedEncodingException e){
33             e.printStackTrace();
34         }
35         
36         return text;
37     }
38 }

View Code

 二、使用案例

1、邮箱验证

  jsp页面

1 <form action="email_result.jsp" method="post">
2 邮箱格式验证<br />
3 邮箱地址:<input type="text" name="mailAdd" id="mailAdd" /><br />
4 <input type="submit" value="验证" />
5 </form>

View Code

  jsp验证结果页面

 1 <body>
 2     <div align="center">
 3         <%
 4             String mailAdd = request.getParameter("mailAdd");
 5             Email email = new Email(mailAdd);
 6             if (email.isEmail()) {
 7                 out.print(mailAdd + " <br />是一个标准的邮箱地址!<br />");
 8             } else {
 9                 out.print(mailAdd + " <br />不是一个标准的邮箱地址!<br />");
10             }
11         %>
12         <a href="email.jsp">返回</a>
13     </div>
14 </body>

View Code

javabean代码

 1 package com.lyq.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 邮箱类
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class Email implements Serializable {
12     /**
13      * serialVersionUID值
14      */
15     private static final long serialVersionUID = 1L;
16     /**
17      * Email地址
18      */
19     private String mailAdd;
20     /**
21      * 是否是一个标准的Email地址
22      */
23     private boolean email;
24 
25     /**
26      * 默认无参的构造函数
27      */
28     public Email() {
29 
30     }
31 
32     /**
33      * 构造方法
34      * 
35      * @param mailAdd
36      *            Email地址
37      * @return
38      */
39     public Email(String mailAdd) {
40         this.mailAdd = mailAdd;
41     }
42 
43     /**
44      * 是否是一个标准的Email地址
45      * 
46      * @return 布尔值
47      */
48     public boolean isEmail() {
49         String regex = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
50         if (mailAdd.matches(regex)) {
51             email = true;
52         }
53         return email;
54     }
55 
56     public String getMailAdd() {
57         return mailAdd;
58     }
59 
60     public void setMailAdd(String mailAdd) {
61         this.mailAdd = mailAdd;
62     }
63 
64 }

View Code

 2、日期显示

jsp页面

 1 <style type="text/css">
 2 #clock {
 3     width: 420px;
 4     height: 80px;
 5     background: #E0E0E0;
 6     font-size: 25px;
 7     font-weight: bold;
 8     border: solid 5px orange;
 9     padding: 20px;
10 }
11 
12 #week {
13     padding-top: 15px;
14     color: #0080FF;
15 }
16 </style>
17 <meta http-equiv="Refresh" content="1"><%-- 页面1秒刷新一次,显示出时间效果 --%>
18 </head>
19 <body>
20     <jsp:useBean id="date" class="com.lyq.bean.DateBean"
21         scope="application"></jsp:useBean>
22     <center>
23         <div id="clock">
24             <div id="time">
25                 <jsp:getProperty property="dateTime" name="date" />
26             </div>
27             <div id="week">
28                 <jsp:getProperty property="week" name="date" />
29             </div>
30         </div>
31     </center>
32 </body>

View Code

javabean代码

 1 package com.lyq.bean;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 /**
 8  * 日期处理类
 9  * @author Administrator
10  *
11  */
12 public class DateBean {
13     private String dateTime;
14     private String week;
15     private Calendar calendar = Calendar.getInstance();
16 
17     public String getDateTime() {
18         Date currDate = Calendar.getInstance().getTime();
19         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
20         this.dateTime = sdf.format(currDate);
21         return this.dateTime;
22     }
23 
24     public String getWeek() {
25         String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
26         int index = calendar.get(Calendar.DAY_OF_WEEK); // 获取一星期的某天
27         this.week = weeks[index - 1];
28         return this.week;
29     }
30 }

View Code

 3、数组/字符串处理

jsp页面

 1 <form action="paper_reg.jsp" method="post">
 2         <div>
 3             <h1>调查问卷</h1>
 4             <hr />
 5             <ul>
 6                 <li>你经常用哪些编程语言开发程序:</li>
 7                 <li><input type="checkbox" name="languages" value="JAVA" id="languages0" /><label for="languages0">JAVA</label>
 8                     <input type="checkbox" name="languages" value="PHP" id="languages1" /><label for="languages1">PHP</label> 
 9                     <input type="checkbox" name="languages" value=".NET" id="languages2" /><label for="languages2">.NET</label>
10                     <input type="checkbox" name="languages" value="VC++" id="languages3" /><label for="languages3">VC++</label>
11                     </li>
12             </ul>
13             <ul>
14                 <li>你目前掌握的技术:</li>
15                 <li><input type="checkbox" name="technics" value="HTML" />HTML
16                     <input type="checkbox" name="technics" value="JAVABEAN" />JAVABEAN 
17                     <input type="checkbox" name="technics" value="JSP" />JSP 
18                     <input type="checkbox" name="technics" value="SERVLET" />SERVLET</li>
19             </ul>
20             <ul>
21                 <li>在学习中哪一部分感觉有困难:</li>
22                 <li><input type="checkbox" name="parts" value="JSP" />JSP
23                     <input type="checkbox" name="parts" value="STRUTS" />"STRUTS"
24             </ul>
25             <input type="submit" value="提交" />
26         </div>
27     </form>

View Code

 1 <body>
 2     <jsp:useBean id="paper" class="com.lyq.bean.Paper"></jsp:useBean>
 3     <jsp:useBean id="convert" class="com.lyq.bean.Convert"></jsp:useBean>
 4     <jsp:setProperty property="*" name="paper" />
 5     <div>
 6         <h1>调查结果</h1>
 7         <hr />
 8         <li>你经常使用的编程语言:<%= convert.arr2Str(paper.getLanguages()) %></li>
 9         <li>你目前掌握的技术:<%= convert.arr2Str(paper.getTechnics()) %></li>
10         <li>在学习中哪一部分感觉有困难:<%= convert.arr2Str(paper.getParts()) %></li>
11     </div>
12 </body>

View Code

javabean代码

 1 package com.lyq.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 数组转字符串
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class Paper implements Serializable {
12     private static final long serialVersionUID = 1L;
13     /**
14      * 语言
15      */
16     private String[] languages;
17     /**
18      * 技术
19      */
20     private String[] technics;
21     /**
22      * 难点
23      */
24     private String[] parts;
25 
26     public Paper() {
27 
28     }
29 
30     public String[] getLanguages() {
31         return this.languages;
32     }
33 
34     public void setLanguages(String[] languages) {
35         this.languages = languages;
36     }
37 
38     public String[] getTechnics() {
39         return this.technics;
40     }
41 
42     public void setTechnics(String[] technics) {
43         this.technics = technics;
44     }
45 
46     public String[] getParts() {
47         return this.parts;
48     }
49 
50     public void setParts(String[] parts) {
51         this.parts = parts;
52     }
53 }

View Code

 1 package com.lyq.bean;
 2 
 3 public class Convert {
 4     /**
 5      * 将数组转成字符串
 6      * 
 7      * @param arr
 8      *            数组
 9      * @return 字符串
10      */
11     public String arr2Str(String[] arr) {
12         StringBuffer sb = new StringBuffer();  //StringBuffer是可变长的对象,比使用String拼接效率更高
13         if (arr == null || arr.length == 0)
14             return sb.toString();
15         for (String s : arr) {
16             sb.append(s);
17             sb.append(",");
18         }
19         if (sb.length() > 0)
20             sb = sb.deleteCharAt(sb.length() - 1);
21         return sb.toString();
22     }
23 }

View Code

4、不同方法接收页面参数

jsp页面

1 <form action="person_reg.jsp" method="post">
2 <b>添加用户信息</b><br />
3 姓名:<input type="text" id="username" name="username" /><br />
4 年龄:<input type="text" id="age" name="age" /><br />
5 性别:<input type="text" id="sex" name="sex" /><br />
6 住址:<input type="text" id="add" name="add" /><br />
7 <input type="submit" value="提交" />
8 </form>

View Code

 1 <body>
 2     <%
 3         request.setCharacterEncoding("UTF-8"); //防止中文乱码
 4     %>
 5     <jsp:useBean id="person" class="com.lyq.bean.Person" scope="page">
 6         <jsp:setProperty name="person" property="*" /><%-- 使用*接收所有参数,使用这种方式要求表单中的属性名与JavaBean的属性名一致 --%>
 7         <jsp:setProperty name="person" property="name" param="username" /><%--如果表单中的属性名(即参数)与JavaBean的属性名一致,则用param对其赋值 --%>
 8     </jsp:useBean>
 9     <div>
10         <ul>
11             <li>姓名:<jsp:getProperty property="name" name="person" /></li>
12             <li>年龄:<jsp:getProperty property="age" name="person" /></li>
13             <li>性别:<jsp:getProperty property="sex" name="person" /></li>
14             <li>住址:<jsp:getProperty property="add" name="person" /></li>
15         </ul>
16     </div>
17 </body>

View Code

javabean代码

 1 package com.lyq.bean;
 2 
 3 /**
 4  * 人员信息
 5  * @author Administrator
 6  *
 7  */
 8 public class Person {
 9     /**
10      * 姓名
11      */
12     private String name;
13     /**
14      * 年龄
15      */
16     private int age;
17     /**
18      * 性别
19      */
20     private String sex;
21     /**
22      * 住址
23      */
24     private String add;
25 
26     public String getName() {
27         return this.name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public int getAge() {
35         return this.age;
36     }
37 
38     public void setAge(int age) {
39         this.age = age;
40     }
41 
42     public String getSex() {
43         return this.sex;
44     }
45 
46     public void setSex(String sex) {
47         this.sex = sex;
48     }
49 
50     public String getAdd() {
51         return this.add;
52     }
53 
54     public void setAdd(String add) {
55         this.add = add;
56     }
57 }

View Code