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