I created a login form in jsp. Also made the session save system that when the user will log in his username will be visible in the welcome page inside a label tag. I used this code for this -
In the login page :
request.getSession().setAttribute("username",username);
And in the welcome page :
out.print(request.getSession().getAttribute("username"));
Also have an log out link like anchor tag which's href="/login.jsp"
Now I comes to the problem. When I first time login with the username "a" it successfully shows "a" in the welcome page. But when I logout and login again with username "b" then also it shows "a" in the welcome page. When I refresh the browser then it shows "b". I can try a method that I can make a code that will refresh the browser after first refresh. But my page will be too heavy in future. For that this will be not a standard way right?
I found a solution that session.invalidate(); but I tried this code inside anchor tag but is of no use.
Kindly help!
Here is the logout page code -
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/basic.js"></script>
<title>Dashboard || XCEL-ERP</title>
</head>
<body>
<div class="dashboard_sidebar" id="dashboard_sidebar">
<ul>
<li class="sidebar_item"><a href="#">HOME</a></li>
<li class="sidebar_item"><a href="#">RECIPE & COSTING</a></li>
<li class="sidebar_item"><a href="#">PLANNING</a></li>
<li class="sidebar_item"><a href="#">PRODUCTION</a></li>
<li class="sidebar_item"><a href="#">ACCOUNT</a></li>
<li class="sidebar_item"><a href="#">OVERVIEW</a></li>
</ul>
</div>
<div class="dashboard_header" id="dashboard_header">
<div class="dashboard_hamburger" onclick="sidebarFunction()">
<span></span>
<span></span>
<span></span>
</div>
<div class="logo">ERP</div>
<div class="user">
<img src="https://www.dropbox.com/s/s3wcx1zot0gp3fe/user_icon.png?raw=1" alt="user" onclick="userProfileFunction()">
<label>
<%
out.print(request.getSession().getAttribute("username"));
%>
</label>
<div class="userProfileBox" id="userProfileBox">
<img src="https://www.dropbox.com/s/zq3d2dz63y3y4ct/man_pic.jpg?raw=1" alt="Profile_img">
<div class="userName"> <% out.print(request.getSession().getAttribute("username")); %> </div>
<div class="desg_Section"> Costing Officer - Wash </div>
<div class="authorization">User has authorization of : </div>
<button>Log Out</button>
</div>
</div>
<a href="login.jsp">Log Out</a>
</div>
</body>
</html>
And here is the login connection code which is connected to a form method post -
<%@ page import = "java.sql.*"%>
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/erp";
String DB_USER = "root";
String DB_PASS = "";
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
String username = request.getParameter("username");
String password = request.getParameter("password");
String query = "SELECT * FROM userdata WHERE UserName = '".concat(username).concat("' AND Password = '").concat(password).concat("'");
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
stm = conn.createStatement();
rs = stm.executeQuery(query);
Boolean match_result = rs.isBeforeFirst();
if(match_result == true){
getServletContext().getRequestDispatcher("/dashboard.jsp").forward(request, response);
request.getSession().setAttribute("username",username);
}else{
out.print("Sorry, username or password error!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
rs.close();
stm.close();
conn.close();
} catch(Exception e){
out.println(e);
}
%>