File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Java newbie question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java newbie question" Watch "Java newbie question" New topic

Java newbie question

tyler durt

Joined: Nov 08, 2009
Posts: 3
I wanted to do this task,of logging into a website with a username and a password.Once i am there on the page i want to simulate a click on a link(dont know if simulate is the right word.. ).The click would give me a page where there is data arranged in a table.This data has to be stored in a text file.

Once this is done a simple logout would finish my task.

I am not sure how and using what technologies should i accomplish this task.Can someone help me with the approach that i should take to do this task.

Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Basically what you need to do is send GET/POST requests to the page, provided that it's a simple HTML page, no fancy Javascript/Flash buttons, etc. I'd recommend using the Jakarta Commons HttpClient!

OCJP 6 (93%)
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43350
Welcome to JavaRanch

Please always tell us what the thread is about. "Java newbie question" doesn't give us much inspiration to answer . Also, I think the "newbie" bit is inaccurate; this is a question beyond the "beginning" stage, and GET/POST looks like something out of a servlet, so I shall move this post thither. If I was mistaken about the location, it may move again, but you can still find the thread from "beginning"
tyler durt

Joined: Nov 08, 2009
Posts: 3

I am up with the HTTPClient module,but was stuck with a few things.

1.My code is supposed to login to a site and get the data,but here what i see is a html source which is nothing but a login page.

2.How can i click on a particular link on a page.

Here's the code that i was trying to use:

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.HttpAuthRealm;
import org.apache.commons.httpclient.methods.*;

public class SecureGetPageExample {
public static void main( String[] args ) {
/*if( args.length < 3 ) {
System.out.println( "Usage: java GetPageExample URL username password" );
System.exit( 0 );
String url = "";
String username = "";
String password = "";
try {
HttpClient client = new HttpClient();
new HttpAuthRealm(password, username),
new UsernamePasswordCredentials( username, password ) );
GetMethod method = new GetMethod( url );
method.setFollowRedirects( true );

// Execute the GET method
int statusCode = client.executeMethod( method );
if( statusCode != -1 ) {
String contents = method.getResponseBodyAsString();
System.out.println( contents );

catch( Exception e ) {
Consider Paul's rocket mass heater.
subject: Java newbie question
It's not a secret anymore!