MES
MES

Reputation: 122

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

I have a JSP page named User_Ref.jsp whcih has a datepicker. When I click on submit on that page, it is redirected to another JSP page named 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, i.e. 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.

Upvotes: 0

Views: 6885

Answers (2)

Roman C
Roman C

Reputation: 1

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)}"/>

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 149185

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.

Upvotes: 3

Related Questions