我有一个问题,我在这里从一个回答的问题中发现了一段代码。
这是我的代码:
public class LogoutServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
boolean logout = false;
RequestDispatcher rd = null;
Cookie[] c = req.getCookies();
String name;
for(Cookie cookie: c)
{
if(cookie.getName().equals("cUsername"))
{
name = cookie.getValue();
}
}
if(req.getParameter("logout") != null)
{
logout = true;
rd = req.getRequestDispatcher("loginpagina.jsp");
rd.forward(req, resp);
}
if(logout)
{
File inputFile = new File("C://Users/Jacco_000/Desktop/LoggedUser.txt");
File tempFile = new File("C://Users/Jacco_000/Desktop/tempfile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "cUsername";
String currentLine;
while((currentLine = reader.readLine()) != null)
{
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove))
{
writer.write(currentLine + System.getProperty("line.seperator"));
}
}
writer.close();
reader.close();
tempFile.renameTo(inputFile);
}
}
}
它的作用是注销一个登录到网站的用户。登录用户保存在一个名为""LoggedUsers.txt"
"的文件中。我想要的是,当我登出时,正在登出的名称将从文件中删除,而其他已登录的名称也将被删除,因为这就是现在发生的事情。
谢谢!
根据您的条件,您正在重写要注销的用户名,其余部分将被跳过。你只需要改变你的状况:
if(!trimmedLine.equals(lineToRemove))// use !
{
writer.write(currentLine);
}
编辑:
我希望这是你正在寻找的:
String name = null;
// to do rest
if(!trimmedLine.equals(name))// use !
{
writer.write(trimmedLine);
writer.newLine();// add, if required
}
在你的LoginServlet.java
中你没有掉饼干:
if(loginSucces)
{
Cookie c1 = new Cookie("cUsername", name);
Cookie c2 = new Cookie("cPassword", pass);
resp.addCookie(c1);//you are missing this line
rd = req.getRequestDispatcher("Welkom.jsp");
}
现在唯一的问题是,当我重新登录到用户的用户名在loggedusers文件
中出现双重
问题似乎是在LoginServlet.java
中,您有userLijst
列表跟踪登录的用户,因为您从未从列表中删除登录的用户,它被写入文件,因为您多次重新登录。如果列表中包含登录的用户,如
if(name != null && !name.isEmpty() && !userLijst.contains(name) && pass != null && !pass.isEmpty()) {
//
}
注:将密码存储在cookie中是一个坏主意
这是在登录时将用户名写入loggedusers.txt的代码:
公共类LoginServlet扩展HttpServlet{userLijst = new ArrayList();
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
boolean loginSucces = false;
String name = req.getParameter("username");
String pass = req.getParameter("password");
FileWriter fw = new FileWriter(new File("C://Users/Jacco_000/Desktop/LoggedUser.txt"));
BufferedWriter out = new BufferedWriter(fw);
if(!name.isEmpty() && name != null && !pass.isEmpty() && pass != null)
{
File f = new File("C://Users/Jacco_000/Desktop/users.txt");
if(f.exists() && f.isFile())
{
Scanner sc = new Scanner(f);
sc.useDelimiter(",");
while(sc.hasNextLine())
{
String s = sc.nextLine();
Scanner sc2 = new Scanner(s);
sc2.useDelimiter(",");
String nm = sc2.next();
String ps = sc2.next();
String rnm = sc2.next();
String em = sc2.next();
String ad = sc2.next();
String ld = sc2.next();
if(name.contains(nm) && pass.contains(ps))
{
loginSucces = true;
String loggedUsers = nm;
userLijst.add(loggedUsers);
for(int i = 0; i < userLijst.size(); i++)
{
out.write(userLijst.get(i));
out.newLine();
}
out.close();
fw.close();
}
}
}
}
else
{
loginSucces = false;
}
RequestDispatcher rd = null;
if(!loginSucces)
{
req.setAttribute("msgs", "Gebruikersnaam en/of Wachtwoord is incorrect");
rd = req.getRequestDispatcher("loginpagina.jsp");
}
if(req.getParameter("register") != null)
{
rd = req.getRequestDispatcher("registerpagina.jsp");
}
if(loginSucces)
{
Cookie c1 = new Cookie("cUsername", name);
Cookie c2 = new Cookie("cPassword", pass);
rd = req.getRequestDispatcher("Welkom.jsp");
}
rd.forward(req, resp);