Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
Here Is my login.jsp code

HTML
<%@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?
HTML
<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

Java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		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) {
			// TODO Auto-generated catch block			
			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();
		}
	}
}
Posted
Updated 7-Jul-13 17:17pm
v2

1 solution

Do the following steps..

1. First remove the onclick event from login/logout button.
2. Set the form-action with the url pattern .
I don't know what is the urlpattern for your app ("main/Serveletr" or "login").
 
Share this answer
 
Comments
mali_angel 8-Jul-13 3:34am    
thank u..!
Shubhashish_Mandal 8-Jul-13 3:53am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900