0

I am trying to pass two values (intMethod and SpotDays) from SourceServlet to a JSP named CcySorting.jsp.

I am using the setRequestAttribute() method to set the values at servlet end and using getRequestAttribute() at JSP end to receive the values. But I am receiving null values in JSP. My code is below. Please have a look at it and suggest the possible reason. I have tried a lot, but in vain.

I am also providing my JSP and servlet folder structure.

My folder structure:

  • JSP path: application.war\CcySorting.jsp
  • Servlet path: application.war\WEB-INF\classes\SampleServlet.class

My entries in Web.xml:

<servlet>
  <servlet-name>SampleServlet</servlet-name>
  <servlet-class>SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>SampleServlet</servlet-name>
  <url-pattern>/SampleServlet</url-pattern>
</servlet-mapping>

My JSP Files:

  • CcySorting.jsp

    function searchData(brn,ccy)
    {
        var frmObj  =getUserFormObj('window','div0','form0');
        window.open("/SampleServlet?BrnName="+brn+"&Currency="+ccy);    
        var intMethod= <%= request.getAttribute("intMethod1") %>;
        var spotDay = <%= request.getAttribute("SpotDays1") %>;
        alert("data from servlet"+intMethod+"and spot"+spotDay1);
    }
    
  • SampleServlet.java

    public class SampleServlet extends HttpServlet{     
    
        public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
    
            // Some code to fetch the data from database and store in two variable intm and spot
            int int=2
            int spot=3              
            request.setAttribute("intMethod1",int);
            request.setAttribute("SpotDays1", spot);
            RequestDispatcher rd=request.getRequestDispatcher("/CcySorting.jsp");
            rd.forward( request, response ) ;
        }
    }
    
informatik01
  • 16,038
  • 10
  • 74
  • 104
user2072307
  • 1
  • 1
  • 1
  • 1
    As a side note: [**using scriplets in JSP is highly discouraged**](http://stackoverflow.com/a/3180202/814702). You hardly ever need to override `service()` method: just override more concrete `doXXX()`method. Read here: [**Should I override service() method?**](http://stackoverflow.com/a/6822041/814702) – informatik01 Feb 14 '13 at 23:09

2 Answers2

0

Hmm.. that seems everything correct other than retrieving data :

change your jsp receivers to :

var intMethod= '<%= request.getAttribute("intMethod1") %>';
var spotDay = '<%= request.getAttribute("SpotDays1") %>';
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Using scriplets is not encouraged. You can try using this in your JS function.

var intmethod='${intmethod1}';
var spotday='${SpotDays1}';

Also try scriplets in your HTML section and see what values are you retrieving.

intmethod=<%= request.getAttribute("intMethod1")%>;
spotsday=<%= request.getAttribute("SpotDays1") %>;
Tirthankar
  • 11
  • 3