2016-07-27 79 views
0

我可以通過IntegerString,Float等。但是當我通過我定義的對象(Employee)時,JSP正在接收它作爲null如何將對象從servlet傳遞給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"); 
} 
+0

您確定請求屬性「searchResult」確實存在嗎? – reporter

+0

你在某個時候設置了這個屬性嗎?你是否按照正確的要求設置了它? – Thomas

+0

protected void doGet(HttpServletRequest請求,HttpServletResponse響應)拋出ServletException異常IOException {} {}} EmployeeDAO dao = new EmployeeDAOImpl(); \t \t員工結果= dao.search(request.getParameter(「id」)); \t \t \t \t ServletContext app = getServletContext(); \t \t app.setAttribute(「searchResult」,result); \t \t System.out.println(「Emp =」+ result); \t \t response.sendRedirect(「./searchview.jsp」); \t} –

回答

1

試試這個:

GreetingsServlet.java

import java.io.IOException; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet("/greetings") 
public class GreetingsServlet extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     String message = "Hello, World"; 
     req.setAttribute("message", message); 
     RequestDispatcher dispatcher = req.getServletContext().getRequestDispatcher("/WEB-INF/jsp/greetings.jsp"); 
     dispatcher.forward(req, resp); 
    } 

} 

greetings.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    </head> 
    <body> 
    <h1><%= request.getAttribute("message") %></h1> 
    </body> 
</html> 

這不能與工作sendRedirect但正如你基本上是做一個往返在客戶端和服務器之間,跨越2個請求,而不是一個。第一個請求具有您的參數,但由於您的客戶端不存儲它,所以發生重定向時會丟失。您應該轉發到您的JSP,除非該servlet所做的操作不應該一遍又一遍地執行(如數據庫插入)。如果你真的需要重定向,請看here