简体   繁体   中英

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>

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.

replace

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

with

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

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");

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM