Results 1 to 6 of 6
  1. #1
    sandy004.u is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default logout by disabling back button

    hello friends...
    i am a new user...

    i am creating a web appliction in which i want
    if the user is been logout once then he should not be able to access the home page by simply pressing the back button...
    i tried different things but it didn't work...


    pls help...

    login.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>LOGIN PAGE</title>
    </head>
    <body>
    <form action="Loginpage">
    <input type="text" name=username value="username"><br/>
    <input type="password" name=password value="pass"><br/>
    <input type="submit" name= hitme value="hit me"><br/>


    </form>

    </body>
    </html>

    servlet
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    HttpSession session =request.getSession();

    String name = request.getParameter("username");
    request.setAttribute("uname",name);
    out.print("<html>");
    out.print("<head>");
    out.print( "<style type='text/css'>");
    out.print("h3 {color:sienna;}");
    out.print("h3 {margin-left:20px;}");
    out.print("</style>");
    out.print("</head>");
    out.print("<h1>hi </h1>");
    out.print("<h2> welcome </h2>");
    out.print("<h3>");
    out.print(name);
    out.print("</h3>");
    out.print("</html>");
    RequestDispatcher rd= request.getRequestDispatcher("/home1.jsp");
    rd.forward(request, response);
    }
    }

    home page

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1> 1st page</h1>
    <%
    request.getSession(true);
    String val = (String)request.getAttribute("uname"); %>
    <h2><%= val%></h2>

    <a href="/LoginForm/logout.jsp" > logout</a>

    </body>
    </html>


    logout.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>


    <%
    request.getSession(true);
    System.out.println("1");
    session.invalidate();
    System.out.println("2");
    response.sendRedirect("logout2.jsp");
    %>

    </body>
    </html>


    logout2.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%request.getSession(true); %>
    <h1> Successful log out</h1>

    </body>
    </html>



    so pls help me friends...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    First off, separate code from diaplay. You have html in your servlets, and Java in your JSPs. This is not good practice.

    You'll want a login servlet that handles the login, and forwards to the home page. It doesn't write to the out stream!

    You'll want a logout servlet that logs you out and then forwards to a jsp page that says you've logged out. You don't put the log out code in the JSP.

  3. #3
    sandy004.u is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default logout by restricting the user to use back button..

    ok i have removed all the html code from the servlets and tried to minimise the java code in jsp pages..
    thanks for the advise ...

    but my real question of how to restrict the user to goto the home page by pressing the back buttom is still there..

    can any one give me code how to
    invalidate the user in tho logout page and again if he tries to press the back button, the code should direct it to the login page...

    pls pls pls help me guys...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Check if the user is logged in?
    That's what you usually do.
    If the user isn't logged in then redirect.

  5. #5
    sandy004.u is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default give me some code..

    pls give some code brother...


    i am completely messed up..wid my code...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    All I can suggest is read up on it all.
    I'm not a code factory...at least not unless you pay me...:)

Similar Threads

  1. Replies: 2
    Last Post: 01-30-2010, 05:14 AM
  2. problem with back button of the browser.
    By shivakumari in forum Java Servlet
    Replies: 2
    Last Post: 03-25-2009, 07:41 AM
  3. Disabling a button at EOF
    By dbashby in forum New To Java
    Replies: 1
    Last Post: 03-10-2009, 02:37 PM
  4. Back button
    By Tokajac in forum Web Frameworks
    Replies: 1
    Last Post: 11-27-2008, 05:27 AM
  5. [SOLVED] Disabling a button not working
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 04:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •