首页 > 解决方案 > 如何在 javascript 函数中设置 request.getSession().setAttribute(obj, obj)

问题描述

我想通过request.getSession().setAttribute(obj, obj)将 javascript 变量的值设置到 jsp 请求/会话中,以便以后可以使用它。

我在下面写了一段代码,

function setVal(cache){
    var vo = cache;
    String voStr = vo;
    <% request.getSession().setAttribute("single_"%>+voStr+<%,  "true"); %>
}

在这里,我通过按钮的 onclick 事件调用了 ' setVal ' 函数,并且语法中没有显示错误,所以我希望语法是正确的。但是当我运行 jsp 时,它会在控制台中导致JasperException / ServletException 。

javax.servlet.ServletException: Unable to compile class for JSP: 

An error occurred at line: 46 in the jsp file: /home.jsp
The method setAttribute(String, Object) in the type HttpSession is not applicable for the arguments (String)

任何人都可以建议,我如何通过javascript函数在request.getSession().setAttribute中设置值。

标签: javascriptscriptlet

解决方案


推荐阅读