JSP:setPropery ()、getProperty(){两个页面}

JSP:setPropery ()、getProperty(){两个页面}

jsp jsp:useBean JavaBean 赵振江

描述

创建一个JavaBean,用以接受汽车的颜色,以及表示汽车是否安装了空调的布尔值。如果尔值为真,则汽车安装了空调;如果布尔值为假,则汽车未安装空调。该JavaBean返回颜色和布尔值。(完成javaBean,创建一个页面显示结果) 
mycar.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 ‘mycar.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 action="4/showcar.jsp" >
    汽车颜色:<input type="text" name="color"><br>
    有空调吗?<select name="kongtiao">
    <option value="true">有空调</option>
    <option value="false">没空调</option>
    </select>
    <button type="submit">提交</button>
    </form>
  </body>
</html>

Car.java

package bean;

public class Car {
    private String color;
    private boolean kongtiao;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public boolean isKongtiao() {
        return kongtiao;
    }
    public void setKongtiao(boolean kongtiao) {
        this.kongtiao = kongtiao;
    }
}

showcar.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 ‘showcar.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>
    <%
    String color=request.getParameter("color");
    boolean kongtiao=Boolean.valueOf(request.getParameter("kongtiao"));
     %>
     <jsp:useBean id="c" class="bean.Car">
     <jsp:setProperty name="c" property="color" value="<%=color %>"/><br>
     <jsp:setProperty name="c" property="kongtiao" value="<%=kongtiao%>"/><br>
     </jsp:useBean>
     <jsp:getProperty name="c" property="color" />
     <jsp:getProperty name="c" property="kongtiao" />
  </body>
</html>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。