
Company History |
Contact Us |
Education |
Tech. Tips |
Home |
|---|
Question: I have around 10000 links in my database. Now I want to check it whether some of them are dead or not. How can I test whether an URL is live or dead?
Answer: Open up an url connection and check the header of the response. Header will contain codes that mean different things such as 500 - Internal Server Error, 404 - Not Found. Code 200 means ok, so you can check each link and check the code on the header. If it is not 200, then it is not alive. Bear in mind that the link can be temporarily down so if you plan to delete urls from your database, you probably need to come up with some scheme that you feel confortable that the link is actually dead and not just temporarily unavailable.
Question: How can I connect to a HTTPS server from within a servlet? When I compile and run my servlet code I get a runtime error saying "malformed url: unknown protocol error".
Answer: You might have used java.net.URL class, which doesn't support the HTTPS protocol. You can use com.oreilly.servlet.HttpsMessage class for connecting to a https server from java any program. You can download these package from http://www.servlets.com/cos/index.html. Also take a look at the following article in javaworld at the following url: http://www.javaworld.com/javaworld/javatips/jw-javatip96_p.html
Question: Is it possible for a Java application that is running on the Windows NT workstation to get the computer name and the user name of the machine it is running on?
Answer: You can do it using the following approach:
1- Make a batch ( lets say GETWINDIR.BAT ) with the contents :
echo %COMPUTERNAME% > windlocation.txt
echo %USERNAME% > windlocation.txt
2- Run this file using Runtime.exec("GETWINDIR.BAT") function from your java program.
3- Use Standard file classes to get the contents of "windlocation.txt"
Question:What do I need to consider if I want to run my network application from behind a firewall?
Answer: One of the biggest issues that you will face is that most firewall configurations prevent inbound connections (i.e. from the public internet into your network) to arbitrary machines (and on arbitrary ports).
In practice, this will often mean that no-one from the "outside" will be able to connect to your application (in java terms, your java.net.ServerSocket instance will never see any connections).
Possible fixes for this include:
You can also sometimes get some communications to work if you assume that at least one party is not behind a firewall - Napster and GNUtella both work if either party can accept connections - but if both parties are behind firewalls, then you won't get a connection. In java terms, this means making your program simultaneously try to establish outgoing (Socket) connections while listening for incoming connections too (ServerSocket).
Question: Is there a Telnet class in Java that other java applications could use to talk the Telnet protocol?
Answer: There are several OpenSource project involving Java and Telnet. Matthias L. Jugel and Marcus Meißner wrote a very nice one that includes SSH support. If you do not like it or you miss a certain feature please look here for more information.
Question: What is "local loop back" used in networking?
Answer: 127.0.0.1 is a special IP addressed reserved for the local host, or "loopback address". It refers to the host on which your program is running. Operating systems typically implement "network" communications with the local host by a sort of software short-circuiting the ethernet hardware, effectively looping back the connection.
Question: How can I determine the subnet mask and IP broadcast address?
Answer: Java currently has no facility for determining a subnet mask. A subnet mask is used to denote which bits in an IP address are the "network" part and which bits are the "machine" part. A broadcast address has all the "machine" bits set to 1, so you need to know the subnet mask in order to programmatically determine the IP broadcast address.
Question: How can I force an applet to use HTTP/1.1?
Answer: Instead of using a URL/URLConnection, open the Socket directly, and in your request, specify the HTTP version:
out.print("GET " + urlString + " HTTP/1.1\r\n");
The HTTP protocol waits for a CRLF (indenpendently of its client's architecture) at the end of the Request Line, so don't use println.
Explicitly RFC 2616 says:
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Question: How can I retrieve the HTTP header information for a URL?
Answer:To examine the HTTP headers, you use the getHeaderField() method of URLConnection. The HTTP headers are returned separately from the content of the URL, and reading from the URL's input stream only allows you to access the content. The simple program below prints out all the headers for a URL specified as the first command-line argument:
import java.net.*;
public class PrintHeaders {
public static void main(String[] args) throws Exception {
URL url = new URL(args[0]);
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String header = connection.getHeaderField(0);
System.out.println(header);
System.out.println("---Start of headers---");
int i = 1;
while ((header = connection.getHeaderField(i)) != null) {
String key = connection.getHeaderFieldKey(i);
System.out.println(((key==null) ? "" : key + ": ") + header);
i++;
}
System.out.println("---End of headers---");
}
}
Question: I'm trying to open a URL using HttpURLConnection, but the page that I'm looking at has a redirect instruction. If I use the method setFollowRedirects() with false, I don't get the proper page, but if I set it to true, I get the message "Server redirected too many times...". What can I do?
Answer: The HTTP specification states:
A user agent SHOULD NOT automatically redirect a request more
than 5 times, since such redirections usually indicate an infinite loop.
The user agent you're using is here is HttpURLConnection, which strictly adheres to this limit. The fact that you're getting that error message indicates there's something wrong with the site setup - you should have the webmaster fix this. setFollowRedirects() just controls the automatic following of redirects - you can also follow redirects manually, by reading the "Location" header of the HTTP response to obtain the new URL then using a new instance of HttpURLConnection to open that new URL. The example below shows code which prints out what URL you are being redirected to, along with some sample output.
import java.net.*;
public class ShowRedirect {
public static void main(String[] args) throws Exception {
URL url = new URL(args[0]);
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("Response code = " + connection.getResponseCode());
String header = connection.getHeaderField("location");
if (header != null)
System.out.println("Redirected to " + header);
}
}
Using this to access the jGuru web site, we find:
> java ShowRedirect http://www.jguru.com/
Response code = 302
Redirected to http://www.jguru.com/portal/index.jsp?tab=8

Below are some Java security-related resources
Computer Svce & Repair |
Data Entry |
Web Design |
Laptops |
VMachines |
|---|