File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes getting page contents n servlet hit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting page contents n servlet hit" Watch "getting page contents n servlet hit" New topic
Author

getting page contents n servlet hit

Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
i created a servlet, which whenever hit by any way,
it will save the ipaddress using request.getRemoteAddr() and date,
but i want to save the domain name corresponding to this ip addrsss
like www.yahoo.com or whatever with them,
thats my first problem which i m not gettting success using servlet.,

2nd problem is that, i m hitting the servlet using the following statment
in my mail wich is actually a image which hit the servlet

i-e <img src="http://www.abc.com/servlet/Mail" alt="image hitting the servle">

but in this way it just hit the servlet,
the servlet runs and only save the ipaddress and date and
other information only with request attribute.

but i want to get all the fields on this mail page with contains hidden types
i-e <input type=hidden name=hidden1>

i use enumeration object but cant success,
plz tell me how i can get all the fields on that page
even i cant get the name of the image or the alt name which hit the servlet

hope u got my point, plz assist me in this regards

looking forward n best regards
-fahad.
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
u can try this program
import java.net.*;
import java.io.*;
import java.util.*;
public class XYWebTest
{
public static void main (String[] args)
{
// initialize random seed
Random randomGen = new Random(new Date().getTime());
while(true)
{
try
{
// sleep for a random interval
Thread.currentThread().sleep(randomGen.nextInt(args.length>1?(new Integer(args[1]).intValue()):10000));
if(args.length>0)
{
// print the user supplied URL to the console window
System.out.println(args[0]);
// open URL connection
URLConnection urlConn = new URL(args[0]).openConnection();
urlConn.setUseCaches(false);
// read and print the content to the console window
InputStream in = urlConn.getInputStream();
byte buf[] = new byte[4096];
int nSize = in.read(buf);
while(nSize>=0)
{
System.out.print(new String(buf,0,nSize));
nSize = in.read(buf);
}
System.out.print("\r\n");
}
else return;
}
catch(Exception e)
{
// print error message
System.out.println("Exception: "+e.getMessage());
}
}
}
}

then compile javac *.java
and run
java XY* "http://www.yahoo.com"
Then u can view the HTMl code
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Hi
1- To get the server name you can try:
ServletRequest.getServerName()
2-

To retreive these parameters in your servlet code, you can use:
String ServletRequest.getParameter(String name)
Hope this helps
Dominic
Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
thanks for replying, and assisting
but ServletRequest.getServerName()
return the same ip address not in verbose form i-e www.yahoo.com
still not successed...
Fahad

To retreive these parameters in your servlet code, you can use:
String ServletRequest.getParameter(String name)
Hope this helps
Dominic[/qb]<hr></blockquote>
[ January 06, 2003: Message edited by: Fahad tebateba ]
Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
dear glkrr reddy,
tahnks for your program,
it some how help me,
but it works when i already known the host name as u said i have to enter at prompt
java XY* "http://www.yahoo.com"
but i want to know http://www.yahoo.com from the retreieved ip address
thanks
Fahad
Originally posted by glkrr reddy:
u can try this program

then compile javac *.java
and run
java XY* "http://www.yahoo.com"
Then u can view the HTMl code

[ January 06, 2003: Message edited by: Fahad tebateba ]
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
You can refer the javaapi about URL then u can know how to retreive the ipaddress.
Paul Done
Greenhorn

Joined: Jan 03, 2003
Posts: 22
Does this API call help you out (I was a little confused by your post as to whether you wanted the remote client's name or your local server's name):
java.net.InetAddress getLocalHost();
This returns an object representing the local [server's] address, you can then call getHostName() on the returned object. Here's the Javadoc:
If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName.
In terms of captuing HTML Form parameters, you can only do this if your browser does a HTTP Post (eg. from a FORM submit). When you use the 'a href' way, you are performing a HTTP Get. You can get around this by getting your hyperlink to use some javascript before doing a Form submit.
Paul
[ January 07, 2003: Message edited by: Paul Done ]
Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
thanks paul,
request.getServerName() return the name of the server where my servlet is present
InetAddress inetLocalHost = InetAddress.getLocalHost();
String inetHostName = inetLocalHost.getHostName();
return the host name and host ip address where my servlet is present, but i m looking for the host name equivalant to the ip which i get from String ipaddr = request.getRemoteAddr();
hope u got my point
thanks for replying
Fahad
[ January 07, 2003: Message edited by: Fahad tebateba ]
Paul Done
Greenhorn

Joined: Jan 03, 2003
Posts: 22
If you have the remote client's IP address, could you not use InetAddress.getByAddress(ip) and then call getHostName() on the result? However this may try to do a reverse DNS lookup - if that fails then there is probably no way to get the remote clients' name - in fact the remote client may not have a public name at all?
Paul
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

try this code:

Rene
[ January 08, 2003: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
dear rene
thanks
but your program having some problem
the error shown at the unavailability of the following method InetAddress.getByAddress
error as follow,
Method getByAddress(byte[]) not found in class java.net.InetAddress.
check it out or may be i wrong ,, plz let me know
Fahad
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

It is a new method in SDK 1.4.x
Rene
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Hi,
try the following code:
InetAddress inet = InetAddress.getByName("62.109.129.147"); // IP @ in question
System.out.println ("Host: " + inet.getHostName());

Hope this helps,
Bernd
SCJP
Fahad tebateba
Greenhorn

Joined: Jan 05, 2003
Posts: 10
thanks Bernd,
but your code return the same ip address not in word form which i want i-e www.yahoo.com or like this
thanks Rene, i will try 1.4.x then will chekch bcoz now i have 1.3.x
here is my code,
// this file is created by Fahad
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class Test extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws java.io.IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws java.io.IOException
{
String filename = "test.txt";
FileWriter resultsFile = new FileWriter(filename,true);
PrintWriter toFile = new PrintWriter(resultsFile);
Date date = new Date();
String ipaddr = request.getRemoteAddr();
int addspace;
String remoteHost;
String serverName;
InetAddress inet = InetAddress.getByName("203.197.129.161");
String inethostname = inet.getHostName();
//byte[] ipAdr = {(byte)203, (byte)197, (byte)129, (byte)161};
//InetAddress inetLocalHost2 = InetAddress.getByAddress(ipAdr);
//String inetHostName2 = inetLocalHost2.getHostName();
remoteHost = request.getRemoteHost();
serverName = request.getServerName();
InetAddress inetLocalHost = InetAddress.getLocalHost();
String inetHostName = inetLocalHost.getHostName();
String inetHostAddress = inetLocalHost.getHostAddress();

//toFile.println(" ipaddr => "+ipaddr+" date => "+date.toString()+" request.getRemoteHost => "+request.getRemoteHost()+" remoteHost => "+remoteHost+" serverName => "+serverName+" inetHostName => "+inetHostName+ " inetHostAddress => "+inetHostAddress+" inetHostName2 => " +inetHostName2);
toFile.println(" ipaddr => "+ipaddr+" date => "+date.toString()+" request.getRemoteHost => "+request.getRemoteHost()+" remoteHost => "+remoteHost+" serverName => "+serverName+" inetHostName => "+inetHostName+ " inetHostAddress => "+inetHostAddress+" inethostname => "+inethostname);

resultsFile.close();
toFile.close();
}
}
[ January 09, 2003: Message edited by: Fahad tebateba ]
 
Consider Paul's rocket mass heater.
 
subject: getting page contents n servlet hit
 
Similar Threads
getting parameter of image
Getting Server Name if the Current Managed Server on a Cluster Env
passing parameter from js to servlet
nslookup
Submit Methods pulling same data