如何将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
}
}