我的学习路线
——本次学习的简单说明
本次的学习分为九节
注:点击题目会跳转到其他博客链接
Day1:JSP的入门&环境的配置&Tomcat的注意点
Day2:简单项目的创建和基本的功能的实现(打war包,共享项目)
Day3:JSP基础语法
Day4:JSP 九大内置对象及四个作用域
Day5:JavaBean 组件
Day6:Servlet 开发
Day7:EL 表达式
Day8:Jsp 自定义标签
Day9:Jsp 标准标签库持续更新中:新的博客写完会加链接
下面是Day5的学习
Javabean组件引入
JavaBean是使用Java语言开发的一个可重用的组件,在JSP开发中可以使用JavaBean减少重复代码,使用JSP代码的开发更简单。
Student的代码
package model;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
jsp的代码如下
<%@page import="model.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student student=new Student();
student.setName("张三");
student.setAge(10);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
效果
jsp:useBean创建javabean
<jsp:useBean id=“实例化对象名称” scope=“保存范围” class=“类完整名称”/> Scope,一共有
page,request,session 和 application4 个属性范围,默认是 page;
代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="page" class="model.Student"></jsp:useBean>
<%
student.setName("张三");
student.setAge(10);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
效果
jsp:setProperty设置javabean属性值
<jsp:setProperty property=“属性名称” name=“实例化对象的名称” value=“属性值”
param=“参数名称”/> Property=”*” 自动匹配所有
先复习一下最原始的写法
首先创建一个Student.jsp用来得到一个表单,获取我们想要的属性元素
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="javaBean03.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
form表单,实现一个表格的信息录入,submit实现提交,这里其实就像是一个响应事件,javaBean03就是在事件响应之后跳转的页面
javaBean03的代码如下
<%@page import="model.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//解决乱码的问题
request.setCharacterEncoding("UTF-8");
//response是通过name属性来获得用户输入的text的
String name=request.getParameter("name");
String age=request.getParameter("age");
Student student=new Student();
student.setName(name);
student.setAge(Integer.parseInt(age));
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
效果
提交之后的效果
下面是把setProperty引入的效果,最终实现的功能的javaBean03的是一样的,这个下面的代码是javaBean04,很显然代码大大的减少了,注意基本模板的使用。
<%@page import="model.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//解决乱码的问题
request.setCharacterEncoding("UTF-8");
%>
<!-- 下面的这一行是定义对象 -->
<jsp:useBean id="student" scope="page" class="model.Student"/>
<!-- 设置属性,name是实例化对象的名称,就是上面的id -->
<jsp:setProperty property="*" name="student"/>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
jsp:getProperty获取javabean属性值
<jsp:getProperty property=“属性名称” name=“实例化对象的名称”/>
javabean的保存范围
Javabean 的保存范围有 page,request,session.application,默认是 page;
javabean 删除
Page 范围:pageContext.removeAttribute(“javaBean Name”); request
范围:request.removeAttribute(“javaBean Name”); session
范围:session.removeAttribute(“javaBean Name”); application
范围:application.removeAttribute(“javaBean Name”);
转载:https://blog.csdn.net/qq_43918130/article/details/102415429