Vote count:
0
I am doing a simple login application, which has should have two pages. 1. login page 2.homepage
When the user press the login button, the user should be landed in homepage. And the important thing is when the user press the back button in the browser the user SHOULD NOT END UP IN THE LOGIN PAGE(since he hasn't logged out). and in the same way, when user press the logout button in the homepage, the user should be landed in the login page. and importantly when the user press the back button in the browser, the user should not be landed in the hompage.
I have developed a working code for the above scenario. The way i have used to maintain the session is disabling the cache memory in the response header. so that when the browser receives the page, it does not save it in the cache. so when the user press the back button in the browser, the browser the sends the http request to the server.
But i think the above method i used is not scalable since i have to add the no-cache in the response header of every page. If i use an app having 100 page. Its obviously not good. I want some other way. I would very much appreciate suggesstions.
Thanks for taking time to read this.
here is my code.
LogAppServlet
@SuppressWarnings("serial")
public class LogAppServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
//int flag=0;
String username=req.getParameter("username");
String password=req.getParameter("password");
PrintWriter write=resp.getWriter();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Filter filterByName =
new FilterPredicate("userName",
FilterOperator.EQUAL,
username);
Query q = new Query("UserDetails").setFilter(filterByName);
PreparedQuery pq = datastore.prepare(q);
for (Entity result : pq.asIterable()) {
String uName = (String) result.getProperty("userName");
String pWord = (String) result.getProperty("passWord");
if(username.equals(uName)&& password.equals(pWord))
{
HttpSession session=req.getSession();
session.setAttribute("user", username);
session.setAttribute("pword", password);
session.setAttribute("loggedin","yes");
//flag=1;
//System.out.println("match found");
//resp.sendRedirect("homepage.jsp");
write.println("true");
}
}
}
}
Login jsp
</head>
<body background="/images/home.jpg">
<%
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "private, no-cache, no-store, must-revalidate" );
response.setDateHeader( "Expires", 0 );
%>
<%if(session.getAttribute("loggedin")==null)
{%>
<br><br><br><br><br>
<h3 align="center"><font>Login Application</font></h3>
<br><br>
<div id="ajaxdiv" align="center">
<table>
<tr>
<td>
Username:<input id="usname" type="text" required autofocus><br>
</td>
</tr>
<tr>
<td>
Password: <input id="psword" type="password" required><br>
</td>
</tr>
<tr>
<td colspan="2">
<!--<button onclick="window.location.href='homepage.jsp'" id="btn1">Login</button>-->
<button id="btn1" >Login</button>
<td>
</tr>
</table>
</div>
<br><br><br><br><br>
<div align="center">
<p><strong>Sign Up</strong></p>
<form name="sform" action="signapp" method="post" onsubmit="return validateform();">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="uname" placeholder="Enter Your name" >
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="pword" placeholder="Enter password" >
</td>
</tr>
<tr>
<td>
e-mail:
</td>
<td>
<input type="text" name="mail" placeholder="Enter your email id">
</td>
</tr>
<tr>
<td>
Phone:
</td>
<td>
<input type="text" name="phone" placeholder="Enter your phone number">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="sign up">
</td>
</tr>
</table>
</form>
</div>
homepage 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://ift.tt/kTyqzh">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body background="images/bgimg.jpg">
<%
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "private, no-cache, no-store, must-revalidate" );
response.setDateHeader( "Expires", 0 );
%>
<% String user=(String)session.getAttribute("user");
if(user==null)
{
response.sendRedirect("login.jsp");
}
else
{
%>
<br><br><br><br><br>
<h3 align="center">Hello<font color="cadetblue"> <%=user%></font>. Logged in successfully..!!</h3>
<br><br><br><br><br>
<p align="center"><a href="logoutjsp.jsp">Logout</a></p>
<%} %>
</body>
</html>
I need a optimal and simple way to maintain a session in a simple login application.
Aucun commentaire:
Enregistrer un commentaire