JDBC+Servlet+JSP整合开发之25.JSP动作元素

�Cjsp:useBean
�Cjsp:setProperty
�Cjsp:getProperty
�Cjsp:forward
�Cjsp:include
�Cjsp:param
�C实例

?计算器
------------------------------------------------------------------------
? jsp:useBean
�C格式
? <jsp:useBean id="name" class="package.Class" />
�C目的
? 不需用到显式的Java编程就能够创建Java类的实例
�C注意
? 简单来说
�C<jsp:useBean id="book1" class=“com.amaker.Book" />
? 可以认为等同于下面的scriptlet
�C<% coreservlets.Book book1 = new com.amaker.Book(); %>
�C但jsp:useBean拥有下面两项额外的优势
? 从请求参数中导出对象的值更容易
? 在页面和servlet间共享对象更容易
User.java 
image
TestUseBean.jsp
image
测试:
image
下面我们使用动作元素jsp:useBean来实现哈~
TestUseBean.jsp
image
测试:
image 
? jsp:setProperty
�C格式 ? <jsp:setProperty name="name“ property="property“ value="value"
/>
�C目的 ? 不需用到显式的Java编程就可以设置bean的属性(即调用setXxx方法)
�C注意 ? <jsp:setProperty name="book1“ property="title“ value=“amaker JSP" />
�C等价于下面的scriptlet
? <% book1.setTitle(“Java Web Dev!"); %>
 
? jsp:getProperty
�C格式
? <jsp:getProperty name="name" property="property" />
�C目的
? 不需用到显式的Java编程就可以访问bean的属性(即调用getXxx
方法)
�C注意
? <jsp:getProperty name="book1" property="title" />
�C等价于下面的JSP表达式
? <%= book1.getTitle() %>
下面我们通过请求参数来获取
login.jsp
image
TestUseBean.jsp
image
测试:
image
  image
同样我们也可以用param获取参数
TestUseBean.jsp
image
测试:
image
image
�Cjsp:forward
?转发请求到指定文件
?语法
�C<jsp:forward page=“URL”/>
?例如
�C<jsp:forward page=“/result.jsp” />
login.jsp
image
process.jsp
image
测试:
image
帐号michael跳转到成功页面。
image
image
非michael帐号跳转到失败页面。
image
? jsp:include
�C在页面中动态包含文件
�C语法
? <jsp:include page=“URL” flush=“true”/>
�C例如
? <jsp:include page=“header.jsp”flush=“true”/>
TestJspInclude.jsp
image
include.jsp
image
测试:
image 
? jsp:param
�C获得请求参数
TestJspInclude.jsp
image
include.jsp
image
测试:
image
�C实例
?计算器
Calculator.jsp
image 
<%@ 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>
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;    
        }    
}
测试:
image
800/50
image
image
30+50
image
image
------------------------------------------------------------------------

你可能感兴趣的:(java,jsp,servlet,jdbc,动作元素)