1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MES
  • 132
  • 1
  • 14
  • 2
    Why are you mixing JSTL and Scriptlets? Use either of them precisely the former. (Use of Scriplets is discouraged for over a decade). JSTL variables , on the other hand are attributes of some kind (page context by default). If you want to access Scriplets variables in JSTL, you will need to put them into some attributes with a scope like `pageContext.setAttribute("datepicker");`, `request.setAttribute("datepicker");` (session or servlet context depending upon the need) and access them in JSTL as attributes with the scope which they have been associated with. – Tiny Feb 10 '15 at 12:09

2 Answers2

3

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I already did that.I have to pass this str as an argument in a method as mentioned in question. – MES Feb 10 '15 at 11:40
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)}"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • ,Actually when I tried to print str value as **** nothing is being shown .It means values from jsp page is not being passed into this jsp page. – MES Feb 10 '15 at 11:43
  • sorry date is printing,but list is being empty. – MES Feb 10 '15 at 11:46
  • What has been printed if ``? – Roman C Feb 10 '15 at 11:47
  • it prints the date which I have chosen for eg-value from calender is 10/10/2014 . Actually its showing an error that **Conversion failed when converting date and/or time from character string.** As the date selected from calender is of date type and I put it into a string variable.This may be a cause for empty string.Please correct me. – MES Feb 10 '15 at 11:54
  • This code doesn't convert a string to `Date`, but your method may be, post the code of the method impl. – Roman C Feb 10 '15 at 11:57
  • yup,how to do that. Moreover in my sql query I have supplied the date and time as **b.logtime between'+date+f+' and '+date+t+'"** where date is coming from jsp whcih I have selected from calender and f and t are strings such as **String f="00:00:00"; String t="23:59:59";**. – MES Feb 10 '15 at 12:01
  • 1
    in your sql you can use date function to convert a string to date, or you can parse `Date` and use it in SQL as parameter, but it's off-topic. What ever the choice you use is up to you. – Roman C Feb 10 '15 at 12:07