[JSP] 利用JavaBean来传递表单数据

2023-07-06 17:00:33 浏览数 (1)

利用JavaBean来传递表单数据

新建一个表单, 用来提交数据, 代码如下

代码语言:javascript复制
<form name="login" action="loginResponse.jsp">
            请输入您的用户名:<br />
            <input type="text" name="user_name"/><br />
            请输入您的年龄:<br />
            <input type="text" name="user_age"/><br />
            请输入您的地址:<br />
            <input type="text" name="user_address"/><br />
            <input type="submit" value="OK"/>
            <input type="reset" value="Reset"/>            
        </form>

新建一个 Java class, 命名为: loginBean, 代码如下:

代码语言:javascript复制
package javabean;


publicclass loginBean {
    private String name;
    privateint age;
    private String address;

    public String getAddress() {
        return address;
    }

    publicvoid setAddress(String address) {
        this.address = address;
    }

    publicint getAge() {
        return age;
    }

    publicvoid setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    publicvoid setName(String name) {
        this.name = name;
    }    
    
}

新建一个jsp网页, 命名为: loginResponse, 将会在此页面上应用JavaBean, 代码如下:

代码语言:javascript复制
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:useBean id="loginBean" class="javabean.loginBean"/>
        <jsp:setProperty property="name" name="loginBean" param="user_name"/>
        <jsp:setProperty property="age" name="loginBean" param="user_age"/>
        <jsp:setProperty property="address" name="loginBean" param="user_address"/> 
        
        Your name is: <jsp:getProperty property="name" name="loginBean"/><br />
        Your age is: <jsp:getProperty property="age" name="loginBean"/><br />
        Your address is: <jsp:getProperty property="address" name="loginBean"/><br />        
    </body>
</html>

0 人点赞