0

I can pass Integer, String, Float, etc.. but when I am passing my defined object (Employee) the JSP is receiving it as null.

<%@ 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");
}
mnille
  • 1,328
  • 4
  • 16
  • 20
Rahul Agrawal
  • 623
  • 1
  • 5
  • 18
  • Are you sure that the request attribute "searchResult" does exists? – Reporter Jul 27 '16 at 12:19
  • Do you set that attribute at some point? And do you set it on the correct request? – Thomas Jul 27 '16 at 12:20
  • protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EmployeeDAO dao = new EmployeeDAOImpl(); Employee result = dao.search(request.getParameter("id")); ServletContext app = getServletContext(); app.setAttribute("searchResult", result); System.out.println("Emp= "+result); response.sendRedirect("./searchview.jsp"); } – Rahul Agrawal Jul 27 '16 at 12:22
  • edit your question and add the above code in that. – Prabhat Jul 27 '16 at 12:26
  • 1
    add your `result` variable in request param using `request#setAttribute(arg0, arg1); method and farward the request to JSP page – Prabhat Jul 27 '16 at 12:37

1 Answers1

1

Try this:

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>

This cannot work with sendRedirect though as you're basically making a round-trip between the client and server, spanning over 2 requests, not one. The first request has your parameter, but since your client does not store it, it is lost when the redirection occurs. You should forward to your JSP unless what's done by the servlet should not be executed over and over again (like a database insertion). Look here if you really need to redirect.

Arthur Noseda
  • 2,534
  • 19
  • 28