如何取得浏览器中完整的URL地址!

解决方案 »

  1.   

    // WebBrowserPane.java
    // WebBrowserPane is a simple Web-browsing component that 
    // extends JEditorPane and maintains a history of visited URLs.
    // Java core packages
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;import javax.swing.JEditorPane;public class WebBrowserPane extends JEditorPane {
       
       private List history = new ArrayList();
       private int historyIndex;
       
       // WebBrowserPane constructor
       public WebBrowserPane() 
       {
          // disable editing to enable hyperlinks
          setEditable( false );
       }
       
       // display given URL and add it to history
       public void goToURL( URL url )
       { 
          displayPage( url );
          history.add( url ); 
          historyIndex = history.size() - 1;
       }
       
       // display next history URL in editorPane
       public URL forward()
       {
          historyIndex++;
          
          // do not go past end of history
          if ( historyIndex >= history.size() )
             historyIndex = history.size() - 1;
          
          URL url = ( URL ) history.get( historyIndex );
          displayPage( url );
             
          return url;
       }
       
       // display previous history URL in editorPane
       public URL back()
       {
          historyIndex--;
          
          // do not go past beginning of history
          if ( historyIndex < 0 )
             historyIndex = 0;
          
          // display previous URL
          URL url = ( URL ) history.get( historyIndex );
          displayPage( url );      return url;      
       }
       
       // display given URL in JEditorPane
       private void displayPage( URL pageURL )
       {
          // display URL
          try {
             setPage( pageURL );
          }      // handle exception reading from URL
          catch ( IOException ioException ) {
             ioException.printStackTrace();
          }
       }     
    }
      

  2.   

    // WebToolBar.java
    // WebToolBar is a JToolBar subclass that contains components
    // for navigating a WebBrowserPane. WebToolBar includes back
    // and forward buttons and a text field for entering URLs.
    // Java core packages
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.MalformedURLException;
    import java.net.URL;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;public class WebToolBar extends JToolBar 
       implements HyperlinkListener {
       
       private WebBrowserPane webBrowserPane;
       private JButton backButton;
       private JButton forwardButton;
       private JTextField urlTextField;
       
       // WebToolBar constructor
       public WebToolBar( WebBrowserPane browser ) 
       {
          super( "Web Navigation" );
          
          // register for HyperlinkEvents
          webBrowserPane = browser;
          webBrowserPane.addHyperlinkListener( this );
          
          // create JTextField for entering URLs
          urlTextField = new JTextField( 25 );      
          urlTextField.addActionListener(
             new ActionListener() {
                
                // navigate webBrowser to user-entered URL
                public void actionPerformed( ActionEvent event )
                {
                   // attempt to load URL in webBrowserPane
                   try {
                      URL url = new URL( urlTextField.getText() ); 
                      webBrowserPane.goToURL( url );
                   }               // handle invalid URL
                   catch ( MalformedURLException urlException ) {
                      urlException.printStackTrace();
                   }
                }
             }
          );
          
          // create JButton for navigating to previous history URL
          backButton = new JButton( new ImageIcon( 
             getClass().getResource( "images/back.gif" ) ) );
          
          backButton.addActionListener(
             new ActionListener() {
                
                public void actionPerformed( ActionEvent event )
                {
                   // navigate to previous URL
                   URL url = webBrowserPane.back();
                   
                   // display URL in urlTextField
                   urlTextField.setText( url.toString() );
                }
             }
          );
          
          // create JButton for navigating to next history URL
          forwardButton = new JButton( new ImageIcon(
             getClass().getResource( "images/forward.gif" ) ) );
          
          forwardButton.addActionListener(
             new ActionListener() {
                
                public void actionPerformed( ActionEvent event )
                {
                   // navigate to next URL
                   URL url = webBrowserPane.forward();
                   
                   // display new URL in urlTextField
                   urlTextField.setText( url.toString() );
                }
             }
          );            // add JButtons and JTextField to WebToolBar
          add( backButton );
          add( forwardButton );
          add( urlTextField );
          
       } // end WebToolBar constructor
       
       // listen for HyperlinkEvents in WebBrowserPane
       public void hyperlinkUpdate( HyperlinkEvent event )
       {
          // if hyperlink was activated, go to hyperlink's URL
          if ( event.getEventType() == 
             HyperlinkEvent.EventType.ACTIVATED ) {
                
             // get URL from HyperlinkEvent
             URL url = event.getURL();
             
             // navigate to URL and display URL in urlTextField
             webBrowserPane.goToURL( url );
             urlTextField.setText( url.toString() );
          }
       }   
    }
      

  3.   

    // WebBrowser.java
    // WebBrowser is an application for browsing Web sites using
    // a WebToolBar and WebBrowserPane.
    // Java core packages
    import java.awt.BorderLayout;
    import java.awt.Container;import javax.swing.JFrame;
    import javax.swing.JScrollPane;public class WebBrowser extends JFrame {
       
       private WebToolBar toolBar;
       private WebBrowserPane browserPane;
       
       // WebBrowser constructor
       public WebBrowser()
       {     
          super( "Deitel Web Browser" );
          
          // create WebBrowserPane and WebToolBar for navigation
          browserPane = new WebBrowserPane();      
          toolBar = new WebToolBar( browserPane );
          
          // lay out WebBrowser components
          Container contentPane = getContentPane();
          contentPane.add( toolBar, BorderLayout.NORTH );  
          contentPane.add( new JScrollPane( browserPane ), 
             BorderLayout.CENTER );
       }
       
       // execute application
       public static void main( String args[] )
       {
          WebBrowser browser = new WebBrowser();
          browser.setDefaultCloseOperation( EXIT_ON_CLOSE );
          browser.setSize( 640, 480 );
          browser.setVisible( true );
       }
    }
      

  4.   

    jsp中用这个
    request.getServerName()+request.getServletPath()