0

I am making a web application where I can read large data from database using hibernate in my Java Servlet. When I read large data from database field, I put it into the String with success, after that I want to put it in JSON and pass it to Javascript Ajax but it seems that data is too large for JSON. When I try to pass smaller JSON to Ajax it works fine. What's wrong at here?

This is my Ajax:

$.post("servletispistabeleuseru", {"user":user}, function(data){
    var tab=data.result;
    console.log(tab);
});

and this is my Servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/json");
        String user=request.getParameter("user");
        Configuration myConf = new Configuration();
        myConf.configure("hib/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<User>useri;
        useri=conn.createQuery("SELECT u FROM User u WHERE useUsername='"+user+"'").list();
        String result=useri.get(0).getUseKineski();

        System.out.println(result);
        t.commit();
        conn.close();
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.print("{\"result\":\"" + result + "\"}");
        }
    }
KevinO
  • 4,303
  • 4
  • 27
  • 36
Danilo99
  • 33
  • 7
  • "seems that data is too large for json"...do you get some particular error? And why are you building your JSON by hand like that? You should be using a proper serialiser. The error could be as simple as some kind of special character (e.g. a double-quote character) messing up the formatting. For all we know it could be nothing to do with the size. Firstly tell us what actual error and/or problem you're seeing – ADyson Nov 14 '18 at 15:25
  • BTW your SQL query is wide open to injection attacks, you should use a proper parameterised query to protect your database. – ADyson Nov 14 '18 at 15:26
  • there is no any specific error. The data just can not be shown in console. i have chinese characters in my string can that be a problem? mysql charset is utf8mb4. – Danilo99 Nov 14 '18 at 15:33
  • "just can not be shown"...means what **exactly**? Nothing appears at all? Or it truncates your string at a certain number of characters? Or just some character are blank? Please be precise about the issue. I doubt chinese characters would be an issue unless your browser doens't support UTF8, or Java is not connecting to the database using the correct charset, or maybe you didn't set UTF8 in your web page. There are a few possibilities. First thing to do is inspect the Java using the debugger and see if the data looks correct there or not. Then we can see if it's a server or client issue. – ADyson Nov 14 '18 at 15:36
  • in console nothing appears at all, and when i replace that "long string" with regular short string everything is ok. In java servlet when i type Sistem.out.println(long string) data is shown as expected. my page is UTF-8. – Danilo99 Nov 14 '18 at 15:41
  • Normally, there is no limit on JSON response, but the string type is limited, so maybe you need to find another type or decompose your data into many portions – Terai Nov 14 '18 at 15:43
  • What you mean by that? i think that my string is ok because it shows as expected in System.out.println(string). do you think i can try to make array of strings and send that in json to ajax? – Danilo99 Nov 14 '18 at 15:52
  • rather than checking the browser console, look in the Network tab of the developer tools and get the raw response from the ajax call...does it look right in there, or not? And how long are we talking? What's the cut-off point where it stops displaying - what's the maximum length of string you can see? – ADyson Nov 14 '18 at 15:54
  • Also try calling the URL from another client (e.g. PostMan)...do you get the expected response, or not? Let's narrow this down a bit. – ADyson Nov 14 '18 at 15:54
  • Also, does your "short string" also contain the chinese characters you mentioned (same as the long one), or not? Then we can rule that in or out as a possibility. – ADyson Nov 14 '18 at 15:57
  • No, short string is just regular characters. now i tried to send short string to ajax and i is shown in console as "???????". but at least it is written in console. – Danilo99 Nov 14 '18 at 16:01
  • Perhaps you simply have to wait for the data being downloaded?! What amount of data are you actually talking about? – dpr Nov 14 '18 at 16:03
  • @dpr the JS code shown waits until the entire response is received before executing, it cannot work any other way. – ADyson Nov 14 '18 at 16:05
  • i have excel table of 12 kb which is traslated into html table code, and that code is converted to string and saved to db(about 4000 chars). so my string is about 4000 characters. – Danilo99 Nov 14 '18 at 16:08
  • And how long can it get to before you can't see it any more? And have you got answers to any of my other questions? We need some detailed debugging info – ADyson Nov 14 '18 at 16:21
  • Is the issue that you want to return a long string to the view? – JamesS Nov 14 '18 at 16:22
  • i tried with another client but it is same, nothing happens. How can i know exact max length of string that will work? – Danilo99 Nov 14 '18 at 16:33
  • @Adyson i tried everything. is there any alternative to web.config file when i can set max json length? – Danilo99 Nov 14 '18 at 16:43
  • " How can i know exact max length of string that will work"...trial and error initially. I don't know how to control the max length in your Java environment, sorry, perhaps you can google it. But TBH if that was the problem I'd expect an exception instead. – ADyson Nov 14 '18 at 16:52
  • P.S. another thing - try just returning plain text instead of JSON. `out.print(result);` - just the string and nothing else. And obviously temporarily change the JS to `console.log(data);` and nothing else. Or look in the network tab. I still wonder if the serialisation might be an issue, as I mentioned in the first comment. You haven't changed that as far as I know. – ADyson Nov 14 '18 at 16:53
  • i tried with only out.print(result) i doesn't help. what exactly should i check in network tab? everything seems okay there. – Danilo99 Nov 14 '18 at 17:14
  • check the Response tab of the ajax call to see if the raw response contains what you expected (as opposed to the console output) – ADyson Nov 14 '18 at 17:19
  • Also which browser are you using? Some browser limit the length of what can be logged to the console. – ADyson Nov 14 '18 at 17:19
  • when i go to ajax call response it shows long code that i cannot understand well. when i go to servlet/response it shows my string . i tried with mozilla and chromium. – Danilo99 Nov 14 '18 at 17:33
  • "long code that i cannot understand well"...what do you mean by that? Show us, if you're not sure what it is. Stick it in a pastebin or something – ADyson Nov 14 '18 at 19:34

0 Answers0