Java JSP&;MVC:将对象从控制器传递到jsp页面

Java JSP&;MVC:将对象从控制器传递到jsp页面,java,jsp,model-view-controller,Java,Jsp,Model View Controller,我正在jsp中使用MVC设计模式。我可以将对象传递给单个jsp页面,但不能传递给其他jsp页面(可能有许多页面)。我想使用对象(或通过getter)显示教师类的用户名和密码 公共课堂教师{ 字符串用户名; 字符串密码; /*接球手和接球手*/ } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应) 抛出ServletException、IOException{ 字符串用户名; 字符串密码; userName=request.ge

我正在jsp中使用MVC设计模式。我可以将对象传递给单个jsp页面,但不能传递给其他jsp页面(可能有许多页面)。我想使用对象(或通过getter)显示教师类的用户名和密码

公共课堂教师{
字符串用户名;
字符串密码;
/*接球手和接球手*/
}
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
字符串用户名;
字符串密码;
userName=request.getParameter(“tUserNameTxt”);
密码=request.getParameter(“tPasswordTxt”);
教师=新教师();
setUserName(用户名);
设置密码(密码);
request.setAttribute(“教师”,教师);
请求调度;
dispatch=request.getRequestDispatcher(“login success teacher.jsp”);
发送、转发(请求、响应);
}
要在页面上显示的数据:
第2页:

在Servlet中而不是在特定页面中设置会话范围内的
教师

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //...
    Teacher teacher = new Teacher();
    //...
    request.getSession().setAttribute("teacher", teacher);
    //...
}
然后,检索它并在JSP代码中使用它,不会出现任何问题:

Page1.jsp:

<body>
    Welcome ${teacher.username}. Your ID is ${teacher.password}
    <h1>
        <a href="page2.jsp">Click Here</a>
    </h1>
</body>

欢迎${teacher.username}。您的ID是${teacher.password}
Page2.jsp:

<body>
    Welcome ${teacher.username}. Your ID is ${teacher.password}
</body>

欢迎${teacher.username}。您的ID是${teacher.password}
提示:

  • 不要将密码字段用作ID。甚至不用于测试目的。分配一个正确的ID,不要将(真实的)密码存储在数据库之外的任何地方,至少要散列
<body>
    Welcome ${teacher.username}. Your ID is ${teacher.password}
</body>