简体   繁体   English

J2EE和Angular HTTP POST servlet

[英]J2EE and Angular HTTP POST servlet

I have googled a lot tried all the workarounds which were mostly for PHP but it didn't work. 我已经google了很多尝试所有的解决方法,主要是为PHP,但它没有用。 I'm trying to send POST parameters to j2ee servlet but parameters are not being received at servlet although if I'm sending parameters by attaching with URL (GET) so that I'm able to receive data. 我正在尝试将POST参数发送到j2ee servlet但是在servlet上没有接收到参数,尽管如果我通过附加URL(GET)发送参数以便我能够接收数据。

Here's the code. 这是代码。

   import { Component } from '@angular/core';
   import { IonicPage, NavController, NavParams } from 'ionic-angular';
   import { Http, Headers, RequestOptions } from '@angular/http';
   import 'rxjs/Rx';
   @IonicPage()
   @Component({
  selector: 'page-login',
  templateUrl: 'login.html',
 })
export class LoginPage {
 userName: string;
password: string;

constructor(public navCtrl: NavController, public navParams: NavParams, 
public http: Http) {
 }

login() {

var headers = new Headers({ 'Content-Type': 'application/json' });

let options = new RequestOptions({ headers: headers });
let devicecode = '1-001-001-009-1';

let postParams = {
  loginid: this.userName,
  password: this.password,
  devicecode: devicecode
}

this.http.post("http://localhost:8092/OCWebService/LoginOSO", postParams
  , options)
  .subscribe(data => {
    console.log(data);
    alert(data);
  }, error => {
    console.log(error);// Error getting the data
  });


    }

   }

Here's the Servlet Code: 这是Servlet代码:

  protected void processRequest(HttpServletRequest request, 
  HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
    response.setContentType("text/html;charset=UTF-8");    

        BufferedReader bf = request.getReader();
        String userName = request.getParameter("loginid");
        String password = request.getParameter("password");
        String deviceCode = request.getParameter("devicecode");
      }

Note: I'm new to Ionic 2. 注意:我是Ionic 2的新手。

Parameters ( request.getParameter() ) is only for application/x-www-form-urlencoded data or query string parameters. 参数( request.getParameter() )仅适用于application/x-www-form-urlencoded数据或查询字符串参数。 You are sending JSON. 您正在发送JSON。

To solve the problem, you can either send application/x-www-form-urlencoded , or extract the JSON from the request input stream and parse it yourself. 要解决此问题,您可以发送application / x-www-form-urlencoded ,或者从请求输入流中提取JSON并自行解析。 You will need to use a library like Jackson for this. 你需要使用杰克逊这样的图书馆。 For example 例如

 InputStream in = request.getInputStream();
 MyPojo pojo = new ObjectMapper().readValue(in, MyPojo.class);

ObjectMapper is a Jackson class. ObjectMapper是一个Jackson类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM