1 / 16

Making Text for the Web part 2

Making Text for the Web part 2. Barb Ericson Georgia Institute of Technology March 2006. Writing HTML. Now that you know basic HTML You can write a program that outputs an HTML page Use a BufferedWriter and FileWriter to write out the file From java.io

clawless
Télécharger la présentation

Making Text for the Web part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Making Text for the Webpart 2 Barb Ericson Georgia Institute of Technology March 2006 Georgia Institute of Technology

  2. Writing HTML • Now that you know basic HTML • You can write a program that outputs an HTML page • Use a BufferedWriter and FileWriter to write out the file • From java.io • Use the write method to output the HTML • Use the newLine method to force a new line Georgia Institute of Technology

  3. public class WebPageWriter { /** * Method to write an example web (HTML) page * @param fileName the file to write to */ public void writeExamplePage(String fileName) { // try the following try { // open a file for writing BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); // start the html page writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + "HTML 4.01 Transition//EN\""); writer.newLine(); writer.write("\"http://www.w3.org/TR/html4/loose.dtd\">"); writer.newLine(); writer.write("<html>"); writer.newLine(); // write out the header writer.write("<head><title>A simple web page" + "</title></head>"); writer.newLine(); // write out the body writer.write("<body>"); writer.newLine(); writer.write("<h1>A Simple Heading</h1>"); writer.newLine(); writer.write("<p>Some simple text</p>"); writer.newLine(); writer.write("</body>"); writer.newLine(); // end the page writer.write("</html>"); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } } Writing a Program to Generate HTML Georgia Institute of Technology

  4. Write Example Page • Test this method • Compile the class • Execute the method • Check the resulting HTML page • But why would we want to do this? • Why write a program to output an HTML page that we could write by hand? Georgia Institute of Technology

  5. Writing Programs that Output HTML • Write programs • To have reusable parts • To communicate process • To allow for tailoring • So breakup the writing of an HTML page • Into reusable parts that allow for tailoring • Break tasks into subtasks (procedural abstraction) • Create helper methods • Methods that help another method accomplish a task • These are usually private methods Georgia Institute of Technology

  6. Method to Start an HTML Page /** * Method to write the doctype and html tags * @param writer the writer to use * @throws IOException */ private void writeStart(BufferedWriter writer) throws IOException { // write the document type writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + "HTML 4.01 Transition//EN\""); writer.newLine(); writer.write("\"http://www.w3.org/TR/html4/loose.dtd\">"); writer.newLine(); // start the html page writer.write("<html>"); writer.newLine(); } Georgia Institute of Technology

  7. Write out the Heading and Title /** * Method to write the title out * @param writer the writer to use * @param title the title to use * @throws IOException */ private void writeHead(BufferedWriter writer, String title) throws IOException { writer.write("<head><title>" + title + "</title></head>"); writer.newLine(); } Georgia Institute of Technology

  8. Start the Body /** * Method to write the body of the page * @param writer the writer to use * @param body the body to write * @throws IOException */ private void writeBody(BufferedWriter writer, String body) throws IOException { writer.write("<body>" + body + "</body>"); writer.newLine(); } Georgia Institute of Technology

  9. Finish the Page /** * Method to finish the html page * @param writer the writer to use * @throws IOException */ private void writeEnd(BufferedWriter writer) throws IOException { writer.newLine(); writer.write("</body>"); writer.newLine(); writer.write("</html>"); } Georgia Institute of Technology

  10. /** * Method for writing a homepage for the passed * name * @param name the person's name * @param interests a list of the person's interests */ public void writeHomepageV2(String name, String interests) { // try the following try { // open a file for writing BufferedWriter writer = new BufferedWriter(new FileWriter(name + ".html")); // write the start writeStart(writer); // write the header writeHead(writer,name + "'s Homepage"); // write the body writeBody(writer,"<h1>Welcome to " + name + "'s Homepage</h1>" + "<p> I am interested in " + interests); // end the page writeEnd(writer); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } Method to Write a Homepage Georgia Institute of Technology

  11. How this Works • Methods can call other methods • If the other methods aren't needed outside the class make them private • These are called helper methods • When you call • writeHomepageV2("Barb Ericson","horseback riding, reading, and genealogy"); • This method will call the helper methods as it encounters them Georgia Institute of Technology

  12. Create a Web page from a Directory • Get all the image files in a directory • Ending with .jpg • And write out an html page that has thumbnails (smaller versions) of the images • Use the helper methods • To write out the basics of the HTML page • Get the line separator for the current system • And use that to add a new line String endOfLine = System.getProperty("line.separator"); Georgia Institute of Technology

  13. public void createImagePage(String directory) { String name = null; String body = ""; String endOfLine = System.getProperty("line.separator"); // try the following try { // create the File object File dir = new File(directory); // get the full path name of the directory String pathName = directory + dir.getName() + ".html"; BufferedWriter writer = new BufferedWriter(new FileWriter(pathName)); // write the start writeStart(writer); // write the head writeHead(writer,"Thumbnails from " + directory); // get the array of items in the directory String[] items = dir.list(); // loop through the array for (int i = 0; i < items.length; i++) { name = items[i]; if (name.indexOf(".jpg") >= 0) { body = body + "<p>Filename: " + name + "<img src='" + name + "' height='150'/></p>" + endOfLine; } } // write the body writeBody(writer,body); // write the end writeEnd(writer); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } Create Image Page Georgia Institute of Technology

  14. Main for Testing Create Image Page public static void main(String[] args) { WebPageWriter writer = new WebPageWriter(); String dir = "c:/intro-prog-java/mediasources/"; writer.createImagePage(dir); } Georgia Institute of Technology

  15. Exercise • Add a createImageTablePage method that puts 4 images in the first row of a table on a web page and then puts the file names in the row below the images • Add a createSoundPage that lists all the sound files in the mediasources directory in a bulleted list • Use a hyperlink around the name of the sound file and it will play when you click on it • <a href="soundFile.wav">soundFile.sav</a> Georgia Institute of Technology

  16. Summary • You can use Java programs to create HTML • You can break up a long public method into smaller private methods • Procedural abstraction • HTML pages can be generated from information in a directory • Like all the pictures or sound files Georgia Institute of Technology

More Related