Jump to content
Sign in to follow this  
parangjiwa_1

Problem ngan JSP session

Recommended Posts

Salam...hamba nak bertanya..kalau silap umah..sorry ekk..

hamba wat satu login/logout simple nag JSP guna session...mmg jadi..tetapi setelah logout, masih bleh masuk ke page sebelumnya bila kita click "back" kat browser..

camner ek...ahmab dh try solution2 dari pakcik Google dan Yahoo..tp still tak dpt...code2 hamba sprti di bawah:

sessionForm.jsp
Code:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<%
 String usernameFinal=(String) session.getAttribute("usernameSession");
  if(usernameFinal==null)
    {
      out.println("you are log out!!Session id is: "+session.getId());
    }
  else
    {
    out.println("you are still logged in.."+session.getId());
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="setSession.jsp">
Enter your name: <input type="text" name="userName">
<input type="submit" value="Submit">
</form>
</body>
</html>



setSession.jsp
Code:


<%
response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>

<%
String usernameInput=request.getParameter("userName"); // get userName value from form and store in variable...

out.println(usernameInput);

if((usernameInput==null)||(usernameInput==""))
 {
  //out.println("Name required!");
  %><jsp:forward page="sessionForm.jsp"/><%
 }
else
 {
 out.println("Session available but not yet set..Now setting session attributes");
 }

session.setAttribute("usernameSession", usernameInput); //create the session object with the name usernameSession
out.println("ok...done setting the session...the session id is still da same: "+session.getId());
%>

<body>
<p><a href="showSessionValue.jsp">Next to show session values</a><p>
</body>
</html>



showSessionValue.jsp
Code:


<%
response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<%
String usernameFinalZ=(String) session.getAttribute("usernameSession");
request.getSession();
out.println(usernameFinalZ);
if(usernameFinalZ==null)
//if(request.getAttribute("usernameSession")==null)
  {
  %><jsp:forward page="home.jsp"/><%
  }
else
  {
  out.println("HOI! Session still set. the session id is: "+session.getId());
  }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<input type="hidden" id="refreshed" value="no">
<%
String usernameFinal=(String) session.getAttribute("usernameSession");
//if(usernameFinal==null)
//{
//usernameFinal="";
//}
%>

 <p>Welcome: <%=usernameFinal%></p>
 <p>Session id is: <%=session.getId()%></p>
 <a href="sessionLogout.jsp">Logout</a>
</body>
</html>



sessionLogout.jsp
Code:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
  //String usernameFinal=(String) session.getAttribute("usernameSession");

  //ada 2 cara untuk logout!cara 1 dan cara 2. tp cara 1 lebih clear dan specific!


  //cara1
  //session.removeAttribute("usernameSession");
  //%><jsp:forward page="home.jsp"/><%
       

  //cara2
  session.invalidate();
  %><jsp:forward page="home.jsp"/>   
</body>
</html>



ok..harap dapat bantu..trimas y'all.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...