English 中文(简体)
转机
原标题:sendRedirect not redirecting

I m trying to create a Java controller servlet to log users out of the session. I know there s 2 methods, one by using sendRedirect and the other is RequestDispatcher. In my case, I want to send them to a page outside of the domain, which (to my limited Java knowledge) requires me to use sendRedirect.

然而,Im 带走了302英尔,该网页没有改用。 我已经尝试了一个教学版本,并且工作,但当我在我的办公室内执行时,我会回过错,不转头。

我希望有人能够向我指出正确的方向。

使用Im的代码如下。 使用净户模板的Im:


    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();
    }
}

Edit: I m 称该服务器通过<a href> tag。 Servlet name islogOut.java.

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

The doGet and doPost callsRequest.

    @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);
}
问题回答

Standard redirection of the response is a reference within your application. To solve this I ve created a simple HTML page with meta data refresh to the external page: http://www.w3schools.com/tags/tag_meta.asp
Example:
<meta HTTP-EQUIV="refresh" CONTENT="2; URL=http://www.google.be;">

如果自动转口工作失败,确保有简单的超链接进入外部网页。

一旦测试,Serlet的答复被转至我自己的前导页和voila。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签