写了一个java web 动态服务器,主要通过内部类来实现,动态类使用了外部类,采用了
classforname 实例化,动态类的构造方法不能带参数, 效果都出来了,分享给有需要的
朋友。判断做的不够多,
写得不够好,感谢指出缺点。以下是代码:
静态文件index.html:
<html>
<head>
<title>简单的web服务器</title>
</head>
<body>
<div align="center">简单的web静态服务器</div>
</body>
</html>
主要核心代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
/**
* web处理类
*/
class WebProcess{
private Request request;
private Response response;
public WebProcess(Request request, Response response) {
// TODO Auto-generated constructor stub
this.request=request;
this.response=response;
this.process();
}
private void process() {
// TODO Auto-generated method stub
String uri=request.getUri();
System.out.println("uri---------------------------"+uri);
String uriSuffix=uri.substring(uri.indexOf('.')+1);
System.out.println("uriSuffix------------"+uriSuffix);
if("htmlxmlhtm".indexOf(uriSuffix)>-1){
System.out.println("静态文件");
File fileToSend=new File(uri);
if(fileToSend.exists()){
System.out.println("存在文件");
try {
FileInputStream fis=new FileInputStream(fileToSend);
byte[] data=new byte[fis.available()];
fis.read(data);
//System.out.println(data.length);
response.out(new String(data));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("不存在文件");
response.out("您要访问的网页不存在");
}
}else{
System.out.println("动态文件");
//Visit v=new Visit(response,request);
File fileToSend1=new File(uri);
System.out.println("fileToSend----------------"+fileToSend1);
/* if(fileToSend1.exists()){*/
//System.out.println("类文件存在");
try {
//获取需要的的类的路径
URL url=new URL("file:\D:\Users\Administrator\Workspaces\MyEclipse 8.6\2013-08-21\src");
String classname=uri;
System.out.println("url-----------------"+url);
ClassLoader cl=new URLClassLoader(new URL[]{url});
Class d=cl.loadClass(classname);
System.out.println(d);
//System.out.println("cl-----------------------"+cl);
Object c=Class.forName(uri).newInstance();
Method method=c.getClass().getMethod("process", Response.class,Request.class);
method.invoke(c,response,request);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*}else{
System.out.println("类文件不存在");
}*/
}
}
}
/**
* 响应类
*/
class Response{
private PrintWriter writer;
public Response(OutputStream outputStream) {
// TODO Auto-generated constructor stub
this.writer=new PrintWriter(outputStream,true);
//this.out(data);
}
public void out(String data) {
// TODO Auto-generated method stub
System.out.println("out()-----------------start");
writer.println(data);
}
}
/**
* 请求类
*/
class Request{
private InputStream input;
public Request(InputStream inputStream) {
// TODO Auto-generated constructor stub
this.input=inputStream;
//this.getUri();
}
public String getUri(){
String resource=null;
System.out.println("getUri()-------------------start--");
BufferedReader in=new BufferedReader(new InputStreamReader(input));
try {
String line=in.readLine();
resource=line.substring(line.indexOf('/')+1, line.lastIndexOf('/')-5);
System.out.println("resource-----------"+resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resource;
}
}
/**
* @author Administrator
*采用了线程
*/
public class ServerWeb implements Runnable {
private static final int port = 8085;
ServerSocket serversocket;
/**
* @param args
*/
public ServerWeb(){
new Thread(this).start(); //把当前类设置为一个线程
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("run()---------------------start-----");
try {
serversocket=new ServerSocket(port);
System.out.println("serversocket--------------------"+serversocket.getLocalPort());
while(true){
Socket socket=serversocket.accept();
System.out.println("socket----------------------"+socket.getLocalPort());
Request request=new Request(socket.getInputStream());
Response response=new Response(socket.getOutputStream());
WebProcess webprocess=new WebProcess(request,response);
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ServerWeb();
}
}
独立动态类的代码:
import java.util.Date;
public class Visit {
public Visit(/*Response response, Request request*/) {
// TODO Auto-generated constructor stub
/*this.request=request;
this.response=response;*/
//this.process();
}
public void process(Response response, Request request){
response.out("动态网页"+new Date().toString());
System.out.println("process------------start");
}
}