JavaBean实例:
新建如下项目——新建包——新建类……
实例一:
一:在tsc包中新建Person类
package tsc;
public class Person {
String name = "ts";
int age = 18;
public Person() {
}
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;
}
}
二:新建jweb4.jsp页面,JavaBean在地址栏中传递参数
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="tsc.*"%>
<%--地址栏中传递参数 --%>
<!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="persons" class="tsc.Person"></jsp:useBean>
<jsp:setProperty property="name" name="persons" param="n" />
<jsp:setProperty property="age" name="persons" param="m" />
<h1>
姓名是:<jsp:getProperty property="name" name="persons" /><br> 年龄是:<jsp:getProperty
property="age" name="persons" />
</h1>
</body>
</html>
运行结果:
综上:通过JavaBean获取属性的默认值,通过地址栏赋值 param属性用来传递参数
所以,在地址栏中继续输入?n=姓名值&m=年龄值即可成功传递参数。