Sunday, 5 July 2015

Extracting information from URL address

Problem Statement:
Gaurav Singhal is making a web application in which he has to get the details like login id, password. He uses HTML and servlet for this purpose. Well the purpose is solved but as Gaurav Singhal was very curious that, how does the function "String getParameter(String)" works.
He decided to make a program to to extract the HTML form data.

Ex:
"http://www.krazyGaurav.com/signup/service?username=test&pwd=test&profile=developer&role=ELITE&key=manager";

Solution:
Username: test
Password: test
Profile: developer
Role: ELITE
Key: manager

Try to solve the problem on your own, minimize your browser and give it a try.


Source Code:

import java.util.Scanner;
class GauravSinghal{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String url = sc.next();

String username,pwd,profile,role,key;
username = url.substring(url.indexOf("?username=")+10,url.indexOf("&pwd="));
pwd = url.substring(url.indexOf("&pwd=")+5,url.indexOf("&profile="));
profile = url.substring(url.indexOf("&profile=")+9,url.indexOf("&role="));
role = url.substring(url.indexOf("&role=")+6,url.indexOf("&key="));
key = url.substring(url.indexOf("&key=")+5,url.length()-1);
System.out.println("username: "+username);
System.out.println("pwd: "+pwd);
System.out.println("profile: "+profile);
System.out.println("role: "+role);
System.out.println("key: "+key);
}
}

No comments: