0

I do not have much experience with JSP/Servlets and I have searched for an answer regarding my problem but have not found an answer to my question.

Lets say I have a file on the back-end, called test.txt, and i want my servlet to stream the file contents to a jsp page dynamically. By dynamically i mean that if the file is being updated, text is being appended to the end of file, then i want the client looking at the resulting html/jsp file to see the changes live.

For example, the client would be able to see the changes in a textbox element in the html document.

Is this possible with JSP/Servlets or not? If so, how would i go about it?

Thanks for your time.

Already tried searching for related questions, searching for stream inquires and haven't found any answer that would answer my question.

2 Answers2

0

I think that you should probably load the file:

See an example: How should I load files into my Java application?

and reload the page using javascript

See: How to reload a page using JavaScript

Germán
  • 328
  • 6
  • 15
  • I take it from this solution, every time the file on the back end is appended and the html document is reloaded, the new data from the file on the backend will become available to the html document, correct? Thank you for the solution but i was hopefully looking for one that was more intuitive with streaming. – user3470766 Jan 02 '19 at 21:20
0

I am sure you are not professional in web development , so lets fix it. When a client (Mobile App , Browser , ... ) wants to send a request to a server , it will create a TCP/IP (we have more protocols like UDP but web applications work on top of TCP/IP protocol) connection. All data between client and server will transfer by this connection. It's your only way to send and receive data from server or client. When you create a JSP file (and it will compile to Class file when deploy on server application [ for better understand search JASPER JSP ENGINE ] ) to response to requests i will produce a response like this for browser :

some Http-Headers
Content-Type : text/html
some other Http-Headers

{JSP as a html text}

so browser will parse the response , render it and show it to your client user. It means this jsp page will sent as a static html page. For update it you have 2 ways :

1- The wrong way : Keep-Alive connection between client and server and each time you want to update client , send whole response to it again ! So all data come and html will render again and ... . So its bad way and not safe and many things else.

2- The correct way : Create a new async connection between client and server using JavaScript and just request for watch data change in file and show it to user in real time , without render whole html page. If this connection use Http protocol (http protocol just is a text protocol not a network protocol like TCP/IP) add Connection : Keep-Alive in your request header to server keep it alive.

Note : if you want understand better , search for socket in java and learn how it works.

Alireza Akhoundi
  • 214
  • 3
  • 12
  • Hi, thanks for your answer, although i did find some parts of it rude. I understand how sockets work in Java but as of right now it would not be a viable option. – user3470766 Jan 03 '19 at 14:26
  • @user3470766 sry if i tell something that u think is rude but i never want to do this . I never tell you to use socket directly for do it , I just tried to show you some point , and now if you know how its work , so you can make a servlet to produce some response to client for show file changes . sry but its your only way and it is so easy too. hope help you. – Alireza Akhoundi Jan 03 '19 at 15:38
  • Hi @Alireza thanks for your response. I do appreciate it and will try it. Have a good day. – user3470766 Jan 03 '19 at 15:43