• Post Reply Bookmark Topic Watch Topic
  • New Topic

hi  RSS feed

 
fh fh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have these code wich need to refatoring i do same answer but i not shor if any bady can help me to tell if i do will or if i must tray agin the code then the refactorin code
import java.io.*;
import java.net.*;
import java.util.*;

/**
* Simple Sequential Web server
* All the different activities of a server are done in the main() sequentially as can be seen in the code
* below. These activites are: serving a request, logging to the log file and updating the hit counter in the
* hitcounter.txt - the counter in this file is updated every time a request is received.
*
* Usage: java SimpleWebServer <port number> &
* example: $ java SimpleWebServer 3030 &
* Create a hitcounter.txt file using vi first line having 0 before you start the server
*/
public class WebServer {
public static void main(String args[]) throws Exception{
int port = Integer.parseInt(args[0]);
ServerSocket ss = new ServerSocket(port);

Socket clientSocket;
while (true) {
clientSocket = ss.accept();
//use the clientSocket to communiate with the client
InputStreamReader isr =
new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(isr);

String request = br.readLine();
System.out.println(request);
//Parse the HTTP GET request to find the file name
String file = request.substring(request.indexOf("/")+1, request.indexOf("HTTP")-1);

//read the requested file
BufferedReader fbr = null;
try {
fbr = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException fnfex) {
//ShowFileNotFound400Error();
}
StringBuilder sb = new StringBuilder();
String line;
//collect the lines from the file in a buffer - the sb
while ((line = fbr.readLine()) != null)
sb.append(line + "\n");
fbr.close();

//write the file - the response back to the client
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());
pw.write(sb.toString());
pw.close();


//continued on next page
//get various info which we need for logging such as timestamp,
//IP address of client and status of request
//find the IP address of client
InetAddress clientAddress = clientSocket.getInetAddress();
String clientIPAddress = clientAddress.getHostAddress();

//close the client socket
clientSocket.close();



//refactor
//=============== Logging to log file ============================
//now log the request to the log file - there will be a new log file for every day for example -
//20060326_log.txt use the Calendar to assemble today's date to get the name of the log file
Calendar cal = Calendar.getInstance();
String strDate = "" + cal.get(Calendar.YEAR) + (cal.get(Calendar.MONTH)+1) +
cal.get(Calendar.DAY_OF_MONTH);

//the timestamp which we need for the log
String strTimestamp = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR)
+"/" +(cal.get(Calendar.MONTH)+1);
strTimestamp = strDate+ " " + cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + ":"
+ cal.get(Calendar.MILLISECOND);

//use the date to build the name for the current log file
String currentLogFile = strDate + "_log.txt";

//assemble the log message the log file has information about one request on one line
//the format of the line in the log file is as follows
//<timestamp>|<IP address of client>|<the request>
String logMessage = strTimestamp + "|" + clientIPAddress + "|" +request;
//log the request to the file using the FileWriter -
//file is opened in append mode to prevent overwriting
FileWriter logWriter = new FileWriter(currentLogFile, true);
logWriter.write(logMessage + "\n");
logWriter.close();



//refactor
//=============== Updating the hit counter ========================
//now update the hit counter in the hitcounter.txt
//there is only one line in hitcounter.txt which has the number of hits to the web server -
//updating involves both reading and writing
BufferedReader hcReader = new BufferedReader(new FileReader("hitcounter.txt"));
String strHits = hcReader.readLine();
hcReader.close();
//increment the hitcounter and write it back to the file again
int hits = Integer.parseInt(strHits) + 1;
//append mode is not used since we want to overwrite
PrintWriter hitCounterWriter = new PrintWriter("hitcounter.txt");
hitCounterWriter.println("" + hits);
hitCounterWriter.close();
}
}

}

the refatorcodes
1
import java.io.*;
import java.net.*;
import java.util.*;
public class Updating extends WebServer
{
public String doUpdate() throws Exception
{
BufferedReader hcReader = new BufferedReader(new FileReader("hitcounter.txt"));
String strHits = hcReader.readLine();
hcReader.close();
//increment the hitcounter and write it back to the file again
int hits = Integer.parseInt(strHits) + 1;
//append mode is not used since we want to overwrite
PrintWriter hitCounterWriter = new PrintWriter("hitcounter.txt");
hitCounterWriter.println("" + hits);
hitCounterWriter.close();
}
}
2
import java.io.*;
import java.net.*;
import java.util.*;
public class Logging extends WebServer {
//=============== Logging to log file ============================
//now log the request to the log file - there will be a new log file for every day for example -
//20060326_log.txt use the Calendar to assemble today's date to get the name of the log file
Calendar cal = Calendar.getInstance();
String strDate = "" + cal.get(Calendar.YEAR) + (cal.get(Calendar.MONTH)+1) +
cal.get(Calendar.DAY_OF_MONTH);

//the timestamp which we need for the log
String strTimestamp = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR)
+"/" +(cal.get(Calendar.MONTH)+1);
strTimestamp = strDate+ " " + cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + ":"
+ cal.get(Calendar.MILLISECOND);

//use the date to build the name for the current log file
String currentLogFile = strDate + "_log.txt";

//assemble the log message the log file has information about one request on one line
//the format of the line in the log file is as follows
//<timestamp>|<IP address of client>|<the request>
String logMessage = strTimestamp + "|" + clientIPAddress + "|" +request;
//log the request to the file using the FileWriter -
//file is opened in append mode to prevent overwriting
FileWriter logWriter = new FileWriter(currentLogFile, true);
logWriter.write(logMessage + "\n");
logWriter.close();

}
3
import java.io.*;
import java.net.*;
import java.util.*;
public class Updating extends WebServer
{
public String doUpdate() throws Exception
{
BufferedReader hcReader = new BufferedReader(new FileReader("hitcounter.txt"));
String strHits = hcReader.readLine();
hcReader.close();
//increment the hitcounter and write it back to the file again
int hits = Integer.parseInt(strHits) + 1;
//append mode is not used since we want to overwrite
PrintWriter hitCounterWriter = new PrintWriter("hitcounter.txt");
hitCounterWriter.println("" + hits);
hitCounterWriter.close();
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!