1

I have a JSP page which is not seeing any of the request parameter values when displayed. Originally I tried with passing the parameters from a Servlet, which did not work. Just as a test I also tried calling that JSP from a form on an html page.

What I do in Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String sampleValue = sampleModel.getMyValue();
    request.setAttribute("param", sampleValue);

    RequestDispatcher view = request.getRequestDispatcher("samplePage.jsp");
    view.forward(request, response);
}

How I call JSP from an HTML page through a form with hidden fields:

<div>
    <form action="samplePage.jsp" method="post">
        <input name="param" type="hidden" value="sampleValue"/>
        <input type="submit" value="Update">
    </form>
</div>

Finally what I have on the JSP:

<body>
    <p>Some info: ${param}</p>
</body>

As I said the problem is the value of the request attribute "param" which is lets say "sampleValue", does not get rendered on the page.

I have seen lots of examples how this is done and I think my code is correct. Is there any other reason why this may not be working? I am running a maven project with Tomcat 8.5.

EDIT: What I have found out so far is that the problem is not that the Expression language is not working. The request attribute just has no value when it arrives at the JSP.

PetarMI
  • 390
  • 1
  • 7
  • 15
  • 2
    try with `param.param` https://stackoverflow.com/questions/26747831/jsp-expression-language-get-parameter – Oleg Nov 06 '17 at 19:13
  • Thank you for the suggestion, but this doesn't work. The attribute is a simple value and as such should be accessed with ${param}. The ${param.param} is used for nested properties. – PetarMI Nov 06 '17 at 19:23
  • 1
    No, the first param is to access request parameters the second param is because you called it param. – Oleg Nov 06 '17 at 19:24
  • Are you posting to JSP instead of your servlet? Here the action in jsp does not look like a servlet path: `
    `.
    – tsolakp Nov 06 '17 at 19:47
  • Yes I am. But as I mentioned I do that just to test. Anyway when the parameters are sent from the Servlet through the RequestDispatcher it is again not working - which should be the case since the code is correct. – PetarMI Nov 06 '17 at 19:50
  • are you sure the servlet called? ( mke some "print" to trace/ or run on debugging mode) – Enjy Nov 06 '17 at 21:10

4 Answers4

3

Please ensure that isELIgnored is false in your jsp page.use bellow tag at the top of your jsp.

<%@ page isELIgnored="false" %>

also you can ensure this by ${2 * 4} output is print as 8 on JSP.

Sunil
  • 437
  • 3
  • 11
0

Your form is using method=post. Your Servlet code should be located on the doPost method instead of doGet.

Rafael Odon
  • 1,211
  • 14
  • 20
  • Yes, I know, but this is not the problem. I just used the form as another way of calling the JSP page. No matter if we do a post or a get, it is still not seeing the parameter. – PetarMI Nov 06 '17 at 19:16
  • If so, maybe the Expression Language is not working for some reason. Try to replace ${param} by the old fashioned <%= request.getAttribute("param") %> on your JSP. – Rafael Odon Nov 06 '17 at 19:24
  • Yes I tried that scriplet as well but now it just gave null as a result. Thank you for the suggestions by the way – PetarMI Nov 06 '17 at 19:29
  • Analysing your code I realized you are setting an "attribute" with some value. But notice that that information set in the hidden field on the HTML form is sent to the Servlet as a request paramater. – Rafael Odon Nov 06 '17 at 19:34
  • That's a fair point but in either of those cases the JSP should be able to see the values associated to the request parameter. – PetarMI Nov 06 '17 at 19:48
  • Dear @PetarMI, there are lots of missing pieces here. Gathering all the replies you gave here, I'm afraid the code snippets on the question are very disconnected. The form is directed to a JSP url, but you are expecting it to be answered by a Servlet. The form submit will trigger a doPost but your are implementing a doGet. Your JSP tries to print a request attribute value that comes from an "model object", but the values from forms are known to be set on a request parameter. It could be interesting if you edit your question with the full Serlvet code for example. – Rafael Odon Nov 06 '17 at 20:10
  • Okay you seem confused about the html page that submits a form to the JSP. I used that just to test if the JSP will get parameters from it. Anyway you can easily forget about the form completely. About the servlet code - the rest of the doGet is irrelevant to passing the parameters to a JSP. That is why I posted just the skeleton. – PetarMI Nov 06 '17 at 20:14
0

For Servlet case, replace ${param} in your samplePage.jsp with

<%=request.getAttribute("param")%>

For JSP case, replace ${param} in your samplePage.jsp with

<%=request.getParameter("param")%>
RLD
  • 1,867
  • 3
  • 15
  • 20
0

First, check whether variable sampleValue is capturing the string that you are passing from JSP like below

String sampleValue = sampleModel.getMyValue();
System.out.println(sampleValue);
Pang
  • 9,564
  • 146
  • 81
  • 122