如果验证成功,我的servlet应该将我重定向到welcome.jsp页面,如果不成功,则应该重定向到fail.jsp。
user = request.getParameter("name");
pass = request.getParameter("pass");
String q = "select * from users where user=" + user
+ " and password=" + pass + "";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(q);
String username = null;
String password = null;
while (rs.next()) {
username = rs.getString(2);
password = rs.getString(3);
}
if (username.equals(user) && password.equals(pass)) { // <-- Here is line 56
response.sendRedirect("Welcome.jsp");
} else {
response.sendRedirect("Fail.jsp");
}
。
Avertissement: StandardWrapperValve[LoginServlet]:Servlet.service() for servlet LoginServlet threw exception
java.lang.NullPointerException
at com.DemoLogin.LoginServlet.service(LoginServlet.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
你在一个null的String中做一个equals,如果查询返回0个元素,那么username和password都是null,从而得到一个nullpointer。
import numpy as np
C = np.zeros((100,10))
for i in range(10):
C_sub = get_sub_matrix_C(i, other_args) # shape 10x10
C[i*10:(i+1)*10,:10] = C_sub
你也应该使用回报;重定向之后只是为了确定,并且构建一个dao也会有所帮助,从长远来看,在servlet代码中使用sql代码会变得一团糟
答案 1 :(得分:0)
请尝试以下代码
if(rs.next())
{
response.sendRedirect("Welcome.jsp");
}
else {
response.sendRedirect("Fail.jsp");
}
如果用户名和密码正确,那么rs.next()
将为真,因为您查询将从数据库中检索一行。因此,您可以在Welcome.jsp
中重定向到该用户。否则,用户提供了错误的用户名或密码,因此您可以重定向到用户Fail.jsp
。
但是在你的方法中,如果用户提供了错误的用户名或密码,那么你肯定会得到一个NullPointerException
,因为你没有正确地处理空检查。
使用Connection
或Statement
或ResultSet
时,您应该妥善处理这些内容的开启和关闭。