Presentation is loading. Please wait.

Presentation is loading. Please wait.

(c) 2006-2008 E.S.Boese All Rights Reserved.1 Internet Applications Chapter 15 - Lecture Slides.

Similar presentations


Presentation on theme: "(c) 2006-2008 E.S.Boese All Rights Reserved.1 Internet Applications Chapter 15 - Lecture Slides."— Presentation transcript:

1 (c) 2006-2008 E.S.Boese All Rights Reserved.1 Internet Applications Chapter 15 - Lecture Slides

2 (c) 2006-2008 E.S.Boese All Rights Reserved. 2 Internet Terminology URL : full path Protocol : set of rules (e.g. http, https, ftp, telnet, etc. ) Domain name : can purchase right to use IP address : 4 numbers between 0 and 255 separated with. (e.g. 128.57.127.2 ) Internet address : English version of an IP address Hyperlinks : link on a webpage that takes you to another page (or a different location within the same page)

3 (c) 2006-2008 E.S.Boese All Rights Reserved.3 JEditorPane

4 (c) 2006-2008 E.S.Boese All Rights Reserved. 4 JEditorPane creation Declare a JEditorPane component. JEditorPane pane; Check to see if it is null (first time we run the applet). If it is null, we instantiate it: if( pane == null ) pane = new JEditorPane( );

5 (c) 2006-2008 E.S.Boese All Rights Reserved. 5 JEditorPane Set the web page in to the JEditorPane. NOTE: The URL MUST begin with http:// pane.setPage( url ); try...catch blocks are Java's way of handling errors which are thrown We need to put our code within a try…catch block in case there is a problem accessing the web page. try { pane.setPage( url ); } catch( IOException ioe ) { pane.setText( "Error accessing web page: " + url ); } If there is an error accessing the page we want, we’ll print the error message inside the JEditorPane

