URL assigned a url while constructing the object, if you need a URL object that referes to someURL you like use something like
URL getNewURL(String spec)
{
return new URL(spec);
}
you can't chnage the URL since you contructed it for a fixed URL, goodLuck
Hi:
Thanks for your answer. So how to use getNewURL function to change url string? Is the following code correct?
URL myurl=new URL("http://www.cnn.com")// initial value of url string is http://www.cnn.com
myurl=getNewURL("http://www.yahoo.com")// change the value of url string to be
// http://www.yahoo.com
You can't change the URL object itself because it's immutable (well, for most intents and purposes anyway).
Of course you can point a reference of type URL to a new URL object, which is exactly what this solution does. However, it's important to note that the original URL object is not changed.
> myurl=getNewURL("http://www.yahoo.com")// change
> the value of url string to be
> // http://www.yahoo.com
This code will not change the contents of the Object, it will construct a new Object with the new URL you need, just like this:
myurl=new URL("http://www.yahoo.com")// constructs a new URL object and assigns its reference to myurl, doing so will remove the old object that holds the old URL
Hi:
The answer is clear. I can not change the url string of a URL object. So in order to connect a different url, I must create a new URL object. This creates a problem. Some web site is session oriented. Afte you connect to the web site and you want to get more information from the web site, you must use the inital connection. The followings are problematic codes:
URL url = new URL("http://www.somesite.com");
URLConnection connection = url.openConnection();// Initial connection
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
// then read the inital HTML
url = new URL("http://www.somesite.com/further_information.html");
connection = url.openConnection();// second connection
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
// Instead of giveing me further information I want, the web site returns error "Invalid session.", because this is a new connection.
My question is that how to use inital connection to access the web page http://www.somesite.com/further_information.html? Thanks.
Well, you question is about changing URL in URLConnection, not in URL. This is different, anyways, I didn't use URLConnection before, but it's usual to loose the session if you changed the URL explicitly, I think if you need to access the html file you don't need to change the URL!!! since you're already connected to the URL that containd your target content. After connection you then request the page by 'GET' method or something related to the HTTP protocol that I didn't work with before. Focus on the URLConnection methods or try to ask about that into the Socket programming forums or the Servlet forums, so you can find experts in this Field. GoodLuck
Regards,
Mohammed Saleem