2016-07-27 6 views
0

私はIntegerStringFloatなどを渡すことができますが、私が定義したオブジェクト(Employee)を渡しているときに、JSPはnullとしてそれを受け取ります。サーブレットから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

保護されたボイドのdoGet(HttpServletRequestのリクエスト、HttpServletResponseの応答){\t \t EmployeeDAOのDAO =新しいEmployeeDAOImpl()ServletExceptionが、IOExceptionがスロー。 \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で動作することはできませんクライアントとサーバーの間で、1つではなく2つの要求にまたがっています。最初のリクエストにはパラメータがありますが、クライアントはそれを保存しないため、リダイレクトが発生すると失われます。サーブレットの処理を何度も繰り返し実行しない限り(データベースの挿入など)、JSPに転送する必要があります。本当にリダイレクトする必要がある場合は、hereを参照してください。

関連する問題