我试图制作一个非常简单的登录屏幕。它必须检查密码和用户名。还有一个默认用户,在init()
中设置。奇怪的是,它仍在NullPointerException
上给我一个rd.forward();
有人能解释一下吗我很抱歉我的英语不好,如果我需要详细说明,请告诉我。
package servlets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import classes.user;
public class loginServlet extends HttpServlet {
ArrayList<user> alleUsers = new ArrayList<>();
private String defaultUser="", defaultPassword="";
private user userding;
public void init(ServletConfig config) throws ServletException{
super.init(config);
defaultUser = config.getInitParameter("defaultUser");
defaultPassword = config.getInitParameter("defaultPassword");
user userding = new user(defaultUser, defaultPassword);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String a = req.getParameter("back");
String b = req.getParameter("username");
String c = req.getParameter("password");
Object o = getServletContext().getAttribute("arraylist");
RequestDispatcher rd = null;
if (a.equals("Register")) {
rd = req.getRequestDispatcher("registerScherm.jsp");
}
if (a.equals("Login")) {
System.out.println(b + " "+ c);
if(!b.equals(null) && !c.equals(null)){
if (o instanceof ArrayList) {
alleUsers = (ArrayList) o;
alleUsers.add(userding);
if (alleUsers != null) {
for (user u : alleUsers) {
if (b.equals(u.getUsername()) && c.equals(u.getPassword())) {
getServletContext().setAttribute("naam", b);
rd = req.getRequestDispatcher("ingelogdScherm.jsp");
break;
} else {
rd = req.getRequestDispatcher("loginScherm.jsp");
}
}
}
}
}else{
rd = req.getRequestDispatcher("loginScherm.jsp");
}
}
rd.forward(req, resp);
//It gives an error on this forward
}
}
这是我的loginscreen jsp页面:
<body>
<%
String userName = "";
for (Cookie c : request.getCookies()) {
if (c.getName().equals("username")) {
userName = c.getValue();
break;
}
}
%>
<div id="account">
<div id="inputbox">
<form action="loginServlet.do" method="post">
<input class="ltf" type="text" name="username" value="<%= userName %>" /><br/>
<input class="ltf" type="password" name="password" /><br/>
<input type="submit" name="back" value="Login" />
<input type="submit" name="back" value="Register" />
</form>
</div>
好吧我想我修好了。我删除了一些帮助的守卫。我仍然困惑为什么它没有通过它们,但它现在有效。谢谢!
答案 0 :(得分:0)
在执行此语句时,在整个条件语句中,rd可能尚未初始化:
rd.forward(req, resp);
这可能是您获得NullPointerException的原因,rd尚未初始化且尚未在内存中分配任何空间。这一切真的是一个指向null的指针。
如果这是您的应用程序中可以或允许发生的情况,您可以通过添加以下条件来避免异常
if(rd != null)
rd.forward(req, resp);
您可能希望在前端通知客户端无法初始化rd。
答案 1 :(得分:0)
你的缩进被打破了,这使得阅读变得困难;我为你搞砸了..
package servlets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import classes.user;
public class loginServlet extends HttpServlet {
ArrayList<user> alleUsers = new ArrayList<>();
private String defaultUser="", defaultPassword="";
private user userding;
public void init(ServletConfig config) throws ServletException{
super.init(config);
defaultUser = config.getInitParameter("defaultUser");
defaultPassword = config.getInitParameter("defaultPassword");
user userding = new user(defaultUser, defaultPassword);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String a = req.getParameter("back");
String b = req.getParameter("username");
String c = req.getParameter("password");
Object o = getServletContext().getAttribute("arraylist");
RequestDispatcher rd = null;
if (a.equals("Register")) {
rd = req.getRequestDispatcher("registerScherm.jsp");
}
if (a.equals("Login")) {
System.out.println(b + " "+ c);
if(!b.equals(null) && !c.equals(null)){
if (o instanceof ArrayList) {
alleUsers = (ArrayList) o;
alleUsers.add(userding);
if (alleUsers != null) {
for (user u : alleUsers) {
if (b.equals(u.getUsername()) && c.equals(u.getPassword())) {
getServletContext().setAttribute("naam", b);
rd = req.getRequestDispatcher("ingelogdScherm.jsp");
break;
} else {
rd = req.getRequestDispatcher("loginScherm.jsp");
}
}
}
}
}else{
rd = req.getRequestDispatcher("loginScherm.jsp");
}
}
rd.forward(req, resp); //It gives an error on this forward
}
现在已经完成了.. 请注意,如果 a 不等于“login”或“Register”,则rd自初始化后未更改:
String a = req.getParameter("back");
RequestDispatcher rd = null;
所以看看“a”是什么会很有趣..
根据数据,您可以在这种情况下结束的其他地方,包括以下行:
if (o instanceof ArrayList)
答案 2 :(得分:0)
如果a
的值不等于“登录”或“注册”,则rd
仍为null
,因此rd.forward(req, resp);
将NullPointerException
。
您可以通过打印a
的值来调试它,也可以在rd.forward(req, resp);
req.getRequestDispatcher("name.jsp");
的任何地方添加package servlets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import classes.user;
public class loginServlet extends HttpServlet {
ArrayList<user> alleUsers = new ArrayList<>();
private String defaultUser="", defaultPassword="";
private user userding;
public void init(ServletConfig config) throws ServletException{
super.init(config);
defaultUser = config.getInitParameter("defaultUser");
defaultPassword = config.getInitParameter("defaultPassword");
user userding = new user(defaultUser, defaultPassword);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String a = req.getParameter("back");
String b = req.getParameter("username");
String c = req.getParameter("password");
Object o = getServletContext().getAttribute("arraylist");
RequestDispatcher rd = null;
if (a.equals("Register")) {
rd = req.getRequestDispatcher("registerScherm.jsp");
rd.forward(req, resp);
}
if (a.equals("Login")) {
System.out.println(b + " "+ c);
if(!b.equals(null) && !c.equals(null)){
if (o instanceof ArrayList) {
alleUsers = (ArrayList) o;
alleUsers.add(userding);
if (alleUsers != null) {
for (user u : alleUsers) {
if (b.equals(u.getUsername()) && c.equals(u.getPassword())) {
getServletContext().setAttribute("naam", b);
rd = req.getRequestDispatcher("ingelogdScherm.jsp");
rd.forward(req, resp);
break;
} else {
rd = req.getRequestDispatcher("loginScherm.jsp");
rd.forward(req, resp);
}
}
}
}
}else{
rd = req.getRequestDispatcher("loginScherm.jsp");
rd.forward(req, resp);
}
}
}
}
,如下所示:
var count = (from p in _db.Personen
join pc in _db.Postcodes on p.Postcode equals pc.postcode
join r in _db.Regios on pc.RegioId equals r.RegioId
where p.Leeftijd >= leeftijdgetal[0] && leeftijd[1] <= p.Leeftijd &&
r.RegioNaam == regio &&
p.Geslacht == geslacht &&
p.Showit == 1
)
.Distinct()
.Count();