という名前のJSPページがありますmenu.jsp
。このページでは、ユーザーはメニューから選択肢を選択する必要があります。onclick("myredirect(choiceValue)")
JavaScriptを使用して各メニュー項目に実装しました。この関数は、確認ポップアップボックスを使用して、ユーザーが現在の選択を続行するかどうかを確認します。ユーザーが[OK]を選択すると、trueが返され、変数に格納されcheck
ます。これを、ユーザーがセッション追跡を使用してログインしているかどうかをチェックするchoiceValue
という名前のサーブレットに送信したいと思います。CheckLogin.java
ユーザーがログインしていない場合は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>
CheckLogin.java
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
}
}