Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript变量值从JSP页面传递到Servlet?_Javascript_Jsp_Servlets_Session Variables - Fatal编程技术网

如何将javascript变量值从JSP页面传递到Servlet?

如何将javascript变量值从JSP页面传递到Servlet?,javascript,jsp,servlets,session-variables,Javascript,Jsp,Servlets,Session Variables,我有一个名为menu.JSP的JSP页面。在此页面上,用户必须从菜单中选择一个选项。我使用JavaScript在每个菜单项上实现了onclick(“myredirect(choiceValue)”)。t此功能使用确认弹出框检查用户是否要继续当前选择。如果用户选择ok,则返回true,并将其存储在check变量中。我想将其作为choiceValue发送到名为CheckLogin.java的servlet,该servlet检查用户是否使用会话跟踪登录。如果用户没有登录,他将被重定向到login.js

我有一个名为
menu.JSP
的JSP页面。在此页面上,用户必须从菜单中选择一个选项。我使用JavaScript在每个菜单项上实现了
onclick(“myredirect(choiceValue)”)
。t此功能使用确认弹出框检查用户是否要继续当前选择。如果用户选择ok,则返回true,并将其存储在
check
变量中。我想将其作为
choiceValue
发送到名为
CheckLogin.java
的servlet,该servlet检查用户是否使用会话跟踪登录。如果用户没有登录,他将被重定向到
login.jsp
页面,否则他将被重定向到
instructions.jsp
页面

我已经实现了下面的全部内容。我想在整个会话中使用
choiceValue
,但它没有像我预期的那样工作

menu.jsp

<script type="text/javascript">
<!--
    function myredirect(choiceValue){
    var check=window.confirm("You have Choosen "+choiceValue+" !\n Do you want to continue?");
        if(check){
            window.location.replace("checklogin.do?choice="+choiceValue);
            //window.location.replace("instructions.jsp?choice="+choiceValue);
        }
    }
//-->
</script>

我检查了你的代码,它对我来说运行良好。在myredirect()中,choiceValue是否会出现?你面临的isuue是什么?你忘了详细说明“它不起作用”。如果你的轮胎瘪了,你也不会告诉汽车机械师“我的车坏了”,是吗?
public class CheckLogin extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //cache controlling
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires",0);
        response.setHeader("Cache-Control", "no-cache");

        HttpSession session=request.getSession(false);

        if(session==null)                                   //checking existing session
            response.sendRedirect("login.jsp");
        else if(session.isNew())
            response.sendRedirect("login.jsp");
        else{
            //setting choiceValue to session object so that it can be used further
            String choiceValue=request.getParameter("choice");
            session.setAttribute("choice",choiceValue);
            response.sendRedirect("instructions.jsp");      //redirecting to instruction.jsp page
        }
    }