I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error

前端 未结 2 1099
眼角桃花
眼角桃花 2020-11-29 18:35

The following code causes an error:

 
 <%
   String resp = \"abc\";
   resp = resp + test;
   pageContext.setAt         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 19:24

    Scripts are raw java embedded in the page code, and if you declare variables in your scripts, then they become local variables embedded in the page.

    In contrast, JSTL works entirely with scoped attributes, either at page, request or session scope. You need to rework your scriptlet to fish test out as an attribute:

    
    <%
      String resp = "abc";
      String test = pageContext.getAttribute("test");
      resp = resp + test;
      pageContext.setAttribute("resp", resp);
    %>
    
    

    If you look at the docs for , you'll see you can specify scope as page, request or session, and it defaults to page.

    Better yet, don't use scriptlets at all: they make the baby jesus cry.

提交回复
热议问题