?? webserver.java
字號:
import java.util.*;
import java.io.*;
import java.net.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.FileReader;
public class WebServer {
public static void main(String args[]) {
int i=1, PORT=8080;
ServerSocket server=null;
Socket client=null;
try {
server=new ServerSocket(PORT);
System.out.println("Web Server is listening on port "+server.getLocalPort());
for (;;) {
client=server.accept(); //接受客戶機的連接請求
new ConnectionThread(client,i).start();
i++;
}
} catch (Exception e) {System.out.println(e);}
}
}
/* ConnnectionThread類完成與一個Web瀏覽器的通信 */
class ConnectionThread extends Thread {
Socket client; //連接Web瀏覽器的socket字
int counter; //計數器
public ConnectionThread(Socket cl,int c) {
client=cl;
counter=c;
}
public void run() //線程體
{
try {
String destIP=client.getInetAddress().toString(); //客戶機IP地址
int destport=client.getPort(); //客戶機端口號
System.out.println("Connection "+counter+":connected to "+destIP+" on port "+destport+".");
String content="Connection "+counter+":connected to "+destIP+" on port "+destport+",";
writelog(content);
PrintStream outstream=new PrintStream(client.getOutputStream());
DataInputStream instream=new DataInputStream(client.getInputStream());
String inline=instream.readLine(); //讀取Web瀏覽器提交的請求信息
System.out.println("Received:"+inline);
if (getrequest(inline)) { //如果是GET請求
String filename=getfilename(inline);
File file=new File(filename);
if (file.exists()) { //若文件存在,則將文件送給Web瀏覽器
String sid=getsessionid(inline);//獲取web瀏覽器提交的sessionid
sid=session(sid);//處理sessionid
System.out.println(filename+" requested.");
outstream.println("HTTP/1.0 200 OK");
outstream.println("MIME_version:1.0");
outstream.println("Content_Type:text/html");
int len=(int)file.length();
outstream.println("Content_Length:"+len);
outstream.println("");
sendfile(outstream,file); //發送文件
outstream.flush();
} else { //文件不存在時
String notfound="<html><head><title>Not Found</title></head><body><h1>Error 404-file not found</h1></body></html>";
outstream.println("HTTP/1.0 404 no found");
outstream.println("Content_Type:text/html");
outstream.println("Content_Length:"+notfound.length()+2);
outstream.println("");
outstream.println(notfound);
outstream.flush();
}
}
long m1=1;
while (m1<11100000) {m1++;} //延時
client.close();
} catch (IOException e) {
System.out.println("Exception:"+e);
}
}
/* 獲取請求類型是否為“GET” */
boolean getrequest(String s) {
if (s.length()>0)
{
if (s.substring(0,3).equalsIgnoreCase("GET")) return true;
}
return false;
}
/* 獲取要訪問的文件名 (可以判斷三種請求方式)*/
String getfilename(String s) {
String f=s.substring(s.indexOf(' ')+1);
int i=f.indexOf(";");
if(i!=-1)
{f=f.substring(0,i);/*GET 語句為:GET /index.html;jssession=57433735734 HTTP/1.1 */
if (f.charAt(0)=='/')
f=f.substring(1);
}
else
{
f=f.substring(0,f.indexOf(' ')); /*GET 語句為:GET /index.html HTTP/1.1 */
if (f.charAt(0)=='/')
f=f.substring(1);
}
if (f.equals(""))
f="index.html";/*GET 語句為:GET / HTTP/1.1 */
return f;
}
static void writelog(String content) {
String fileName = "./log.txt";
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String logcontent = content+df.format(date).toString();
//按方法A追加文件
appendMethodA(fileName, logcontent);
appendMethodA(fileName, "\n\r");
//顯示文件內容
}
/*使用RandomAccessFile方法追加文件*/
public static void appendMethodA(String fileName, String content){
try {
// 打開一個隨機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件長度,字節數
long fileLength = randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e){
e.printStackTrace();
}
}
/* 獲取sessionid */
String getsessionid(String s)
{String sid=s;
int i=sid.indexOf("=");
try{
if(i!=-1)
{sid=sid.substring(i+1);
sid=sid.substring(0,sid.indexOf(' '));
}
else
sid=null;
}catch(Exception e){
System.out.println("e.toString()");
}
System.out.println("sessionid="+sid);
return sid;
}
/*把指定文件發送給Web瀏覽器 */
void sendfile(PrintStream outs,File file) {
try {
DataInputStream in=new DataInputStream(new FileInputStream(file));
int len=(int)file.length();
byte buf[]=new byte[len];
in.readFully(buf);//將文件數據讀入buf中
outs.write(buf,0,len);
outs.flush();
in.close();
} catch (Exception e) {
System.out.println("Error retrieving file.");
System.exit(1);
}
}
// session處理
String session(String sessionid)
{ Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timenow =df.format(date).toString();
ghashtable HT=new ghashtable();
String sid=sessionid;
if(sid==null)
{ sid= createsession( timenow, HT.HT);
System.out.println("create a a new session and the sessionid is "+sid);
}
else
{try{
if(searchsession(sid,HT.HT))
{System.out.println("yes! this session cell exsit");}
else
{sid= createsession( timenow,HT.HT);
System.out.println("cannot find the session information,now create a new!");
System.out.println("create a a new session and the sessionid is "+sid);
}}catch(Exception e){System.out.println(e.toString());}
}
return sid;
}
//創建session(用hashtable實現)
String createsession(String timenow,Hashtable ht)
{
String s=timenow;
//Hashtable cht= ht;
ssvalue va=new ssvalue();
sskey ke= new sskey();
va.s1=s;
String temp=(String)ke.sskey();
ht.put(temp,va.ssvalue());
return temp;
}
//搜索session信息 用get()實現
boolean searchsession(String sessionid,Hashtable ht)
{ String sid= sessionid;
// Hashtable sht=ht;
String temp=(String)ht.get(sid);
if (temp==null)
{return false;}
else
{return true;}
}
}
//定義加入hashtable(必須以類的形式)中的值
class ssvalue{
String s1;
String ssvalue()
{
return s1;
}
}
//定義加入hashtable(必須以類的形式)中的檢索碼(這就是sessionid)
//產生8位隨機字符串
class sskey
{
String sskey()
{
char[] cc = new char[10];
for (int i = 0; i<cc.length; i++)
{
while(cc[i]<'A'||cc[i]>'Z')
cc[i]=(char)(Math.random()*(int)'Z');
}//end for
String str = new String("");
for (int i = 0; i<cc.length; i++)
str += cc[i];
return str;
}
}
//用類來解決多線程公用hashtable的問題
class ghashtable{
static final Hashtable HT=new Hashtable();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -