0

I want to when click on log out tab then redirect on login page after logout. This is not go on login page.

Home.jsp

<ul><li><a href="http://pushkalit.in/logout.jsp"> Logout</a></li></ul>

logout.jsp

<% try {

    if(session.getAttribute("Username") != null) {
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Cache-Control","no-store");
        response.setHeader("Pragma","no-cache");
        response.setDateHeader ("Expires", 0);
        session.invalidate();
        response.sendRedirect("http://pushkalit.in/hrlogin.jsp");
    }
    else {}
}
catch(Exception ex) {
    out.print(ex);
}
%>
HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
Deepak Singh
  • 460
  • 2
  • 7
  • 19

2 Answers2

1

i think it would be easier if you just put the logout login inside servlet instead of an jsp. And don't forget to do a

session.invalidate()

EDIT (further detail): something like this :

package com.my.package;

public class LogoutServlet extends HttpServlet {
  protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

    response.setHeader("Cache-Control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    request.getSession().invalidate();
    response.sendRedirect(request.getContextPath() + "/login.jsp");
  }
}

In your servlet.xml (you could also do it with an annotation) configure it like this:

<servlet>
    <servlet-name>logoutServlet</servlet-name>
    <servlet-class>com.my.package.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>logoutServlet</servlet-name>
    <url-pattern>/logout</url-pattern>
</servlet-mapping>

And then finally your link goes to:

<a href="http://pushkalit.in/yourapplication/logout">Logout</a>
fGo
  • 1,146
  • 5
  • 11
0

URL mapping in anchor tag seems to be wrong.

It is linked to logout.jsp but you have the redirect code in Hrlogout.jsp.

Change:

<a href="http://pushkalit.in/logout.jsp"> Logout</a>

To:

<a href="http://pushkalit.in/Hrlogout.jsp"> Logout</a>
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • What was happening after clicking link? Where did you get landed? – Ravinder Reddy Mar 24 '14 at 09:47
  • first you need to read this: http://stackoverflow.com/questions/14018215/what-is-url-pattern-in-web-xml-and-how-to-configure-servlet and also, why is not working? – fGo Mar 25 '14 at 14:24
  • DeepakSingh: You need to understand configuring servlet mapping. Read @fGo's answer for more details – Ravinder Reddy Mar 25 '14 at 17:00
  • i have solved logout problem .http://stackoverflow.com/questions/22496903/set-day-of-month-to-1-for-new-user please help me how to do – Deepak Singh Mar 26 '14 at 06:15