登录和注销同一页面不起作用 [英] Login and Logout same page did not work
本文介绍了登录和注销同一页面不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的login.jsp代码
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">
登录和注销代码都在UserServlet.java文件中
Both login and logout codes are in UserServlet.java file
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();
}
}
}
推荐答案
执行以下步骤..
1.首先从登录/注销按钮中删除onclick事件。
2.使用url模式设置表单操作。
我不知道你的应用程序的urlpattern是什么(main / Serveletr或login)。
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").
这篇关于登录和注销同一页面不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文