6

我想将 Student 类型的自定义对象从 servlet 传递给 JSP。我创建了一个学生 bean 类。Student 包含 2 个属性 firstname 和 lastName。

学生豆:

import java.io.Serializable;

public class Student implements Serializable {

    public Student() {
    }

    String firstName;
    String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

用于从用户获取 FirstName 和 LastName 的 HTML 文件:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <form id="myForm" method="POST" action="MyFormServlet">
        FirstName<input type="text" id="firstName"  name="FirstName"/><br>
        LastName<input type="text" id="lastName" name="LastName"/><br>
        <button type="submit" />Submit</button>
    </form>
</body>
</html>

小服务程序代码:

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



    public class MyFormServlet extends HttpServlet {

        @Override
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) {

            Student s = new Student();
            s.setFirstName(request.getParameter("FirstName"));
            s.setLastName(request.getParameter("LastName"));

            HttpSession session =request.getSession();
            session.setAttribute("student", s);

            try {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/myJsp.jsp");
                rd.forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

myJsp.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
    <body>
        <%
            // I want to do something like this :   
            //Student student =(Student)session.getAttribute("student");
            //String fullName=student.firstName + student.lastName;
        %>      
        <h1><%=fullName%></h1>
    </body>
</html>

我想获取“学生”对象,访问其属性并将其存储在 JSP 变量中以供进一步处理。

4

4 回答 4

10

The setAttribute() method on request, session and servletContext will already make it available as a JSP/EL variable by the attribute name.

In your particular case, with the following line in the servlet,

session.setAttribute("student", s);

it's available in JSP/EL as ${student}. So, just this should do:

<body>
    <h1>${student.firstName} ${student.lastName}</h1>
</body>

If you want to store it as another variable in JSP so that you can reuse it multiple times, use JSTL <c:set>.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<body>
    <c:set var="fullName" value="${student.firstName} ${student.lastName}" />
    <h1>${fullName}</h1>
</body>
于 2012-04-24T15:05:42.673 回答
3

如果不需要在整个会话中使用,您应该将属性传递给请求范围。在 Servlet 中,

 request.setAttribute("student", s);

在 JSP 中,

Student student =(Student) request.getAttribute("student");
String fullName = "Default";
if(student!=null){
 fullName=student.firstName +" " + student.lastName;
}
于 2012-04-24T12:07:20.170 回答
1

不必使用request.setAttribute()来发送数据。您也可以为此使用HttpSession 。首先,您必须像已经完成的那样设置它。

HttpSession session =request.getSession();
session.setAttribute("student", s);

现在您可以使用 getAttribute() 在 jsp 中取回它

Student student =(Student) session.getAttribute("student");

现在你可以随心所欲地和你的学生一起玩了。(您可能希望将 Student 类导入 jsp。)

于 2016-03-23T06:33:27.753 回答
0

我遇到了同样的问题,然后在跟踪和错误之后,我得到了如下解决方案 -

<% Student student = (Student)request.getAttribute("student");%><br>
<table><br>
    <tr><td>First Name :</td><td><%=student.getFirstName() %></td></tr><br>
    <tr><td>Last Name :</td><td><%=student.getLastName() %></td></tr><br>
</table><br>
于 2017-10-16T09:56:45.150 回答