I was just wondering if there is a method or something in Java that, once a URL has been passed, can determine if a webpage under that URL was written in HTML, PHP, XML, and so on. I looked in URL and HTTPConnection classes but didn't find anything. Maybe missed something.

Dani AI

Generated

Short answer: there is no guaranteed, programmatic way to tell what server-side language produced a page. A client (including Java code) only receives the HTTP response (headers + body). Any conclusion about "HTML vs PHP vs XML" is heuristic: inspect headers, file extensions, and the returned markup, but none of those are definitive.

As a practical Java workflow:

  • Send a HEAD request first and check response headers such as Content-Type, Server, and X-Powered-By.
  • If needed, GET the body and check for an XML prolog (<?xml), an XHTML content-type or DOCTYPE, or generator/meta tags that name a CMS.
  • Look for URL paths or asset directories that hint at a platform (for example wp-content for WordPress), and inspect inline script/link targets for extensions like .php, .jsp, etc. Treat these as clues, not proof.

Example (minimal) using HttpURLConnection:

URL u = new URL("http://example.com");
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("HEAD");
h.connect();
String ct = h.getContentType();
String server = h.getHeaderField("Server");
String xp = h.getHeaderField("X-Powered-By");
h.disconnect();

If you need the body, use GET and scan the first few KB for <?xml, <!DOCTYPE html>, or <meta name="generator">.

Notes and cautions:

  • As implied, viewing source shows only output — it does not reveal server internals. As hinted, if you are generating the response on your own server (servlet, etc.), you already control/know the content type (e.g., response.setContentType).
  • Servers and proxies can hide or lie in headers; URL rewriting removes extensions; active probing/fingerprinting may have legal/ethical implications. Use these heuristics only for passive inspection or with permission.

Recommended Answers

All 2 Replies

Good mornin' mate,

If you have internet explorer, once you viewing a webpage, simply click on 'view > Source'. That will display the HTML of the webpage. Most often you'll get code like java,javascript etc will be in seperate files used in the webpage through simple "href" links (That then obviously stops you from seeing that).

Anyways, hope you find this usefull!

Are you talking about server side? If so, what object are you using to hold the request?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.