Servlet中将JavaBean对象传递到JSP页

在Servlet中将JavaBean对象传递到JSP页


将一个封装用户注册信息的Javabean对象传递到JSP页面,然后在JSP页汇总读取该JavaBean对象的数据,主要是在servlet中使用HttpServletRequest对象的getParameter()方法、setAttribute()方法以及getAttribute()方法。setAttribute()方法的作用是可以在HttpServletRequest对象中保存一个属性。

一、新建用户注册页 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here


用户名:
密码:
性别:
年龄:
Email:

二、新建名为UserInfo的javaBean类,该类用于封装用户的注册信息。

package com.user;

/**
 * UerInfo的JavaBean类,该类用于封装用户的注册信息
 * @author yilong
 *
 */
public class UserInfo {
    private String userName;    // 用户名
    private String userPwd;     // 密码
    private String userSex;     // 性别
    private int userAge;        // 年龄
    private String email;       // Email

    public UserInfo(){}

    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

三、新建名为PassServlet的Servlet类,在该类的doPost()方法中将获取的用户注册信息封装到UserInfo中,然后将请求转发到logininfo.jsp页。

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.user.UserInfo;

public class PassServlet extends HttpServlet {

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

        response.setContentType("text/html");
        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");
        // 创建封装用户信息的JavaBean对象
        UserInfo user = new UserInfo();
        user.setUserName(name);
        user.setUserPwd(pwd);
        user.setUserSex(sex);
        Integer userAge = new Integer(age);
        user.setUserAge(userAge);
        user.setEmail(email);

        // 将user对象添加到request对象中
        request.setAttribute("User", user);
        // 将请求转发到logininfo.jsp页面
        request.getRequestDispatcher("logininfo.jsp").forward(request, response);
    }
}

四、在logininfo.jsp页中,使用request内置对象的getAttribute()方法获取用户注册信息的JavaBean对象,然后将该对象中封装的注册信息显示出来。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.user.UserInfo" %>




Insert title here


    <%
        UserInfo user = (UserInfo)request.getAttribute("User"); 
     %>
用户名: <%=user.getUserName() %>
密码: <%=user.getUserPwd() %>
性别: <%=user.getUserSex() %>
年龄: <%=user.getUserAge() %>
Email: <%=user.getEmail() %>

你可能感兴趣的:(【编程语言】➣,jsp)