How to get the real home page of a website [duplicate] - java

This question already has answers here:
Get domain name from given url
(17 answers)
Closed 7 years ago.
I want to know if there is any way to get the real URL of a webpage. For example, I have a website named http://www.tutorialspoint.com/java/java_files_io.htm, the real/main URL is http://www.tutorialspoint.com of its main page.
Now I have another website http://www.sinhgad.edu/sinhgad-engineering-institutes/SEI-placement/index.html, its main page URL is http://www.sinhgad.edu/sinhgad-engineering-institutes/ in place of http://www.sinhgad.edu/ only.
I want to get this main page URL or real URL in Java.

In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress.getHostName() to get Hostname of the current Server name.
source.

Related

when asking for a html file from browser as part of a servlet web application, browser display 404 error [duplicate]

This question already has answers here:
JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
(3 answers)
Closed 6 years ago.
I'm new to web application development and is trying to create a very simple example of allowing user to type in their name, submit it and return the text "hello (name)" to the user.
the text box that allow user to enter their name and the submit button are located in a html file called XmlServletForm.html.
and my servlet class look like this
however, when I type in the url address http://localhost:8084/mavenwebproject/XmlServletForm.html, the browser display the 404 error message
You should try to run at port 8080.
localhost:8080/your particular url

Get windows client login user name in a servlet/JSP [duplicate]

This question already has answers here:
Retrieve current Windows user in Java EE web application for Single Sign On purposes
(4 answers)
Closed 6 years ago.
I am having one servlet where I am trying to get windows user name. I am getting the correct host name but unable to get the correct user name for different machines. I have tried to deploy that servlet in my machine and sharing the URL to different folks, when they are hitting that URL, I am getting my machine user name (windows), not others.
I have tried with below codes:
String userName = new com.sun.security.auth.module.NTSystem().getName();
and
String userName = System.getProperty(“username”);
and
System.getProperty("user.name");
In my servlet I am trying to check it, I am sharing my URL to different person but always I am getting same user name like it’s my user name but the host name I am getting correct one.
Can you please suggest me.
Update:
As Mr. BalusC pointed out, this will only work for local machines while development phase. A client-side language like JavaScript can't perform such security-risk task. When you will deploy this code/project, the
System.getenv().get("USERNAME");
will not target client's computer. It will target server system. Proof Here.
Before Update:
I tested this, and it worked.
System.getenv().get("USERNAME")
// Servlet
yourPrintWriter.println(System.getenv().get("USERNAME") + " is the username.");
source is the last answer by Dragos Roban

How to get web page source from a cookie drived web site using Java

It is easy to get web page source if it has a regular url with it.
Here is an answer for it:
How to get a web page's source code from Java
But some websites, like Sobeys. they are asking you to input your location first then you can get different flyers with different locations. It seems like it is setting up cookies for your preference and then you can get same content of flyers until you close your browser.
My question is, for this kind of webpage, how can I get the web page source of specific flyers (for exmple I want to get the flayers from Sobeys at Danforth Toronto) using Java? I can't use https://www.sobeys.com/en/flyer as it appears to be the same all the time. But how do I get the specific web page source for my flyers?
Try using java to visit https://www.sobeys.com/en/stores/sobeys-danforth/preferred as it sets the cookie you want, otherwise it returns this cookie
_carrot-core_session=bCt5WnJadHluUWdlbEVPYkVmb3JQbDc1a2dLWHFYUWw5NlVFVUtUblpRRHJLUEEyZ1MzamN2UjVIWGlSZzBEZDlDUTRxM2JkaEpZeUdNVHh2NUFvVjVxQWM4L015Zm1kaG5wVU5nUytocGRhdzhFMGZvc0pFM090ek41OTVVTy96d2ovTmNBOEdqUDk2dmllUjZoR1BDMEF3S2VCR21SM2thMmlidW01UmIyWmNaRm5sbEZkMjNhRElmakpLd3NvUGxPUTcvdDdlZTZ2UGFWN3BucXZUNkxwWW5ENjZ1TEc1WFpmTlBIT2JsODBkNlI4VHV0S3BuZy81b1JRdytoOS0tdGFpWUIydDNRb1BCeWJPN2psbGxkQT09--b4e4aab475a9d016944bcf0e1207c561e7568b78; domain=.sobeys.com; path=/; HttpOnly
Try looking here:
How to set Cookies at Http Get method using Java
Sorry I can't Help any more.

how to detect whether request is coming from browser or smartphone? [duplicate]

This question already has answers here:
Detecting Device Type in a web application
(8 answers)
Closed 9 years ago.
I am having a web application , which works fine .
now there is a particular page which gets bulky when i access the application from smartphone browser , so I wanted a situation like
if(request comes from computer browser client )
forward to bulky page in web application
else
if (request comes from smartphone )
forward to some other light page .
please put your suggestion how can i achieve this
Use the user-agent. Every connection to your server carries this header, by convenience and convention. There is no guarantee that a browser will be truthful(such as a spambot reporting itself as Chrome). You can get the user-agent as follows:
request.getHeader("User-Agent");
and then check again known user-agent strings and templates.
It's generally done by checking "User-Agent" header of Request

hitting a URL using java [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How to send HTTP request in java?
How to use java.net.URLConnection to fire and handle HTTP requests?
using java, how do i hit any url?
for instance, opening of http://www.xyz.com/node1 in a browser will tell xyz.com that node1 is hit.
so in this java program (which sends sms text say 'node1' in example above embedded in the url itself to a sms gateway server)
how do i achieve it without opening any browser or using servlet.
You can use an HttpURLConnection.
But using it directly is overkill if you just want to load the URL in question. This guide shows you how to open a URL.
Basically it boils down to:
URL url = new URL("http://www.xyz.com/node1");
URLConnection conn = url.openConnection();
conn.connect();
//...
The simplest way is to use URL http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html. For more advanced/flexible URL fetching you could use HttpClient http://hc.apache.org/httpclient-3.x/

Categories