Lixas
06-03-2006, 03:09 AM
Hello all,
Please help me to write java program that gets a parameters from url. Example:
url: http://localhost:1234/command?key1=value1&key2=value2
i need to have a hashmap of keys and values
I can use only these libraries:
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
I'm very new to java, so please help me.
here is my code:
import java.io.*;
import java.lang.*;
import java.lang.Object;
import java.net.*;
import java.util.*;
//import java.lang.String;
public abstract class HTTPServer
{
private Socket connectionSocket; //connection with the client
public boolean server_running = false;
// DataInputStream dis = null;
String record = null;
private main_class first;
public HTTPServer()
{ // constructor
first = new main_class();
}
public void server()
{
try {
ServerSocket welcomeSocket = new ServerSocket(8001); // Open the serversocket on port 8001
while (true) //true always
{
connectionSocket = welcomeSocket.accept(); // Create the client connection socket
// InetAddress ip = connectionSocket.getInetAddress(); //geting ip whoes connected
// System.out.println("INFO: " + ip + " has connected");
PrintWriter out = new PrintWriter(connectionSocket.getOutputStream()); //create an instance of PrintWriter to be ready to sent html code to the client
BufferedReader in = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())); //prepare a reader for communicating with the client
String clientRequest = in.readLine(); //read the client request
int httpIndex; // The index of the word 'HTTP' in the client request
int backSlashIndex; // The index of the '/' in the client request
httpIndex = clientRequest.indexOf("HTTP"); // Find index of "HTTP"
backSlashIndex = clientRequest.indexOf("/"); // Find index of first "/"
String request = clientRequest.substring(backSlashIndex+1, httpIndex-1); // Pick up the request
System.out.println("Request was: " + request +" ");
// and so on.......................................................
So, i have request extracted from http get method in variable "request".
now please help me to to do like this:
if request is equal: "turn_server_on?key1=value1&key2=value2"
string command should be "turn_server_on"
hasmap with keys and vales
I hope you understand what i want to do.
Any help would be very valuable
Please help me to write java program that gets a parameters from url. Example:
url: http://localhost:1234/command?key1=value1&key2=value2
i need to have a hashmap of keys and values
I can use only these libraries:
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
I'm very new to java, so please help me.
here is my code:
import java.io.*;
import java.lang.*;
import java.lang.Object;
import java.net.*;
import java.util.*;
//import java.lang.String;
public abstract class HTTPServer
{
private Socket connectionSocket; //connection with the client
public boolean server_running = false;
// DataInputStream dis = null;
String record = null;
private main_class first;
public HTTPServer()
{ // constructor
first = new main_class();
}
public void server()
{
try {
ServerSocket welcomeSocket = new ServerSocket(8001); // Open the serversocket on port 8001
while (true) //true always
{
connectionSocket = welcomeSocket.accept(); // Create the client connection socket
// InetAddress ip = connectionSocket.getInetAddress(); //geting ip whoes connected
// System.out.println("INFO: " + ip + " has connected");
PrintWriter out = new PrintWriter(connectionSocket.getOutputStream()); //create an instance of PrintWriter to be ready to sent html code to the client
BufferedReader in = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())); //prepare a reader for communicating with the client
String clientRequest = in.readLine(); //read the client request
int httpIndex; // The index of the word 'HTTP' in the client request
int backSlashIndex; // The index of the '/' in the client request
httpIndex = clientRequest.indexOf("HTTP"); // Find index of "HTTP"
backSlashIndex = clientRequest.indexOf("/"); // Find index of first "/"
String request = clientRequest.substring(backSlashIndex+1, httpIndex-1); // Pick up the request
System.out.println("Request was: " + request +" ");
// and so on.......................................................
So, i have request extracted from http get method in variable "request".
now please help me to to do like this:
if request is equal: "turn_server_on?key1=value1&key2=value2"
string command should be "turn_server_on"
hasmap with keys and vales
I hope you understand what i want to do.
Any help would be very valuable