简体   繁体   中英

How to use variable defined in a scriptlet in the same jsp page

I have a jsp page namely User_Ref.jsp whcih has a datepicker .When I click on submit on that page ,it is redirected to another jsp page namely ref_time_current.jsp.In this jsp page I have used a scriptlet to hold the value which was selected by user from the calendar ie datepicker. The scriptlet is

<%
  Ref_log_current obj = new Ref_log_current();
  String str= request.getParameter("datepicker");
 ref.refarray_vac1(str);
%>

Now I want to use this str variable defined in scriptlet in this way in same jsp page- <c:out value="${ref.refarray_vac1(str)}"></c:out>

But When I execute this,refarray_vac1(String Date) method which return list is showing an empty list.I think I'm using the str variable in wrong way.Please correct me.

JSTL has only access to scoped variable, not directly to scriplet ones. But you can easily create a page variable that way :

<%
  Ref_log_current obj = new Ref_log_current();
  String str= request.getParameter("datepicker");
  pageContext.setAttribute("str", str); // store str in page scope under name str
%>

You can then safely access ${str} in the JSP file.

In JSTL is not possible to use scriptlet variable in expression. Also you don't need to use scriptlet.

You need to import the bean class you create in JSP

<%@ page import="com.beans.Ref_log_current" %>

You can access parameters like this

<jsp:useBean id="ref" class="com.beans.Ref_log_current" scope="page"/>
<c:out value="${ref.refarray_vac1(param.datepicker)}"/>

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