0

ユーザーをセッションからログアウトさせるJavaコントローラーサーブレットを作成しようとしています。sendRedirectを使用する方法とRequestDispatcherを使用する方法の2つの方法があることを私は知っています。私の場合、ドメイン外のページにそれらを送信したいので、(私の限られたJavaの知識では)sendRedirectを使用する必要があります。

ただし、エラー302が発生し、ページがリダイレクトされません。チュートリアルバージョンを試しましたが、動作しますが、サーブレット内に実装すると、エラーが返され、リダイレクトされません。

誰かが私を正しい方向に向けてくれることを願っています。

私が使用しているコードは以下の通りです。私はnetbeansテンプレートを使用しています:


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        HttpSession session = request.getSession(false);
        if (session == null) {
            System.out.println("Invalid");
            response.sendRedirect("http://www.google.com");
            return;
        } else {
            System.out.println("Invalidated");

            session.invalidate();
            response.sendRedirect("http://www.google.com");
            return;
            /*
            String url = "/logout.jsp";

            ServletContext sc = getServletContext();
            RequestDispatcher rd = sc.getRequestDispatcher(url);
            rd.include(request, response);

             */
        }
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet LogOut</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet LogOut at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
         */
    } finally {            
        out.close();
    }
}

<a href>編集:タグを介してサーブレットを呼び出しています。サーブレット名はLogOut.javaです。

        <div data-role="header" data-position="fixed">
            <h1>Menu</h1>
            <a href="LogOut" data-theme="i">Log Out</a>
        </div>

doGetとdoPostはprocessRequestを呼び出します。

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/** 
 * Handles the HTTP <code>POST</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}
4

1 に答える 1

0

応答の標準リダイレクトは、アプリケーション内で参照されます。これを解決するために、メタデータを外部ページに更新する単純な HTML ページを作成しました: http://www.w3schools.com/tags/tag_meta.asp
例:
<meta HTTP-EQUIV="refresh" CONTENT="2; URL=http://www.google.be;">

自動転送が失敗した場合に備えて、外部ページにアクセスするための簡単なハイパーリンクを用意してください。

テストすると、サーブレットからの応答が自分の転送ページにリダイレクトされ、出来上がりました...

于 2012-05-02T10:16:12.647 に答える