6 (c) 2006-2008 E.S.Boese All Rights Reserved. 6 JEditorPane – setupURL method Create a method setupURL to make it easier for us to change the page being displayed: public void setupURL( String url ) { if( pane == null ) pane = new JEditorPane( ); try { pane.setPage( url ); } catch( IOException ioe ) { pane.setText( "Error accessing web page: " + url ); }

7 (c) 2006-2008 E.S.Boese All Rights Reserved. 7 JEditorPane Store the JEditorPane inside a JScrollPane, to ensure users can access the whole page being displayed JScrollPane scrollPane = new JScrollPane(pane);

8 (c) 2006-2008 E.S.Boese All Rights Reserved. 8 JEditorPane Handle clicks on links inside the web page that is displayed. To do this, there are four things we need to do (similar to other events we've worked with). 1. These events are inside the javax.swing.event package import javax.swing.event.*; 2. Specify that we are listening for these type of events – implements HyperlinkListener 3. Specify that we want to listen for HyperLinkEvents on our JEditorPane component: pane.addHyperlinkListener( this ); 4. Implement the required method for implementing the HyperlinkListener, public void hyperlinkUpdate( HyperlinkEvent event ) { if( event.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) { setupURL( String.valueOf( event.getURL( )) ); }

9 (c) 2006-2008 E.S.Boese All Rights Reserved. 9 Simple example see JEditorPaneEx.java

10 (c) 2006-2008 E.S.Boese All Rights Reserved. 10 Example with buttons see JEditorPaneExBtn.java

11 (c) 2006-2008 E.S.Boese All Rights Reserved.11 Hosting applets on the Internet

12 (c) 2006-2008 E.S.Boese All Rights Reserved. 12 Hosting your applet on the Web Get your applet up and running on a host somewhere. First you need to get your applet ready without using an IDE such as Eclipse. Setting up the HTML file  Create a file (in your text editor).  Enter the following line, changing the XYZ to the name of your class.  This needs to be saved a file named exactly: "index.html" in lower case letters.  Modify the width and height to what you want.

13 (c) 2006-2008 E.S.Boese All Rights Reserved. 13 Hosting your applet on the Web Hosting for free (or buy a domain – see below)  University accounts  http://www.doteasy.com/  http://members.freewebs.com/index.jsp Another option: Buying a Domain Name (FYI, not necessary)  Register.com  Registar.com  Aplus.net  domainsInSeconds.com  others...

14 (c) 2006-2008 E.S.Boese All Rights Reserved. 14 Hosting your applet on the Web Uploading files  Upload the index.html, all the.class files, all the images and sound files necessary to make your applet work  Make sure your code referencing the image file names matches the actual file name (case sensitive!! This is important as Eclipse in Windows has been lazy, allowing any case to work)  NOTE: if your project read in from a local file (not a URL) or writes to a file, you need to remove that code before it will work on the Internet (see Security section).

15 (c) 2006-2008 E.S.Boese All Rights Reserved.15 Applet Parameters

16 (c) 2006-2008 E.S.Boese All Rights Reserved. 16 Applet Parameters Customize applet without changing the code Send preferences in HTML document as param tags Give a name and value Use double-quotes for text with spaces in it. Use double- quotes for text with spaces in it.

17 (c) 2006-2008 E.S.Boese All Rights Reserved. 17 Applet Parameters In Eclipse

18 (c) 2006-2008 E.S.Boese All Rights Reserved. 18 Applet Parameters Inside your Java Code, call getParameter( keyName ) Example:  For HTML  Java Code label = new JLabel( getParameter( “title” ) ); Parameter for getParameter matches the NAME in HTML PARAM tag

19 (c) 2006-2008 E.S.Boese All Rights Reserved. 19 Applet Parameters Read in values within the applet  getParameter( name ) import java.awt.*; import javax.swing.*; public class AppParams extends JApplet { JPanel bkgrnd; JLabel heading; public void init( ) { bkgrnd = new JPanel( ); bkgrnd.setBackground( new Color ( Integer. parseInt ( getParameter( "bkcolor" ), 16 ) ) ); heading = new JLabel( getParameter( "title" ) ); heading.setForeground( new Color( Integer. parseInt ( getParameter( "textcolor" ), 16 ) ) ); bkgrnd.add( heading ); setLayout( new BorderLayout( ) ); add( bkgrnd, BorderLayout. CENTER ); }

20 (c) 2006-2008 E.S.Boese All Rights Reserved.20 Configuration Files

21 (c) 2006-2008 E.S.Boese All Rights Reserved. 21 Configuration Files Another way to customize the applet without changing the source code Store name/value pairs in a text file Example file: title=My Favorite Playground bkcolor=0000FF textcolor=AAAA00

22 (c) 2006-2008 E.S.Boese All Rights Reserved. 22 Configuration Files split method  Specify a delimiter  Stores the separated tokens into an array  Example: use \n as a delimiter to separate each line into a separate String stored in an array String dataFile = “cats and dogs\nJava goodness\nskiing”; String[ ] lines; lines = dataFile.split( "\n" );  Remove the newline at the end lines[0] = lines[0].substring( 0, lines[0].length( ) - 1 ); lines cats and dogs\n Java goodness\n skiing 012012

23 (c) 2006-2008 E.S.Boese All Rights Reserved. 23 title=My Favorite Playground\n bkcolor=0000FF\n textcolor=AAAA00\n 012012 Configuration Files Algorithm  Read in all the lines in the file and store in a String  Use split method on \n to separate each line into a separate String stored in an array String[ ] lines; lines = configFile.split( "\n" );  Use split method again on = for each line to separate the name from the value lines[0].split(“=“) title=My Favorite Playground bkcolor=0000FF textcolor=AAAA00 lines title= My Favorite Playground\n 01 01 title=My Favorite Playground\n bkcolor=0000FF\n textcolor=AAAA00\n 012012

24 (c) 2006-2008 E.S.Boese All Rights Reserved. 24 Configuration Files public String readConfigFile( ) { String content; try { URL target = new URL( getCodeBase( ), configFilename ); URLConnection con = target.openConnection( ); con.connect( ); byte b[ ] = new byte[ 1024 ]; // byte array int nbytes; // num bytes read in String retVal = new String( ); BufferedInputStream in = new BufferedInputStream( con.getInputStream( ), 2048 ); while ( (nbytes = in.read( b, 0, 1024 )) != -1 ) // while there's more data to read { content = new String( b, 0, nbytes ); // get 1024 bytes of data fr file retVal += content; } in.close( ); // close connection return retVal; // returns the whole config file as a String } catch ( Exception e ) { return "Error reading config file"; } } Read in the entire config file You shouldn’t need to change this code for reading in any file

25 (c) 2006-2008 E.S.Boese All Rights Reserved. 25 Configuration Files public void parseConfig( String cfg ) { String[ ] lines, tokenkey; lines = cfg.split( "\n" ); for( int i=0; i<lines.length; i++ ) { tokenkey = lines[i].split( "=" ); // remove newline at end String value = tokenkey[1].substring( 0, tokenkey[1].length( ) -1 ); if ( tokenkey[0].equals( "bkcolor" ) ) backcolor = new Color( Integer. parseInt ( value, 16 ) ); else if ( tokenkey[0].equals( "textcolor" ) ) textcolor = new Color( Integer. parseInt ( value, 16 ) ); else if ( tokenkey[0].equals( "title" ) ) titletext = value; } Method to parse the String containing the contents of the config file Customize your applet based on the token and value pairs parseInt needs the value and the 16 for the base. Colors use hex (base 16)

26 (c) 2006-2008 E.S.Boese All Rights Reserved.26 Applets and Email

27 (c) 2006-2008 E.S.Boese All Rights Reserved. 27 Applets and Email Applets cannot send email directly Need CGI program CGI  Common Gateway Interface  Program executing on the server that allows an HTML web page or Java applet to access/write info on the server  Can be written in various programming languages: C, perl, php Example:  Requires access to code interpreter and either mail or sendmail program

28 (c) 2006-2008 E.S.Boese All Rights Reserved. 28 Applets and Email CGI Programs Your Applet 1 - Request to CGI program Web Server 2 - Executes CGI program Read/Write access to files on server 3 - CGI program returns info Web server is where you ‘host’ your applet on the Internet

29 (c) 2006-2008 E.S.Boese All Rights Reserved. 29 Applets and Email Perl CGI Example Store perl file (ends with.pl) in appropriate area on web site Change permissions on file  Unix/MacOS system: Run: chmod u+rwx CGIfile.pl  Windows system: right-click on file, choose “Properties” Depending on setup, may need to ask administrator:  Full path to perl  Full path to either mail or sendmail program  Location for CGI files (e.g. in public_html/cgi-bin directory)  Required filename postfix End with.pl or.cgi

30 (c) 2006-2008 E.S.Boese All Rights Reserved. 30 Applets and Email Unix Perl Script #!/usr/bin/perl use CGI ':standard'; my $outfile = "mailing.out"; my $sendmail = "/usr/sbin/sendmail -t"; my $reply_to = "Reply-to: boese\@cs.colostate.edu\n"; my $subject = "Subject: Applet Message\n"; my $to = "To: boese\@cs.colostate.edu\n"; my $content = param('message'); print header; # required first set of lines print "Thanks!"; open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL $reply_to; print SENDMAIL $subject; print SENDMAIL $to; print SENDMAIL "Content-type: text/plain\n\n"; print SENDMAIL $content; close(SENDMAIL); Change to the absolute path to the perl program on your system, and the absolute path to the sendmail program Change the Reply- to and To email addresses to your own The \ before the @ is required Change the Reply- to and To email addresses to your own The \ before the @ is required

31 (c) 2006-2008 E.S.Boese All Rights Reserved. 31 Applets and Email Inside your Java applet Write a method named sendMail takes as parameters  the URL to the CGI program and  the message you want to send  URL ends with ? then the message string ?message=String  Example: String s = sendMail( "http://www.cs.colostate.edu/~boese/cgi-bin/mailer.cgi?message=",message );

32 (c) 2006-2008 E.S.Boese All Rights Reserved. 32 Applets and Email public String sendMail( String fullPath, String msg ) { try { String enc = URLEncoder.encode(msg, "UTF-8"); URL target = new URL( fullPath + enc ); String content; URLConnection con = target.openConnection( ); // open connection con.setUseCaches( false ); con.setDefaultUseCaches( false ); byte b[ ] = new byte[ 1024 ]; // byte array int nbytes; // num bytes read in String retVal = new String(); BufferedInputStream in = new BufferedInputStream( con.getInputStream( ), 2048 ); while( (nbytes = in.read( b, 0, 1024 )) != -1 ) // while there's more data to read { content = new String( b, 0, nbytes ); // get 1024 bytes of data fr file retVal += content; } in.close( ); // close connection return retVal; } catch( Exception e ) { return "Error " + e.toString( ); } } You shouldn’t need to change this method, just call it

33 (c) 2006-2008 E.S.Boese All Rights Reserved. 33 Applets and Email PHP CGI Example Store php file (ends with.php) in appropriate area on web site Works the same on UNIX/Linux/MacOS and Windows Change permissions on file  Unix/MacOS system: Run: chmod u+rwx CGIfile.pl  Windows system: right-click on file, choose “Properties” Depending on setup, may need to ask administrator:  Full path to either mail or sendmail program  Location for PHP files (e.g. in public_html/cgi-bin directory)  Required filename postfix End with.php or.cgi

34 (c) 2006-2008 E.S.Boese All Rights Reserved. 34 Applets and Email Unix PHP Script <?php $request_method = $_SERVER["REQUEST_METHOD"]; if ( $request_method == "GET“ ) { $query_vars = $_GET; } elseif ( $request_method == "POST“ ) { $query_vars = $_POST; } reset ( $query_vars ); $emailbody = "New Form Submission: "; while ( list ($key, $val ) = each ( $query_vars ) ) { $emailbody = "$emailbody \n $key = $val" ; } $emailto = 'info@coffeeClubOfTheWorld.com'; $emailfrom = 'info@coffeeClubOfTheWorld.com'; $subject = "New Form Submission"; mail ( $emailto, $subject, $emailbody, "From: $emailfrom" ); ?> Either use mail or sendmail Depending on your system setup. Change the Reply- to and To email addresses to your own

35 (c) 2006-2008 E.S.Boese All Rights Reserved. 35 Applets and Email Inside your Java applet Write the same method named sendMail as used for the CGI program  Example: String s = sendMail( "http://www.cs.colostate.edu/~boese/mailer.php?message=",message );

36 (c) 2006-2008 E.S.Boese All Rights Reserved.36 Writing Files

37 (c) 2006-2008 E.S.Boese All Rights Reserved. 37 Writing Files via Applets Due to applet security restrictions, applets cannot write to files To record information send information to a CGI program  Examples: Results from a poll User information High score Blogs

38 (c) 2006-2008 E.S.Boese All Rights Reserved. 38 Writing Files via Applets #!/usr/bin/perl use CGI ':standard'; my $outfile = "mailing.out"; print header; # required first set of lines # Write to file open(OUT, ">>$outfile") or print( "Couldn't open $outfile: $!“ ); if ( param( 'message‘ ) ) { print OUT param( 'message‘ ),"\n"; } close( OUT ); print "Thanks!"; “>>” appends to the file. Use “>” instead of “>>” if you want to rewrite the file – e.g. store a new “High Score” message is the name we specified in applet after CGI in the URL Perl CGI Program

39 (c) 2006-2008 E.S.Boese All Rights Reserved. 39 Writing Files via Applets <?php $request_method = $_SERVER["REQUEST_METHOD"]; if ( $request_method == "GET“ ) { $query_vars = $_GET; } elseif ( $request_method == "POST“ ) { $query_vars = $_POST; } reset( $query_vars ); $file = $_SERVER['DOCUMENT_ROOT']. "/formData"; $fp = fopen( $file, “a“ ); while ( list ( $key, $val ) = each ( $query_vars ) ) { fputs ( $fp, "$key = $val\n"); } fclose($fp); ?> Change formData to the name of the file you want the values saved to The a is for appending to a file. If you want to overwrite it, use w PHP CGI Program

40 (c) 2006-2008 E.S.Boese All Rights Reserved. 40 Summary JEditorPane Hosting Applets Applet Parameters Configuration Files Applets and Emails Writing to Files


Download ppt "(c) 2006-2008 E.S.Boese All Rights Reserved.1 Internet Applications Chapter 15 - Lecture Slides."

Similar presentations


Ads by Google