0

I´m working in ionic app. When the user logs in, a file with different information is sent to the server.

let datos = this.getInfoLogin();

The content of the method is as follows

getInfoLogin(): any {
    let datos = {
        "version": this.versionNumber,
        "user": this.loginGroup.value.user,
        "fechaLogin": this.getCurrentDate(),
        "password": this.loginGroup.value.password
    };

    return datos;
}

I send that code to the server with an angular post service

this.http.postLogin(datos).subscribe(respLogin => {});

http.service.ts

@Injectable({
providedIn: 'root'
})
export class HttpService {

private url = 'http://localhost:8085/nombreApp/LoginServlet';

// Http Headers
headers: HttpHeaders = new HttpHeaders({
    Accept: "application/json",
    "Content-Type": "application/json; charset='utf-8'",
});

constructor(public http: HttpClient) {
    console.log("Servicio login listo");
}

postLogin(login): Observable<any> {
    return this.http.post(`${this.url}`, login, { headers: this.headers });
}
}

But on the server I always get null when I use

String user = request.getParameter("user");

LoginServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        response.setContentType("text/html");
        String user = request.getParameter("user");
        
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error PrintWriter." + e.getMessage());
    }

}

I know the data arrives but I don't know how to read it. What am I doing wrong? I have searched a lot and I see that the correct way to do it is this. I'm right?

tripossi
  • 166
  • 10
  • You're sending parameters as JSON but you're trying to read them as form data. What exactly do you want? 1) Send parameters as JSON instead of form data. Or 2) Read parameters as JSON instead of form data. The solution to 1) would be to read parameters as JSON instead of form data. The solution to 2) would be to send parameters as form data instead of JSON. – BalusC Sep 10 '20 at 12:47
  • The number 2. Read parameters as JSON. How do I read the data as JSON? Could you help me with that? – tripossi Sep 10 '20 at 12:53
  • @BalusC, Could you show me an example of how I should do it? – tripossi Sep 10 '20 at 14:38
  • There's one in https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax/4113258#4113258, under the section "Manually sending JSON object to servlet". – BalusC Sep 10 '20 at 15:05

0 Answers0