Register Login
Internet / AI Technology University (ITU/AITU)





|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 3. Threads and Network >> 3.2. Network
Current Topic: 3.2.7. Writing to the web.
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Writing to the web

It is possible to write to the web ? if on the other side there is a mechanism that expect your writing.

The scenario is very similar to the Client Socket and Server Socket communications. And the difference is very small. We establish two-way communications while using web-specific classes that are capable to send and receive HTTP headers in a similar way as browser does for us.

We use the URLConnection class and its methods to mimic a browser functionality.
Think about a regular web page that you can see in a browser. This HTML page can contain a form with text fields. Usually you would press a SUBMIT button to send data to the server.
The URL to the server is part of the HTML form tag. HTML forms usually use the HTTP POST METHOD to send data to a server. We will learn HTML in the next section.
In this section, let us mimic a browser and send this data from a Java program.
Our program must be able to write data by creating first a URLConnection to the server. Here are the steps describing this process.
1. Create a URL object.
URL url = new URL(urlString);

2. Use the openConnection() method of the URL object to retrieve the URLConnection object.
URLConnection connection = url.openConnection();

3. Use the setDoOutput() method to enable output capability on the URLConnection.
connection.setDoOutput(true);

4. Use the getOutputStream() method and send data to the target and close the stream.
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(data);
out.close();

5. Then the program can use the getInputStream() method to receive response from the server.
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// Then read the response stream and collect lines in the HTML string
String html = "";
for (String line=null;(line = in.readLine()) != null; ) {
html += line + System.lineSeparator();
}

6. A browser would display this response - HTML string - as a web page. Our program will just print it to the Console.

7. Then our program should close the streams and the connection.

8. On the other end, at the server side there must be a mechanism to receive this data, for example, a Java Servlet very similar to the ServerSocketService.

9. This servlet would process the data and send response back to the client program.

10. Note, that it is almost the same communications as happen between a browser and a server servlet.
Here is the example of the WebWriter class. We will learn more about Java Servlets in the next section.


package its.day16.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* The WebWriter class uses the writeToWeb() method to send data to a web target
* @author Jeff.Zhuk@Javaschool.com
*/
public class WebWriter {
/**
* The writeToWeb() method uses the URL and URLConnection classes to write to a web target
* The server side can provide the response, which will be returned to the caller
* @param urlString
* @param data
* @return response
* @throws Exception
*/
public String writeToWeb(String urlString, String data) throws Exception {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(data);
out.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String html = "";
for (String line=null;(line = in.readLine()) != null; ) {
html += line + System.lineSeparator();
}
in.close();
return html;
}
/**
* The main() will test the WebReader class with its writeToWeb() method to write data to a web target.
* The URL points to a set of services that implement ItsBaseService.
* The server side will call a proper service and provide the response, which will be displayed in the Console
*/
public static void main(String[] args) throws Exception {
// create a WebWriter object
WebWriter writer = new WebWriter();
String response = writer.writeToWeb(
"http://itofthefuture.com/BASE/Lookup?action=ItsBaseServices", "RequestRepetitionService|Hello");
System.out.println(response);
}
}
Was it clear so far? Highlight the text in question Or

Assignments:

1. Open Eclipse and under the package its.day16.http in the project week7.network ? create NEW ? Class - WebWriter.
2. Type the source of this class provided above.
3. Get rid of red lines in Eclipse.
4. Right mouse click on the source and RUN as Java Application
5. Check the response from the server in the Console.
6. Modify the data portion and check the response.
7. Create 2 QnAs and send as 3.2.7.HttpReader.Your.Name.txt to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2
We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!
We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>package its.day16.http;
<br/>
<br/>import java.io.BufferedReader;
<br/>import java.io.InputStreamReader;
<br/>import java.io.OutputStreamWriter;
<br/>import java.net.URL;
<br/>import java.net.URLConnection;
<br/>/**
<br/> * The WebWriter class uses the writeToWeb() method to send data to a web target
<br/> * @author Jeff.Zhuk@Javaschool.com
<br/> */
<br/>public class WebWriter {
<br/>	/**
<br/>	 * The writeToWeb() method uses the URL and URLConnection classes to write to a web target
<br/>	 * The server side can provide the response, which will be returned to the caller
<br/>	 * @param urlString
<br/>	 * @param data
<br/>	 * @return response
<br/>	 * @throws Exception
<br/>	 */
<br/>	public String writeToWeb(String urlString, String data) throws Exception {
<br/>	       URL url = new URL(urlString);
<br/>               URLConnection connection = url.openConnection();
<br/>               connection.setDoOutput(true);
<br/>
<br/>              OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
<br/>              out.write(data);
<br/>              out.close();
<br/>
<br/>              BufferedReader in = new BufferedReader(
<br/>                                    new InputStreamReader(
<br/>                                    connection.getInputStream()));
<br/>              String html = "";
<br/>              for (String line=null;(line = in.readLine()) != null; ) {
<br/>                 html += line + System.lineSeparator();
<br/>              }
<br/>              in.close();
<br/>	      return html;
<br/>	}
<br/>	/**
<br/>	 * The main() will test the WebReader class with its writeToWeb() method to write data to a web target.
<br/>	 * The URL points to a set of services that implement ItsBaseService.
<br/>	 * The server side will call a proper service and provide the response, which will be displayed in the Console
<br/>	 */
<br/>	public static void main(String[] args) throws Exception {
<br/>		// create a WebWriter object
<br/>		WebWriter writer = new WebWriter();
<br/>		String response = writer.writeToWeb(
<br/>			"http://itofthefuture.com/BASE/Lookup?action=ItsBaseServices", "RequestRepetitionService|Hello");
<br/>		System.out.println(response);
<br/>	}
<br/>}
<br/>






Was it clear so far? Highlight the text in question

Or


Assignments:

1. Open Eclipse and under the package its.day16.http in the project week7.network ? create NEW ? Class - WebWriter.
2. Type the source of this class provided above.
3. Get rid of red lines in Eclipse.
4. Right mouse click on the source and RUN as Java Application
5. Check the response from the server in the Console.
6. Modify the data portion and check the response.
7. Create 2 QnAs and send as 3.2.7.HttpReader.Your.Name.txt to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2

We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to AI
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy