我正在尝试使用登录servlet来验证我的角度webapp的登录信息。但是,当我发布请求时,却收到500个内部服务器错误。
这是我的loginservlet中的doPost:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ERS_Users user = new ObjectMapper().readValue(req.getInputStream(), ERS_Users.class);
System.out.println(user);
if(user == null) {
System.out.println("inside the first if");
resp.setStatus(400);
resp.getWriter().write("No data submitted");
return;
} else {
// authenticate user with database
System.out.println("Inside the first else");
user = launcher.authUser(user);
if(user != null) {
// everything is good send auth data
System.out.println("inside the second if");
resp.setStatus(200);
resp.getWriter().write(new ObjectMapper().writeValueAsString(user));
return;
}
}
resp.setStatus(401);
resp.getWriter().write("Email or password was incorrect");
}
我使用system.out.println
来查找问题。当有角度的应用程序正在向该Servlet发送电子邮件和密码时,在到达authUser方法之前,将打印“ Inside the first else”。
这是authUser方法:
public ERS_Users authUser(ERS_Users authUser) {
ERS_Users u = getUserbyUsername(authUser.getEmail());
if (u!= null ) {
if (u.getPassword().equals(authUser.getPassword())) {
u.setPassword("");
}else {
u = null;
}
}
return u;
}
public ERS_Users getUserbyUsername(String authUser) {
System.out.println("inside getuserbyusername");
Session session = sessionFactory.openSession();
System.out.println("inside getuserbyusername2");
Transaction tx = session.beginTransaction();
List<ERS_Users> u = null;
String hql = "From Users where ERS_EMAIL =: username";
NativeQuery q = session.createNativeQuery(hql);
q.setParameter("username", authUser);
u=q.getResultList();
ERS_Users users= null;
if(!u.isEmpty()){
// ignores multiple results
users= u.get(0);
}
session.close();
return users;
}
“ inside getuserbyusername”是打开会话时发生错误之前最后打印的内容:
Session session = sessionFactory.openSession();
我所看到的最接近的答案是Error 500 due to session
但是我清楚地创建了会话。