Client HTML VS JSP [duplicate] - java

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Generate an HTML Response in a Java Servlet
(3 answers)
Closed 3 days ago.
I've been getting a good understanding of raw servlets, now, I know that HTML is prebuilt so if the client wanted to make a request to the server for some type of dynamic change, is this where i need to use JavaScript to make that request and do some DOM minuplulations?
Help me understand JSP, so instead of doing that DOM minuplulations with javascript from the client, I can just use JSP on the server and have my servlets build the page on run time then return that jsp back to the client?
Help me undertstand JSP vs HTML on how they differ from client to server
now i know if i make a request to the server for a page it comes back as a html that ive built, to populate the page with data from the server i would need to make request using js then use the response data to make sometype of changes within the clientside, so is this where JSP comes in handy, the work will be done on the server side? all the server got to do is insert the data and return the page?

Related

Is it possible to stream a text files content from a Servlet to JSP?

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.
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
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.

How do I send and receive information to a web page?

Ok, basically I have a game that I want to be able to upload and download data (highscores) so the user can see it.
My trouble is that I don't know how to upload their score to the server. I would not know where to start with making the program download the information (I want it to only download the needed information. In the past I have had to download an entire website to scan it for the needed information!).
If this question has already been asked, I'm sorry but I have searched for answer for over 2 hours now!
HttpRequest needs to be used here. You can send data (JSON recommended) from your JAVA code to the server, and the server stores it in DB. This topic is too broad, so I recommend you to look through HttpRequest first.
Ok here's how this works :
You'll have 3 main components - JSP(because you tagged Java), Servlet and
Java class.
JSP, the web page will be loaded on client machine and the other 2 will be on
server along with the db.
From JSP or the web page you'll make a request
(either synchronous or asynchronous).
synchronous : by submitting a form to the servlet
asynchronous : by calling servlet method without submitting any for(AJAX)
Now this request will either be for saving HighScore or retrieving HighScore,
therefore it will call the respective method from server(the servlet)
and which in turn will call methods from Java Class(DAO, data access object).
DOA will return the required value or will store the value depending
on the request.
You do not need to download any website and search data in it. Just make requests and handle responses.
Guide for servlets : http://www.tutorialspoint.com/servlets/

how to detect whether request is coming from browser or smartphone? [duplicate]

This question already has answers here:
Detecting Device Type in a web application
(8 answers)
Closed 9 years ago.
I am having a web application , which works fine .
now there is a particular page which gets bulky when i access the application from smartphone browser , so I wanted a situation like
if(request comes from computer browser client )
forward to bulky page in web application
else
if (request comes from smartphone )
forward to some other light page .
please put your suggestion how can i achieve this
Use the user-agent. Every connection to your server carries this header, by convenience and convention. There is no guarantee that a browser will be truthful(such as a spambot reporting itself as Chrome). You can get the user-agent as follows:
request.getHeader("User-Agent");
and then check again known user-agent strings and templates.
It's generally done by checking "User-Agent" header of Request

how to send HTTP request to a servlet [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send HTTP request in java?
I only have one servlet running on the Tomcat server side. Now I want to send a HTTP request to this servlet from a Swing application, and it's not an APPLET application (because I see some examples sending request from applet). How can I do this?
While you can open a direct socket connection and send the raw HTTP headers & content and receive a response back, I would urge you to take a look at HttpRequestBase.

Accessing HTML DOM elements from Java

I'm developing (with Java) a P2P application. One of the features includes a chat service. When a user sends a message to all of the application users, each user gets the message and updates its chat HTML page.
How can I access, from my Java code, the DOM of this page and change it, without the need to refresh the page in order to see the new message?
Is there any object in Java that can get me this access? For example, can I call a JavaScript function that inserts the new message?
If by from Java you mean applet then:
You can define some javascript functions in your HTML page to return/modify what you want and then call the javascripts from the applet. Look at here.
If by Java you mean Web server then you have to use some AJAX solution, you can look for example at JQuery
What you're really looking for is a technology known as Comet. Comet is Reverse Ajax. It's a technique that uses long-lived HTTP connections to hold a connection open from a client browser to a server so that the server can push updates back to the client browser.
The basic flow is that the server pushes a command back to the browser in the response, and JavaScript parses the response via a callback function, and then the JavaScript updates the DOM, all without reloading the page.
You can learn more about Comet on the CometD Website, and if you're developing on Google App Engine, this blog post on the ChannelAPI will be helpful.

Categories