美文网首页Servlet实例专题
Servlet中将JavaBean对象传递给JSP页面

Servlet中将JavaBean对象传递给JSP页面

作者: 神坛下的我 | 来源:发表于2018-08-21 09:45 被阅读0次

    InfoBean.java

    public class InfoBean {
        private String name;
        private String pwd;
        private String sex;
        private  int age;
        private String email;
        public InfoBean(){}
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        
    }
    

    UseServlet.java

    @WebServlet("/user.do")
    public class UseServlet extends HttpServlet{
    
        private static final long serialVersionUID = 1L;
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //设置字符集格式为utf-8
            req.setCharacterEncoding("UTF-8");
            String name=req.getParameter("name");
            String pwd=req.getParameter("pwd");
            String sex=req.getParameter("sex");
            String age=req.getParameter("age");
            String email=req.getParameter("email");
            InfoBean user= new InfoBean();
            user.setName(name);
            user.setPwd(pwd);
            user.setSex(sex);
            user.setAge(new Integer(age));
            user.setEmail(email);
            req.setAttribute("User", user);
            
            req.getRequestDispatcher("success.jsp").forward(req, resp);
            
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doPost(req, resp);
        }
    }
    

    index.jsp

    <body>
    
        <form action="user.do" method="post">
            <table align="center">
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="name"/></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="pwd"/></td>
                </tr>
                <tr>
                    <td>性别:</td>
                    <td>
                        <input type="radio" name="sex" value="男"/>男
                        <input type="radio" name="sex" 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 colspan="2" align="center">
                    <input type="submit" value="登录"/>
                    <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
        </form>
        
    </body>
    

    success.jsp

    <%
            InfoBean user = (InfoBean)request.getAttribute("User");
        %>
        <b>登录成功!</b><br>
        您的信息为:<br/>
        <table align="center">
                <tr>
                    <td>用户名:</td>
                    <td><%=user.getName() %></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><%=user.getPwd() %></td>
                </tr>
                <tr>
                    <td>性别:</td>
                    <td><%=user.getSex() %></td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td><%=user.getAge()%></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><%=user.getEmail() %></td>
                </tr>
                <tr>
                <td><a href="index.jsp">【回到主页】</a></td></tr>
        </table>
    
    25.PNG

    相关文章

      网友评论

        本文标题:Servlet中将JavaBean对象传递给JSP页面

        本文链接:https://www.haomeiwen.com/subject/luldiftx.html