–jsp:useBean –jsp:setProperty –jsp:getProperty –jsp:forward –jsp:include –jsp:param –实例 ?计算器
------------------------------------------------------------------------
? jsp:useBean –格式 ? <jsp:useBean id="name" class="package.Class" /> –目的 ? 不需用到显式的Java编程就能够创建Java类的实例 –注意 ? 简单来说 –<jsp:useBean id="book1" class=“com.amaker.Book" /> ? 可以认为等同于下面的scriptlet –<% coreservlets.Book book1 = new com.amaker.Book(); %> –但jsp:useBean拥有下面两项额外的优势 ? 从请求参数中导出对象的值更容易 ? 在页面和servlet间共享对象更容易
User.java
TestUseBean.jsp
测试:
下面我们使用动作元素jsp:useBean来实现哈~
TestUseBean.jsp
测试:
? jsp:setProperty –格式 ? <jsp:setProperty name="name“ property="property“ value="value" /> –目的 ? 不需用到显式的Java编程就可以设置bean的属性(即调用setXxx方法) –注意 ? <jsp:setProperty name="book1“ property="title“ value=“amaker JSP" /> –等价于下面的scriptlet ? <% book1.setTitle(“Java Web Dev!"); %>
? jsp:getProperty –格式 ? <jsp:getProperty name="name" property="property" /> –目的 ? 不需用到显式的Java编程就可以访问bean的属性(即调用getXxx 方法) –注意 ? <jsp:getProperty name="book1" property="title" /> –等价于下面的JSP表达式 ? <%= book1.getTitle() %>
下面我们通过请求参数来获取
login.jsp
TestUseBean.jsp
测试:
同样我们也可以用param获取参数
TestUseBean.jsp
测试:
–jsp:forward ?转发请求到指定文件 ?语法 –<jsp:forward page=“URL”/> ?例如 –<jsp:forward page=“/result.jsp” />
login.jsp
process.jsp
测试:
帐号michael跳转到成功页面。
非michael帐号跳转到失败页面。
? jsp:include –在页面中动态包含文件 –语法 ? <jsp:include page=“URL” flush=“true”/> –例如 ? <jsp:include page=“header.jsp”flush=“true”/>
TestJspInclude.jsp
include.jsp
测试:
? jsp:param –获得请求参数
TestJspInclude.jsp
include.jsp
测试:
–实例 ?计算器
Calculator.jsp
<%@ page language= "java" import= "java.util.*" pageEncoding= "gbk"%>
<%@page import= "com.michael.jsp.CalculatorBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Calculator.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name="f1" id="f1" action="<%=path %>/Calculator.jsp" method="post"> <jsp:useBean id="cb" class="com.michael.jsp.CalculatorBean"></jsp:useBean> <jsp:setProperty property="*" name="cb"/> <hr> 计算结果:<% if(cb.getOperator()!=null){ try{ out.println(cb.getFirst()+cb.getOperator()+cb.getSecond()+"="+cb.getResult()); }catch(Exception e){ } }else{ out.println("请输入!"); } %> <hr> <table border="0"> <tr> <td>第一个数:</td> <td><input type="text" name="first"></td> </tr> <tr> <td>操作符:</td> <td> <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> </tr> <tr> <td>第二个数:</td> <td><input type="text" name="second"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="计算"></td> </tr> </table> </form> </body> </html>
<%@page import= "com.michael.jsp.CalculatorBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Calculator.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name="f1" id="f1" action="<%=path %>/Calculator.jsp" method="post"> <jsp:useBean id="cb" class="com.michael.jsp.CalculatorBean"></jsp:useBean> <jsp:setProperty property="*" name="cb"/> <hr> 计算结果:<% if(cb.getOperator()!=null){ try{ out.println(cb.getFirst()+cb.getOperator()+cb.getSecond()+"="+cb.getResult()); }catch(Exception e){ } }else{ out.println("请输入!"); } %> <hr> <table border="0"> <tr> <td>第一个数:</td> <td><input type="text" name="first"></td> </tr> <tr> <td>操作符:</td> <td> <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> </tr> <tr> <td>第二个数:</td> <td><input type="text" name="second"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="计算"></td> </tr> </table> </form> </body> </html>
CalculatorBean.java
package com.michael.jsp;
public class CalculatorBean {
private double first;
private double second;
private double result;
private String operator;
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 double getResult() {
try {
if(operator!= null){
if(operator.equals( "+")){
result = first + second;
} else if(operator.equals( "-")){
result = first - second;
} else if(operator.equals( "*")){
result = first * second;
} else if(operator.equals( "/")){
result = first / second;
} else{
result = 0.0;
}
} else{
result = 0.0;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void setResult( double result) {
this.result = result;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}
public class CalculatorBean {
private double first;
private double second;
private double result;
private String operator;
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 double getResult() {
try {
if(operator!= null){
if(operator.equals( "+")){
result = first + second;
} else if(operator.equals( "-")){
result = first - second;
} else if(operator.equals( "*")){
result = first * second;
} else if(operator.equals( "/")){
result = first / second;
} else{
result = 0.0;
}
} else{
result = 0.0;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void setResult( double result) {
this.result = result;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}
测试:
800/50
30+50
------------------------------------------------------------------------