我一直收到此错误,但还没有找到解决它的方法。
似乎与扫描仪有关,但我不确定此处要更改什么。
我的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)
以及运行时在浏览器中看到的屏幕截图:
我希望收到你们的来信!
预先加油,谢谢!
最佳答案
三思而后行。或者更准确地说,不要将您的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];
这样,您只需将扫描仪推进一次,就不会遇到无法进行扫描的情况。
关于java - HTTP状态500-NoSuchElementException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30623698/