JSP动作元素

-jsp:useBean
 -格式
  <jsp:useBean id="name" class="package.Class"/>
 -目的
  不需用到显式的java编程就能创建java类的实例
 -注意
  简单来说
   -<jsp:useBean id="book1" class="com.tree.Book"/>
  可以认为等同于下面的scriptlet
   -<% coreservlets.Book book1=new com.tree.Book();%>
 -但jsp:useBean拥有下面两项额外的优势
   从请求参数中导出对象的值更容易
   在页面和servlet间共享对象更容易

-jsp:setProperty
-jsp:getProperty


这三个我用几个.jsp文件来说明一下吧

先简单的创建一个Person.java类
package beandemo;

public class Person {

	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 language="java" contentType="text/html; charset=ISO-8859-1"
	import="beandemo.Person"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

	<%
	
	    Person p=new Person();
	    p.setId(1);
	    p.setName("tree");
	    p.setAge(10);
	    
	    
	    out.println(p.getId());
	    out.println(p.getName());
	    out.println(p.getAge());
	    
	
	%>
</body>
</html>

然后使用javabean可以这样

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	import="beandemo.Person"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>	
	
	
	<jsp:useBean id="p" class="beandemo.Person" scope="session"/>
	<jsp:setProperty name="p" property="id" value="2"/>
	<jsp:setProperty name="p" property="name" value="tree"/>
	<jsp:setProperty name="p" property="age" value="9"/>
	 

	<jsp:getProperty name="p" property="id"/>
	<jsp:getProperty name="p" property="name"/>
	<jsp:getProperty name="p" property="age"/>
</body>
</html>

或者可以这样在另外一个jsp页面中这样写
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form name="f1" id="f1" action="jspdemo5.jsp" method="post">
	<table border="0">
	
		<tr>
			<td>ID:</td>
			<td><input type="text" name="id"></td>
		</tr>
	
		<tr>
			<td>Name:</td>
			<td><input type="text" name="name"></td>
		</tr>
		
		<tr>
			<td>age:</td>
			<td><input type="text" name="age"></td>
		</tr>
		
		<tr>
			<td colspan="2" align="center"><input type="submit"></td>
		</tr>
	
	
	</table>
</form>

</body>
</html>

然后把获取到的值传递给刚刚的jsp,jsp可以这样写
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	import="beandemo.Person"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>	
	
	
	
	<jsp:useBean id="p" class="beandemo.Person" scope="session"/>
	<jsp:setProperty name="p" property="*"/>
  	<jsp:getProperty name="p" property="id"/>
	<jsp:getProperty name="p" property="name"/>
	<jsp:getProperty name="p" property="age"/>
</body>
</html>


然后输出也可以改写
例如
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	import="beandemo.Person"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<jsp:useBean id="p" class="beandemo.Person" scope="session"/>
	<jsp:setProperty name="p" property="*"/>
	${p.id }
	${p.name }
	${p.age }
	//	 

<%--	<%=p.getId() %>
	<%=p.getName() %>
	<%=p.getAge() %>  --%>也可以
</body>
</html>


-jsp:forward
 -转发请求到指定文件
 -语法
   <jsp:forward page="URL"/>

-jsp:include
 -在页面中动态包含文件
 -语法
  <jsp:include page="URL" flush="true"/>

这俩都是添加页面,很简单。

-jsp:param
 -获得请求参数

最后有一个jsp的计算器页面。
如下

新建一个Calculator.java
package beandemo;

public class Calculator {
	private double first;
	private double second;
	private String operator;
	private double result;
	public double getFirst() {
		return first;
	}
	public void setFirst(double first) {
		this.first = first;
	}
	public double getSecond() {
		return second;
	}
	public void setSecond(double second) {
		this.second = second;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public double getResult() {
		
		if(operator!=null&&operator.equals("+")){
			result=first+second;
		}else if(operator!=null&&operator.equals("-")){
			result=first-second;
		}else if(operator!=null&&operator.equals("*")){
			result=first*second;
		}else if(operator!=null&&operator.equals("/")){
			result=first/second;
		}else result=0;
		return result;
	}
	public void setResult(double result) {
		this.result = result;
	}
	
}


一个Calculator.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My Calculator</title>
</head>
<body>


<jsp:useBean id="c" class="beandemo.Calculator"/>
<jsp:setProperty name="c" property="*"/>

Result:${c.first }${c.operator }${c.second }=${c.result }

<form name="f1" id="f1" action="Calculator.jsp" method="post">
	<table border="0">
	
		<tr>
			<td>First Num:</td>
			<td><input type="text" name="first"></td>
		</tr>
	
		<tr>
			<td>Operator:</td>
			<td>
				<select name="operator">
					<option value="+">+</option>
					<option value="-">-</option>
					<option vlaue="*">*</option>
					<option value="/">/</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>Second Num:</td>
			<td><input type="text" name="second"></td>
		</tr>
		
		<tr>
			<td colspan="2" align="center"><input type="submit" value="Calculate..."></td>
		</tr>
	
	
	</table>
</form>


</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值