Servlet通过JavaBean传值到JSP页面

时间:2022-09-25 19:08:34

主要通过Attribute进行传递,主要代码如下:

赋值,并定义跳转的页面:

        request.setAttribute("user", user);
        request.getRequestDispatcher("view.jsp").forward(request, response);

取值,在JSP页面或者另外一个Servlet中:

UserInfo user=(UserInfo)request.getAttribute("user");

示例:

1、login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="Java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title>登录页面</title>
</head>
<body>
    <form action="PassServlet" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>&nbsp;码:</td>
                <td><input type="password" name="pwd" /></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input type="radio" name="sex" value="男" /><input
                    type="radio" name="name" value="女" /></td>
            </tr>
            <tr>
                <td>年龄:</td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="注册" /> <input type="reset"
                    value="重置" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

2、UserInfo.java

package com.jsoft.testservletbasics;

public class UserInfo {
    private String userName;
    private String userPwd;
    private String userSex;
    private int userAge;
    private String userEmail;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

}

3、PassServlet.java

package com.jsoft.testservletbasics;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class PassServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String email = request.getParameter("email");
        UserInfo user = new UserInfo();
        user.setUserName(name);
        user.setUserPwd(pwd);
        user.setUserSex(sex);
        user.setUserAge(Integer.parseInt(age));
        user.setUserEmail(email);
        request.setAttribute("user", user);
        request.getRequestDispatcher("view.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

5、view.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.jsoft.testservletbasics.UserInfo" %>
<!DOCTYPE HTML >
<html>
<head>
<title>显示页面</title>
</head>
<body>
    <%
      UserInfo user=(UserInfo)request.getAttribute("user");
     %>
    <table border="1" align="center">
        <tr>
            <td>用户名:</td>
            <td><%=user.getUserName()%></td>
        </tr>
        <tr>
            <td>&nbsp;码:</td>
            <td><%=user.getUserPwd()%></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td><%=user.getUserSex()%></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><%=user.getUserAge()%></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><%=user.getUserEmail()%></td>
        </tr>
    </table>
</body>
</html>

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test19