我对编程很陌生,需要一些关于注销的指导。 我正在制作一个基于Web的应用程序,它可以在后端服务器上运行。我的登录是 工作正常,但不确定如何实施注销。任何帮助都很大 赞赏。以下是登录的servlet代码:
protected void processRequest(HttpServletRequest request
, HttpServletResponse response)
throws ServletException, IOException {
String address = "";
String operation = request.getParameter("operation");
// System.out.println(operation);
HttpSession session = request.getSession(true);
UserModel u = null;
if (operation != null) {
if (operation.equalsIgnoreCase("login")) {
u = new UserModel();
String Username = request.getParameter("username");
String Password = request.getParameter("password");
System.out.println("userModel" + u);
u = u.find(Username, Password);
System.out.println("i" + u.getUsername());
if (u.getUsername() != null) {
session.setAttribute("user", u);
address = "Homepage.jsp";
} else {
address = "InvalidLogin.jsp";
}
这是我的JSP页面代码:
<form id="login1" action="Servlet">
<input type="hidden" name="operation" value="login" />
<h1>Log In</h1>
<fieldset id="inputs">
<input id="username" type="text" placeholder="Username"
name="username"autofocus required>
<input id="password" type="password" placeholder="Password"
name="password" required>
</fieldset>
<fieldset id="actions">
<button type="submitm" value="Login"
class="IR" id="IRButton"><em></em></button>
</fieldset>
</form>
<form action="Register1.jsp">
<button type ="submitn" value="Register" class="IR2"
id="IRButton2"><em></em></button>
</form>
....如果有人能在两个页面中告诉我我需要什么代码,那就太棒了!谢谢
答案 0 :(得分:1)
试试这个......请考虑Session Management
进行登录和退出操作
<强>的web.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>ServletLogic</servlet-name>
<servlet-class>model.ServletLogic</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletLogout</servlet-name>
<servlet-class>model.ServletLogout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLogic</servlet-name>
<url-pattern>/ServletLogic</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletLogout</servlet-name>
<url-pattern>/ServletLogout</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
<强>的index.jsp 强>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="ServletLogic" method="post">
USER:<input type="text" name="user" id="user"/><br>
PASS:<input type="password" name="pass" id="pass"/><br>
<input type="submit" value="Login"/>
</form>
</body>
</html>
<强>的login.jsp 强>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>My Page</h1>
<form action="ServletLogout" method="post">
<input type="submit" value="Logout"/>
</form>
</body>
</html>
ServletLogic.java // servlet
package model;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLogic extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String user=request.getParameter("user");
String pass=request.getParameter("pass");
String res;
if(user.equals("admin")&&pass.equals("admin"))
res="Success!!!";
else
res="Failure!!!";
request.getRequestDispatcher("login.jsp").forward(request, response);
try {
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
ServletLogout.java // servlet
package model;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLogout extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
request.getRequestDispatcher("index.jsp").forward(request, response);
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}