Here Is my login.jsp code
<%@page import="com.mobitel.bankdemo.web.GetSession"%>
<%@ 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>
<form name="input" action="login" method="post">
<input type="submit" value="LogOut" onclick= <a href="main/Servelet"</a> >
<input type="hidden" name="operation" value="logout">
<h1>Login</h1><br>
UserName: <input type="text" name="txtUname"><br>
Password: <input type="password" name="txtPwrd"><br>
<input type="submit" value="Login" onclick= <a href="main/Servelet"</a> >
<input type="hidden" name="operation" value="login">
<p><%=request.getAttribute("loginMsg") %></p>
<a href="Register.jsp">Register</a>
</form>
</body>
</html>
My question is when i put logout code inside this page it did not work.when i remove it works for login.what is the error here?
<input type="submit" value="LogOut" onclick= <a href="main/Servelet"</a> >
<input type="hidden" name="operation" value="logout">
Both login and logout codes are in UserServlet.java file
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if(operation!=null && operation.equalsIgnoreCase("login")){
loginDetail(request,response);
}else if(operation!=null && operation.equalsIgnoreCase("login")){
logoutSession(request,response);
}
}
private void loginDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
User u = new User();
UserService us =new UserServiceImpl() ;
String Uname = request.getParameter("txtUname");
String Pwrd = request.getParameter("txtPwrd");
u.setUname(Uname);
u.setPwrd(Pwrd);
System.out.println(Uname+""+Pwrd);
try {
if(us.Userlogin(u.getUname(),u.getPwrd())){
String message = "Thank you, " + Uname +"..You are now logged into the system";
HttpSession session = request.getSession(true);
session.setAttribute("username", Uname);
GetSession gs = new GetSession();
gs.doPost(request, response);
session.setMaxInactiveInterval(5*60);
response.setContentType("text/html");
request.setAttribute("message", message);
request.getRequestDispatcher("/Menu.jsp").forward(request, response);
}else {
String message = "You have to register first or check Your user name password again!";
request.setAttribute("loginMsg", message);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Login.jsp");
rd.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void logoutSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try{
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + "/Login.jsp");
}catch(Exception e){
e.printStackTrace();
}
}
}