• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting page contents n servlet hit

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer the javaapi about URL then u can know how to retreive the ipaddress.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this code:

Rene
[ January 08, 2003: Message edited by: Rene Larsen ]
 
Fahad tebateba
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a new method in SDK 1.4.x
Rene
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic