Ich kann übergeben Integer
, String
, Float
, etc .. aber wenn ich mein definiertes Objekt übergeben (Employee
) die JSP empfängt es als null
.Wie übergeben Sie Objekte von Servlets an JSP?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.rahul.model.bean.*"%>
<!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">
<title>Search Result</title>
</head>
<body>
<%
Employee record = (Employee) request.getAttribute("searchResult");
out.println(record);
%>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Designation</th>
<th>Department</th>
<th>Salary</th>
</tr>
</table>
</body>
</html>
And My Controlleer doGet is:
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
EmployeeDAO dao = new EmployeeDAOImpl();
Employee result = dao.search(request.getParameter("id"));
// PrintWriter pw=response.getWriter();
// pw.println(result);
ServletContext app = getServletContext();
app.setAttribute("searchResult", result);
System.out.println("Emp= "+result);
response.sendRedirect("./searchview.jsp");
}
Sind Sie sicher, dass das Anfrageattribut "searchResult" existiert? – reporter
Setzen Sie dieses Attribut irgendwann? Und stellen Sie es auf die richtige Anfrage ein? – Thomas
protected void doGet (HttpServletRequest-Anforderung, HttpServletResponse-Antwort) löst ServletException, IOException {\t \t EmployeeDAO dao = new EmployeeDAOImpl(); \t \t Employee result = dao.search (request.getParameter ("id")); \t \t \t \t ServletKontext app = getServletContext(); \t \t app.setAttribute ("searchResult", Ergebnis); \t \t System.out.println ("Emp =" + Ergebnis); \t \t response.sendRedirect ("./ searchview.jsp"); \t} –