我试图使用db连接登录表单。 当一个类型错误的用户名ajax应该回复“错误的用户名”。 我的servlet是用hibernate连接的,所以我可以用HPQL检查db的用户名。 Withouth Hibernate代码,我的servlet将字符串发回给我,没有任何问题。但是当我尝试检查用户名是否存在时,使用hibernate代码ajax无法回复任何响应。
我的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>
我的servlet
@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>
}
答案 0 :(得分:0)
list=conn.createQuery("k FROM Korisniciajax k WHERE k.korUser=\""+user+"\"").list();
我应该使用&#39;&#39;而不是&#34;&#34;
list=conn.createQuery(" SELECT k FROM Korisniciajax k WHERE korUser='"+user+"'").list();