Я пытаюсь заставить форму входа соединиться с БД.когда один тип неверное имя пользователя ajax должен дать ответ "неправильное имя пользователя".мой сервлет связан с Hibernate, так что я могу проверить имена пользователей из БД с помощью HPQL.Без кодов Hibernate мой сервлет без проблем отправляет мне обратно строку.но с кодами гибернации, когда я пытаюсь проверить, существует ли имя пользователя, ajax не может отсканировать любой ответ.
my index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>pokusaj 2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function check(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
$.post("servletprovera", {"username":username, "password":password},
function(data){
document.getElementById("alert").innerHTML=data.result;
});//servletprovera is URL of my servlet
}
</script>
</head>
<body>
<form >
<input type="text" name="username" id="username"><br>
<input type="password" name="password" id="password"><br>
<b id="alert"></b><br>
<input type="button" value="Login" onclick="check();">
</form>
</body>
</html>
мой сервлет
@WebServlet(name = "ServletProvera", urlPatterns = {"/servletprovera"})
public class ServletProvera extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
String user=request.getParameter("username");
Configuration myConf=new Configuration();
myConf.configure("hibernate.cfg.xml");
StandardServiceRegistry service=new StandardServiceRegistryBuilder().
applySettings(myConf.getProperties()).build();
SessionFactory myFactory=myConf.buildSessionFactory(service);
Session conn=myFactory.openSession();
Transaction t=conn.beginTransaction();
List<Korisniciajax> list;//Korisniciajax-Hibernate_entity class
list=conn.createQuery("k FROM Korisniciajax k WHERE k.korUser=\""+user+"\"").list();
t.commit();
conn.close();
String result;
if(list==null){
result="wrong username";
}
else {
result="success";
}
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.print("{\"result\":\""+result+"\"}");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}