I'm creating a web application using servlets class in java for backend and javascript and html for frontend, l'm trying to access to database in servlets to create validations in post request (i.e: login, upload image), when I create a new instance of the database class (in method function of the servlet class) the client does not receive the json that the server should send. 

I'm not getting any errors, it is just not sending back a response.

I would like to know why is this happening, I'm kinda new to servlets

**Servlet class, Login.java:**

```
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONObject;

@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

public Login() {
    super();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    JSONObject reqBody = new JSONObject(request.getReader().lines().collect(Collectors.joining(System.lineSeparator())));
    JSONObject json = new JSONObject();
    Database db = new Database("postgresql", "localhost", "5432", "first_project", "postgres", "masterkey");
    if(session.isNew()) {
        if(db.checkUser(reqBody.getString("email"), reqBody.getString("pass")) == true) {
                if (db.isAdmin(reqBody.getString("email"))) {
                    storeValue(reqBody.getString("email"), reqBody.getString("pass"), true, session);
                    json.put("status", "200");
                } else {
                    storeValue(reqBody.getString("email"), reqBody.getString("pass"), false, session);
                    json.put("status", "200");
                }
        } else {
                session.invalidate();
            }           
    } else {
        json.put("status", "you're already logged in");
    }   
    out.println(json.toString());       
    }

private void storeValue(String email, String password, boolean admin, HttpSession session) {
    if(email == null) {
        session.setAttribute("email", "");
        session.setAttribute("password", "");
        session.setAttribute("admin", "");
    } else {
        session.setAttribute("email", email);
        session.setAttribute("password", password);
        session.setAttribute("admin", admin);
    }
}

```

Database.java:

```
public class Database {

    protected ResultSet rs;
    protected ResultSetMetaData rsmd;
    protected JSONArray table;
    protected JSONObject row;
    protected PreparedStatement pstmt;
    protected Connection con;

    public Database(String jdbc,String host,String port, String db, String user, String pass){
        try {
            Class.forName("org.postgresql.Driver");
            this.con= DriverManager.getConnection("jdbc:"+jdbc+"://"+host+":"+port+"/"+db,user,pass);
        }
        catch(Exception e){
            e.getStackTrace();
        }
    }

    public boolean checkUser(String email, String password) {
        boolean st = false; 
        try {
                this.pstmt = con.prepareStatement("SELECT * FROM users WHERE email=? and password=?");
                this.pstmt.setString(1, email);
                this.pstmt.setString(2, password);
                this.rs = this.pstmt.executeQuery();
                st = this.rs.next();
        } catch (Exception e) { 
            e.printStackTrace();
        }
        return st;
    }

    public boolean isAdmin(String email) {
    boolean st = false;
    try {
        this.pstmt = con.prepareStatement("SELECT admin FROM users WHERE email=?");
        this.pstmt.setString(1, email);
        this.rs = this.pstmt.executeQuery();
        rs.next();
        if(this.rs.getString("admin").equals("t")) 
            st = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return st;

}

```

XHR.js:

```
function XHR() {
        var xmlObject = null;
        const xhr = (method,url,params,headers) => {
            return new Promise((res, rej) => {
                try {
                    xmlObject = new XMLHttpRequest();
                    xmlObject.open(method, url, true);
                    xmlObject.onload = () => {
                        if (xmlObject.status >= 200 && xmlObject.status < 300) {
                            res(JSON.parse(xmlObject.responseText));
                        } else {
                            rej({
                                status: xmlObject.status,
                                statusText: xmlObject.statusText
                            });
                        }
                    };

                    for (var header in headers) {
                        xmlObject.setRequestHeader(header, headers[header]);
                    }
                    if (method === 'POST') {
                        xmlObject.send(JSON.stringify(params));
                    }
                    else{
                        xmlObject.send();
                    }

                } catch (err) {
                    console.log('error');
                    rej(err);
                }
            });
        }

```

session.js:

```
var wrapper = new XHR();
    function $(id) {
        return document.getElementById(id);
    }
function login() {
        var email = $('email').value;
        var password = $('password').value;
        wrapper.post('./Login',{email:email, pass:password},{'Content-Type':'application/x-www-form-urlencoded'}).then(function(data){
            console.log(data)
            alert(data);
        });
    }   
   $('login').addEventListener('click', login);

```

login.html:

```
<head>     
    <link rel="stylesheet" type="text/css" href="style/login.css">
          <script src="js/XHR.js"></script>
          <script src="js/w3.js"></script>  
          <title>First project</title>
        </head>
        <body>
          <div w3-include-html="./layout/header.html"></div>
          <div class="login-page">
            <h1> Log in</h1>
            <form class="register-form">
              <div class="form">
                <input id="email" type="text" placeholder="email address"/>
                <input id="password" type="password" placeholder="password"/>   
                <button id="login">login</button>
                <p class="message">Already registered?
                  <a href="./login.html">Sign in </a>
                </p>
              </div>
            </form>
          </div> 
          <script src="js/session.js"></script>  
        </body>
    </html> 

```

As I said I am working with html so im doing the request with the xmlhttprequest object, where I call in the session.js file, the server process the data (i.e inserting a user in db in register form) but wont a response back to the client.

If I remove the db request (instance of the class) from the servlet the client receives the msg successfully, which means the server send the response back (as JSON).