<span style="font-size:14px;BACKGROUND-COLOR: #ffffff">一个简单的servlet容器由前面的应用程序演变过来,它可以出来简单的servlet和静态资源(html)。
首先我们创建一个简单的servlet类 PrimitveServlet.java
</span>import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class PrimitiveServlet implements Servlet {
public void init(ServletConfig config) throws ServletException {
System.out.println("init");
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
System.out.println("from service");
PrintWriter out = response.getWriter();
out.println("Hello. Roses are red.");
out.print("Violets are blue.");
}
public void destroy() {
System.out.println("destroy");
}
public String getServletInfo() {
return null;
}
public ServletConfig getServletConfig() {
return null;
}
}
它实现了javax.servlet.Servlet接口,Servlet编程是通过javax.servlet和javax.servlet.http这两个包的类和接口来实现的,所以的servlet必须实现或者继承实现了该接口的类。
一个全功能的servlet容器会为servlet的每个请求做下面一些工作:
servlet类加载时调用init方法,值调用一次。
每次接收到请求构造一个javax.servlet.ServletRequest实例,和一个javax.servlet.ServletResponse实例。
调用servlet的service方法,同时将构造的ServletRequest和ServletResponse对象传递给它。
servlet类被关闭时调用destroy方法卸载servlet类。
本文所写的是一个简单的servlet容器,它只做下面两件事:
1. 等待http请求
2. 构造一个javax.servlet.ServletRequest对象,和一个javax.servlet.ServletResponse对象。
应用程序由6个类组成:
HttpServer1
Request
Response
StaticResourceProcessor
ServletProcessor1
Constants
首先看HttpServer1.java
package ex02.pyrmont;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class HttpServer1 {
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
private boolean shutdown = false;
public static void main(String[] args) {
HttpServer1 server = new HttpServer1();
server.await();
}
public void await() {
ServerSocket serverSocket = null;
int port = 8080;
try {
serverSocket = new ServerSocket(port, 1, InetAddress
.getByName("127.0.0.1"));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// Loop waiting for a request
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream();
// create Request object and parse
Request request = new Request(input);
request.parse();
// create Response object
Response response = new Response(output);
response.setRequest(request);
if(request.getUri().startsWith("/servlet/")){
ServletProcessor1 processor=new ServletProcessor1();
processor.process(request,response);
}else{
StaticResourceProcessor processor=new StaticResourceProcessor();
processor.process(request,response);
}
// Close the socket
socket.close();
// check if the previous URI is a shutdown command
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
它负责等待http请求,收到请求后构造request和response,并判断请求是静态资源还是servlet,交给不同的类处理。
下面看Request.java它负责解析http请求
package ex02.pyrmont;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
public class Request implements ServletRequest{
private InputStream input;
private String uri;
public Request(InputStream input) {
this.input=input;
}
public void parse() {
// Read a set of characters from the socket
StringBuffer request = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try {
i = input.read(buffer);
} catch (IOException e) {
e.printStackTrace();
i = -1;
}
for (int j = 0; j < i; j++) {
request.append((char) buffer[j]);
}
System.out.print(request.toString());
uri = parseUri(request.toString());
}
private String parseUri(String requestString) {
int index1, index2;
index1 = requestString.indexOf(' ');
if (index1 != -1) {
index2 = requestString.indexOf(' ', index1 + 1);
if (index2 > index1)
return requestString.substring(index1 + 1, index2);
}
return null;
}
public String getUri() {
return uri;
}
}
Response.java负责响应请求,静态资源请求交给StaticResourceProcessor.java处理,servlet请求就将它交给ServletProcessor1.java进行处理
先看Response.java
package ex02.pyrmont;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Locale;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
public class Response implements ServletResponse{
private static final int BUFFER_SIZE = 1024;
Request request;
OutputStream output;
PrintWriter writer;
public Response(OutputStream output) {
this.output = output;
}
public void setRequest(Request request) {
this.request = request;
}
public void sendStaticResource() throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try {
File file = new File(Constants.WEB_ROOT, request.getUri());
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch != -1) {
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
// file not found
String errorMessage = "HTTP/1.1 404 File Not Found\r\n"
+ "Content-Type: text/html\r\n"
+ "Content-Length: 23\r\n" + "\r\n"
+ "<h1>File Not Found</h1>";
output.write(errorMessage.getBytes());
} finally {
if (fis != null)
fis.close();
}
}
@Override
public PrintWriter getWriter() throws IOException {
//true allow auto refresh
writer = new PrintWriter(output, true);
return writer;
}
}
StaticResourceProcessor.java很简单,它直接调用Response.java的sendStaticResource方法处理静态请求。
package ex02.pyrmont;
import java.io.IOException;
public class StaticResourceProcessor {
public void process(Request request, Response response) {
try {
response.sendStaticResource();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ServletProcessor1.java如下:
package ex02.pyrmont;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;
import javax.servlet.Servlet;
import javax.servlet.ServletResponse;
public class ServletProcessor1 {
public void process(Request request, Response response) {
String uri=request.getUri();
String servletName=uri.substring(uri.lastIndexOf("/")+1);
//类加载器
URLClassLoader loader=null;
try {
//资源数组,指向加载类时查找的位置
URL[] urls=new URL[1];
URLStreamHandler streamHandler=null;
File classpath=new File(Constants.WEB_ROOT);
/**
* servlet容器里面,一个类加载器可以在资源库(repository)找到要加载的servlet
* 构造一个包含资源库的字符串
*/
String repository=(new URL("file",null,classpath.getCanonicalPath()+File.separator
)).toString();
urls[0]=new URL(null,repository,streamHandler);
loader=new URLClassLoader(urls);
}
catch (IOException e) {
System.out.println(e.toString());
}
Class<?> myClass = null;
try {
//找到指定名称的servletName.class
myClass = loader.loadClass(servletName);
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
Servlet servlet=null;
try { //实例化指定名称的servlet对象
servlet=(Servlet) myClass.newInstance();
servlet.service(request,(ServletResponse) response);
}catch (Exception e) {
System.out.println(e.toString());
} catch (Throwable e) {
System.out.println(e.toString());
}
}
}
它使用类加载器,加载指定资源库里面的servlet类,实例化指定名称的servlet对象.最后调用servlet 的service方法。
Constants.java里面有个常量,用来找到servlet.class存放的位置
package ex02.pyrmont;
import java.io.File;
public class Constants {
public static final String WEB_ROOT = System.getProperty("user.dir")
+ File.separator + "webroot";
}
运行程序,在浏览器中输入http://localhost:8080/servlet/PrimitiveServlet,可以看到输出
Hello. Roses are red.
还有一行没有输出,因为out.print()方法不会刷新输出