Nick
Nick

Reputation: 351

servlet to jsp pass value

I want to simple pass value servlet to jsp page. I want to run jsp file and onload data is display from getting servlet

But I got null : "Servlet communicated message to JSP: null "

below is my code.

java code

package api;

public class ServletToJSP extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //communicating a simple String message.
        String message = "Example source code of Servlet to JSP communication.";
        request.setAttribute("message", message);

        RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");
        reqDispatcher.forward(request,response);


      }
}

jsp file

<%@ page import="api.ServletToJSP" language="java" %>


<html>
<body>
<%
  String message = (String) request.getAttribute("message");
  out.println("Servlet communicated message to JSP: "+ message);

 // Vector vecObj = (Vector) request.getAttribute("vecBean");
//  out.println("Servlet to JSP communication of an object: "+vecObj.get(0));
%>
</body>
</html>

web.xml

<servlet>
      <servlet-name>ServletToJSP</servlet-name>
      <servlet-class>api.ServletToJSP</servlet-class>
  </servlet>
  <servlet-mapping>

      <servlet-name>ServletToJSP</servlet-name>
      <url-pattern>/ServletToJSP/*</url-pattern>
  </servlet-mapping>

Upvotes: 4

Views: 37455

Answers (4)

Rakesh Kumar DEo
Rakesh Kumar DEo

Reputation: 1

You have to use context path to get message from servlet to jsp. This will definitely work I have done it.

String msg = "Message from servlet to jsp";
response.sendRedirect(response.encodeRedirectURL(contextPath+"/report/test.jsp?msg="+msg));

Upvotes: -1

user2575725
user2575725

Reputation:

There are few things to change:

In servlet

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //communicating a simple String message.
    String message = "Example source code of Servlet to JSP communication.";
    request.setAttribute("message", message);
    request.getRequestDispatcher("javaPapers.jsp").forward(request,response);
}

In jsp

<html>
  <body>
    Servlet communicated message to JSP: ${message}
  </body>
</html>

Changes made:

  • In servlet, used request.getRequestDispatcher(String url)
  • In jsp, removed servlet import.
  • In jsp, used EL to get attribute value.

Upvotes: 3

MaVRoSCy
MaVRoSCy

Reputation: 17839

Passing the param using the request.setAttribute("message", message); should work using this code:

RequestDispatcher  rd = getServletContext().getRequestDispatcher("yourPage.jsp");
rd.forward(request,response);

You can also pass the attribute using the URL in dispatcher :

RequestDispatcher  rd = getServletContext().getRequestDispatcher("yourPage.jsp?message=some message");
    rd.forward(request,response);

Also you can share info using session object.

session.setAttribute("message","your message");

And then retrieve it in jsp using:

String message=(String)session.getAttribute("message");

Upvotes: 0

jiacheo
jiacheo

Reputation: 308

replace

RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");

with

RequestDispatcher reqDispatcher = request.getRequestDispatcher("javaPapers.jsp");

Upvotes: 0

Related Questions