文件结构

  • 概述
  • 在jsp中使用
  • src里面写JavaBean的.class文件
  • WebContent写.jsp页面
  • run on server的效果
  • 在servlet中使用
  • 改变javaBean的作用域
  • 在jsp中添加form表单
  • run on server的效果
  • 在类中使用
  • 了解设计模式MVC
  • 包的结构图:
  • 改造考试系统


概述

为什么要使用JavaBean?
优点:javabean实现前端和后端的分离,可以很好的维护,结构清晰
优点:同时赋予该类一个id,可解决代码重复编写,方便开发
优点:其实它是对<%%><%=%>java代码的封装,简便了我们的开发。

在jsp中使用

src里面写JavaBean的.class文件

代码中有具体的注释

package src12;
//要求1、是public 类
public class StudentBeen {
//要求3:必须是私有首字母小写的属性
	private int stuNo;
	private String stuName;
	public String address;
	//要求2、有无参构造方法,其实默认会有1个无参构造方法
//	public StudentBeen() {
//		
//	}
//	public StudentBeen(int stuNo,String stuName,String address) {
//		this.stuName=stuName;
//		
//	}
	//要求4、有get set方法

	public int getStuNo() {
		return stuNo;
	}

	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
 }

WebContent写.jsp页面

<!-- 如何使用JavaBeen?
在jsp的页面中使用:通过jsp的动作标签来使用
《jsp:useBeen》《jsp:getProperty》《jsp:setProperty》

 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>JavaBeen页面上的使用方法</h1>
<!-- 
1、引入been的对象 ,方法:鼠标右键点击StudentBeen 选中copy qualified name 再粘贴到jsp:useBeen的class里面,自定义一个id属性,之后就用这个id来使用这个javaBeen
2、set 或者get been的属性值,方法:在标签体内为对象赋值属性,name=id的值,property=属性的名字,value=property对应 的属性的值
-->
	<jsp:useBean  id="student" class="src12.StudentBeen">
		<jsp:setProperty name="student"  property="stuName" value="唐僧"/>
		<jsp:setProperty name="student"  property="stuNo" value="00001"/>
	</jsp:useBean>
	<!-- 获取属性值 -->
	<jsp:getProperty property="stuName" name="student"/>


</body>
</html>

run on server的效果

jsp在Java中的作用 在jsp中使用javabean有什么好处_html

在servlet中使用

改变javaBean的作用域

默认bean作用域是在当前这个jsp页面中的,如果加上scope=“session” 那么在session中我们便可以获取student
在src中创建servlet文件

package src12;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class servlet12
 */
@WebServlet("/servlet12")
public class servlet12 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet12() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		StudentBean stu=(StudentBean)session.getAttribute("student");
		System.out.println(stu.getStuName());
	}

}

在jsp中添加form表单

下面的jsp文件只比上面的jsp文件在22行有区别,以及添加了29-30行的form表单进行servlet的提交,action根据你文件 的位置一定要写对,对应于上面servlet文件中的@WebServlet("/servlet12"),如果直接在Web-Content下可以直接在action中写“/servlet12”,但是如果不是就要像博主一样改变。

<!-- 如何使用JavaBeen?
在jsp的页面中使用:通过jsp的动作标签来使用
《jsp:useBean》《jsp:getProperty》《jsp:setProperty》

 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>JavaBeen页面上的使用方法</h1>
<!-- 
1、引入bean的对象 ,方法:鼠标右键点击StudentBeen 选中copy qualified name 再粘贴到jsp:useBeen的class里面,自定义一个id属性,之后就用这个id来使用这个javaBeen
2、set 或者get bean的属性值,方法:在标签体内为对象赋值属性,name=id的值,property=属性的名字,value=property对应 的属性的值
3、默认bean作用域是在当前这个jsp页面中的,如果加上scope="session" 那么在session中我们便可以获取student
-->
	<jsp:useBean  id="student" class="src12.StudentBean" scope="session">
		<jsp:setProperty name="student"  property="stuName" value="唐僧"/>
		<jsp:setProperty name="student"  property="stuNo" value="00001"/>
	</jsp:useBean>
	<!-- 获取属性值 -->
	<jsp:getProperty property="stuName" name="student"/>
	<!-- 写一个from表单就可以跳转到servlet -->
	<form  action="../servlet12" method="post">
		<input type="submit" value="Javabean的测试">
	</form>

</body>
</html>

run on server的效果

jsp在Java中的作用 在jsp中使用javabean有什么好处_jsp页面_02

在类中使用

了解设计模式MVC

jsp在Java中的作用 在jsp中使用javabean有什么好处_jsp页面_03


JavaBean:表示数据,结果存储,业务处理操作。(充当Model)

Servlet:处理客户端的请求,创建JavaBean对象,执行业务逻辑(bean访问数据)将执行结果发到视图组件。(Controller)

视图(jsp): 发起请求,展示处理结果。(View)

包的结构图:

jsp在Java中的作用 在jsp中使用javabean有什么好处_jsp在Java中的作用_04

改造考试系统

具体代码请查看博主的另外一篇博客。

jsp在Java中的作用 在jsp中使用javabean有什么好处_html_05