我一直在收到这个错误,但我还没有办法解决这个问题。 似乎是关于扫描仪的东西,但我不确定在这里要改变什么。
我的LoginServlet:
package web;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// ______________________________________________________________
boolean loginSuccess = false;
String naam = "";
String pass = "";
RequestDispatcher rd = null;
String operator = req.getParameter("operator");
if (operator.equals("Register")) {
req.getRequestDispatcher("Register.jsp").forward(req, resp);
}
// _________________________*******_________________________________
// ___________________scanner declaratie________
FileReader fr = new FileReader("users.txt");
Scanner sc = new Scanner(fr);
// _________________________*******____________
// ___________________!! Lezen van file !!________________________
if (operator.equals("Login")) {
while (sc.hasNextLine()) {
String regel = sc.nextLine();
sc.useDelimiter("\\s:\\s");
naam = sc.next();
pass = sc.next();
System.out.println(naam);
System.out.println(pass);
// ___________________Toevoegen aan logged users_____
if (req.getParameter("username").equals(naam)
&& (req.getParameter("password").equals(pass))) {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("loggedusers.txt", true)));
out.println(naam);
out.close();
loginSuccess = true;
break;
} else if (req.getParameter("username").equals(naam)
&& (!req.getParameter("password").equals(pass))) {
req.setAttribute("error", "Password incorrect");
loginSuccess = false;
break;
} else if (!sc.hasNextLine()) {
req.setAttribute("error", "Username doesn't exist");
loginSuccess = false;
break;
}
}
sc.close();
if (loginSuccess = true) {
req.getRequestDispatcher("Welcome.jsp").forward(req, resp);
} else if (loginSuccess = false) {
req.getRequestDispatcher("Login.jsp").forward(req, resp);
}
}
}
}
我的Login.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Login page</title>
</head>
<body>
<form action="LoginServlet.do" method="post">
<table>
<tr>
<td align="right">Username:</td>
<td align="left"><input type="text" name="username" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td align="left"><input type="text" name="password" /></td>
</tr>
<tr>
<td align="left"><input type="submit" name="operator" value="Register" /></td>
<td align="right"><input type="submit" name="operator" value="Login" /></td>
</tr>
</table>
</form>
<h1><%=request.getAttribute("error") %></h1>
</body>
</html>
web.xml应该是正确的,因为它之前有效,但是在从servlet编写其余代码之后,它会阻塞。
它在Eclipse中给出的错误:
jun 03, 2015 4:44:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Dynamic Example Servlet] in context with path [/DynamicExample] threw exception
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at web.LoginServlet.doPost(LoginServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我运行时在浏览器中看到的截图:
我很想听听你们的意见! 欢呼并提前致谢!
答案 0 :(得分:1)
在你跳跃之前先看看。或者更确切地说,不会使您的Scanner
加倍。
这些行是罪魁祸首:
String regel = sc.nextLine();
naam = sc.next();
pass = sc.next();
hasNextLine()
对nextLine()
的通话有效,但对后续的next()
通话无效。
您可能想要做的事情是从naam
读取paas
和regel
值。
String regel = sc.nextLine();
String[] regelBroken = regel.split();
naam = regelBroken[0];
paas = regelBroken[1];
这样,您只需要将扫描仪推进一次,这样您就不会遇到无法扫描的情况。
答案 1 :(得分:0)
“ users.txt ”的末尾是否有空行?
在最后一次迭代中,这可能导致naam = sc.next();
抛出异常。
答案 2 :(得分:0)
我弄清楚了,我尝试了一点不同:
FileReader fr = new FileReader("users.txt");
BufferedReader br = new BufferedReader(fr);
while (true) {
String regel = br.readLine();
if (regel == null) {
break;
}
sc = new Scanner(regel);
sc.useDelimiter("\\s:\\s");
naam = sc.next();
pass = sc.next();
有效!
感谢阵列的解决方案,我保存了那段代码,我想我以后可能会尝试这样的方法,谢谢大家!