0

I have to remake the project now and before I had Java methods which return me JSON value as following:

    public String getData(int TransID, int ID, Connection conRDS) throws ParseException {
    String Result = "";
    String SearchStatment="{?= CALL get_referral_fee(?,?)}";
    try {
    java.sql.CallableStatement pst = conRDS.prepareCall(SearchStatment);
    pst.registerOutParameter(1, Types.OTHER );  
    pst.setInt(2,TransID);
    pst.setInt(3,ID);
    pst.execute();
    System.out.println("invoice returned: "+(String) pst.getObject(1));
    Result= (String) pst.getObject(1);
    pst.close(); 
}catch (SQLException e) { /* ignored */ return "Empty"; }
        
return Result ; 

and in JSP page I could get it like this:

<%@ page import="RDS.RDSFunctions" %\> <% RDSFunctions myRDS= new RDSFunctions(); String InvData = myRDS.GetTRSReferralData(brokerid,dealid,con); JSONObject object = (JSONObject) parser.parse(InvData); String partyType = (String) object.get("Party"); //This is the value I need %\>

Now I have method doPost in the servlet and I cannot call the function directly. doPost in servlet:

   @WebServlet(name = "SecondSampleCore2", urlPatterns = { "/SecondSampleCore2" })
   ...
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws                             ServletException, IOException {
          var uri = URI.create("https://api...");
          var client = HttpClient.newHttpClient();
          var reqq = HttpRequest
          .newBuilder()
          .uri(uri)
          .header("Content-Type", "application/json")
          .POST(BodyPublishers.ofString(jsonObject.toJSONString()))
          .build();

    PrintWriter out = response.getWriter();
    HttpResponse<String> resp = client.send(reqq, HttpResponse.BodyHandlers.ofString());    
    JSONObject dealInfo = (JSONObject) json.get("yourRequest");
response.setContentType("application/json");
out.print(dealInfo); //The value which returns from API
    out.flush(); 

`

I need to get this value dealInfo in my JSP page before it loaded so I can insert the values inline with tag <%= value %>

I tried to use RequestDispatcher dispatcher = getServletContext().getNamedDispatcher("/SecondSampleCore2"); but there is no value to return

0 Answers0