0

我正在尝试将两个值(intMethodSpotDays)传递SourceServlet给名为CcySorting.jsp.

我正在使用该setRequestAttribute()方法在 servlet 端设置值并getRequestAttribute()在 JSP 端使用来接收值。但是我在 JSP 中接收到空值。我的代码如下。请查看它并提出可能的原因。我已经尝试了很多,但都是徒劳的。

我还提供了我的 JSP 和 servlet 文件夹结构。

我的文件夹结构:

  • JSP 路径:application.war\CcySorting.jsp
  • 小服务程序路径:application.war\WEB-INF\classes\SampleServlet.class

我的条目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>

我的 JSP 文件:

  • 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 ) ;
        }
    }
    
4

2 回答 2

0

不鼓励使用 scriplets。您可以尝试在您的 JS 函数中使用它。

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

还可以在您的 HTML 部分中尝试 scriplets,看看您正在检索哪些值。

intmethod=<%= request.getAttribute("intMethod1")%>;
spotsday=<%= request.getAttribute("SpotDays1") %>;
于 2014-07-08T07:06:24.447 回答
0

嗯..除了检索数据之外,这似乎一切都正确:

将您的jsp接收器更改为:

var intMethod= '<%= request.getAttribute("intMethod1") %>';
var spotDay = '<%= request.getAttribute("SpotDays1") %>';
于 2013-02-14T17:23:59.510 回答