jsp用户注册简单实例2

要求

编程实现用户注册,信息包括姓名、密码、年龄。提交后注册页面显示信息,分别通过request、bean属性、getProperty动作获取。

使用bean属性获取时需要建立java类如下:

UserRegisterBean类:

package bean;

public class UserRegisterBean {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    private int pass;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getPass() {
        return pass;
    }

    public void setPass(int pass) {
        this.pass = pass;
    }


}

注册代码register.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/9/7
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册页面</title>
</head>
<body>
<%--<jsp:useBean id="UserRegisterBean" scope="session" class="bean.UserRegisterBean">--%>
<form  action="register2.jsp" method="post">
    <table>
        <tr>
            <td>姓名:</td>
            <td><input type="text" name="name" ></td>

        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="text" name="pass"></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" name="age"></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
        </tr>
    </table>

</form>

</body>
</html>


注册信息register2.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/9/12
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册信息</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("name");
    String pass=request.getParameter("pass");
    String age=request.getParameter("age");
%>
使用request获取:<br>
姓名:<%=name%><br>
密码:<%=pass%><br>
年龄:<%=age%><br>
<hr>
使用bean属性获取:<br>
<%--<jsp:getProperty name="user" property="name"/>--%>
<jsp:useBean id="user" scope="page" class="bean.UserRegisterBean" >
    <jsp:setProperty name="user" property="*"/>

</jsp:useBean>
姓名:<%=user.getName()%><br>
<hr>
使用getProperty动作:<br>
密码:<jsp:getProperty name="user" property="pass"/><br>
年龄:<jsp:getProperty name="user" property="age"/>
</body>
</html>

运行结果:

jsp用户注册简单实例2_第1张图片

你可能感兴趣的:(jsp)