1

If I am passing a String into an included jsp file:

        <jsp:include page="groop_checkbox_with_header.jspf">
            <jsp:param name="mapName" value="investQuestionsExperience"/>
        </jsp:include>

I can use it as ${param.mapName} in html and jsp code. But how can I use that param.mapName in the java code in that included file in <%..%> braces? For I can't simply write:

<% String mapName = param.mapName; %>

the variable "param" is marked as unknown.

There are many questions that looks similar, but all of them are about calling through creation a new request. And here we have the jsp variable and java code awaiting for it on the same page. I can look at java expression value in the jsp code by ${}. But how can I make the backward operation?

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • Possible duplicate of [Access Get parameter with a scriptlet](https://stackoverflow.com/questions/280714/access-get-parameter-with-a-scriptlet) – Alan Hay Aug 24 '17 at 14:10
  • @AlanHay the mentioned question asks about SOME access from the included JSP. I don't need SOME access. The access from jsp is trivial and I don't need it. What is the access from the java code? The questions are different. – Gangnus Aug 24 '17 at 14:21
  • 1
    What do you mean by *SOME access*? What do you mean by a *backwards operation*? From your question you appear to want the value of a request param in a Java code scriptlet which as stated in the linked question would be done as follows: `<% String mapName = request.getParameter("mapName"); %>`. – Alan Hay Aug 24 '17 at 14:27
  • @AlanHay Could you put it as an answer, please? – Gangnus Aug 24 '17 at 14:31
  • @AlanHay some neans, that the author of the mentioned question was uninterested in the place where he gets the value of the parameter - html code, jsp code, java code. Maybe, you are a great expert in JSP, and for you it is a trivial difference, but for me, who is making his first refactoring of jsp product, it is not. Don't forget, that novices and experts sees the same thing on different abstraction levels. – Gangnus Aug 24 '17 at 14:35

2 Answers2

1

The scope of the new parameters is the jsp:include or jsp:forward call; that is, in the case of an jsp:include the new parameters (and values) will not apply after the include ends. https://docs.oracle.com/cd/E19575-01/819-3669/bnajd/index.html

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • 1
    You have put very, really very useful reference that explains everything. Parameters will be ADDED to request AT calling the included file and removed AFTER the include, in other words, after the "return" from include. It is just what I need to know. There appears another interesting question - how to work with several values of one parameter, accumulated by sequenced calls, but that is another question. – Gangnus Aug 25 '17 at 08:56
0

Actually what you need to do is use the request to get the Attribute, not the Parameter, so your scriptlet will look something like this:

 <% Object mapNameObj = request.getAttribute("mapName");
    String mapName = mapNameObj != null ? (String) mapNameObj : null;
 %>

Hope this helps

jcoder8
  • 163
  • 1
  • 9
  • He wants a *Parameter* **not** an *Attribute*. – Alan Hay Aug 24 '17 at 15:13
  • @AlanHay so, parameter or attribute? – Gangnus Aug 24 '17 at 15:20
  • If your intention is to receive data from a form and process it on your backend @AlanHay's first proposal is the correct one: <% String mapName = request.getParameter("mapName"); %> If you want to pass data to the front end <% Object mapNameObj = request.getAttribute("mapName"); %> – jcoder8 Aug 24 '17 at 17:57