-3

Is it possible to send extra data attached to a http response via Java or Php?

My Website is a homework-platform: One User enters homeworks into a database, and all users can then see the homeworks on the website. The current load is very inefficient, as the browser makes two requests for eveything to load: One for the index file and one for the homeworks. For the homeworks request the client also sends settings of the user to the server, based on which the returned homeworks are generated by a Php script.

Now, I wonder, if it is possible, to combine those two requests into one? Is it maybe possible to detect the http request with Java or Php on the server, read the cookies (where the settings are saved), then get the homeworks from the database and send the data attached to the http response to the client? Or, even better, firstly only return the index file and as soon as possible and the homework data afterwards as a second response, because the client needs some time to parse the Html & build the DOM-tree when it can't show the homeworks anyway.

While browsing the web I stumbled across terms like "Server-side rendering" and "SPDY", but I don't know if those are the right starting points.

Any help is highly appreciated, as I'm personally very interested in a solution and it would greatly improve the load time of my website.

Sir_baaron
  • 365
  • 1
  • 3
  • 11
  • "the browser makes two requests for eveything to load: One for the index file and one for the homeworks" -- I don't understand what you mean. Could you post the relevant code to show how this works? – kittykittybangbang Jan 20 '16 at 23:01
  • Since the homeworks aren't a static content, it needs to get them everytime from from the database. This is done via an Ajax request, which executes a PHP script, that then returns the homeworks fetched from the database to the client. The client then adds the homeworks into the Dom-tree. You can find the Website here: aaron.filosofisch.com (It's in German, though) – Sir_baaron Jan 20 '16 at 23:06
  • Still want some help with this. It can be done. – Misunderstood May 12 '16 at 12:18

1 Answers1

0

A simple solution to your problem is to initialize your data in the index file.

You would create a javascript object, and embed it right into the html, rendered by your server. You could place this object in the global namespace (such as under window.initData), so that it can be accessed by the code in your script.

<scipt>
window.initData = {
   someVariable: 23,
}; // you could use json_encode if you use php, or Jackson if you use java
</script>

However, it is not a huge problem if your data is fetched in a separate server request. Especially when it takes more time to retrieve the data from the database/web services, you can provide better user experience by first fetching the static content very quickly and displaying a spinner while the (slower) data is being loaded.

David Frank
  • 5,918
  • 8
  • 28
  • 43