无法从servlet将request.getParameter()写入jsp

时间:2015-11-11 06:42:11

标签: jsp servlets

我发送包含servlet数据的html表到我的jsp页面。我的servlet代码如下:

String html = "<table><thead><th>Serial</th><th>MAC</th></thead>"
              + "<tbody>
              + "<tr>1000234<td></td><td>9845HM98</td></tr>"
              + "<tbody>"
              + "</table>";

request.setAttribute("responselDetails", html);

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/jsp/Details.jsp");
rd.forward(request, response);

JSP:

<% if (request.getParameter("responsePeripheralDetails") != null) { %>
<%= request.getParameter("responsePeripheralDetails") %>
<% } %>

1 个答案:

答案 0 :(得分:2)

好像你混合了request.setParameter()request.setAttribute()

您正在servlet中设置request.setAttribute()并尝试通过jsp中的request.getParameter()访问它,这总是会给出nullPointer的异常。

快速修复:在jsp中将getParameter()更改为getAttribute()并完成操作: - )

<% if (request.getAttribute("responsePeripheralDetails") != null) { %>
<%= request.getAttribute("responsePeripheralDetails") %>
<% } %>