所以servlet容器,就是监听socket端口,根据请求的URL,实例化相关的servlet,并执行该servlet实例,将Request参数和Response参数传给servlet,
HttpServlet1类
public class HttpServer1 {
public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator +"webroot";
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
private boolean shutdown = false;
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
HttpServer1 server = new HttpServer1();
server.await();
}
public void await(){
int port =80;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port, 1,InetAddress.getByName("127.0.0.1"));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
while(!shutdown){
Socket socket = null;
InputStream input = null;
OutputStream out = null;
try{
socket = serverSocket.accept();
input = socket.getInputStream();
out = socket.getOutputStream();
Request request = new Request(input);
request.parse();;
Response response = new Response(out);
response.setRequest(request);
if(request.getUri().startsWith("/servlet/")){
ServletProcessor1 processor1 = new ServletProcessor1();
processor1.process(request,response);
}else{
StaticResourceProcessor staticResourceProcessor = new StaticResourceProcessor();
staticResourceProcessor.process(request,response);
}
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Request类,该类是实现javax.servlet.ServletRequest
public class Request implements ServletRequest {
private InputStream input;
private String uri;
public Request(InputStream input){
this.input = input;
}
public void parse(){
StringBuffer request = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try{
i = input.read(buffer);
}catch (Exception e){
e.printStackTrace();;
i = -1;
}
for(int j = 0; jchar)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 this.uri;
}
public Object getAttribute(String s) {
return null;
}
public Enumeration getAttributeNames() {
return null;
}
public String getCharacterEncoding() {
return null;
}
public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
}
public int getContentLength() {
return 0;
}
public long getContentLengthLong() {
return 0;
}
public String getContentType() {
return null;
}
public ServletInputStream getInputStream() throws IOException {
return null;
}
public String getParameter(String s) {
return null;
}
public Enumeration getParameterNames() {
return null;
}
public String[] getParameterValues(String s) {
return new String[0];
}
public Map getParameterMap() {
return null;
}
public String getProtocol() {
return null;
}
public String getScheme() {
return null;
}
public String getServerName() {
return null;
}
public int getServerPort() {
return 0;
}
public BufferedReader getReader() throws IOException {
return null;
}
public String getRemoteAddr() {
return null;
}
public String getRemoteHost() {
return null;
}
public void setAttribute(String s, Object o) {
}
public void removeAttribute(String s) {
}
public Locale getLocale() {
return null;
}
public Enumeration getLocales() {
return null;
}
public boolean isSecure() {
return false;
}
public RequestDispatcher getRequestDispatcher(String s) {
return null;
}
public String getRealPath(String s) {
return null;
}
public int getRemotePort() {
return 0;
}
public String getLocalName() {
return null;
}
public String getLocalAddr() {
return null;
}
public int getLocalPort() {
return 0;
}
public ServletContext getServletContext() {
return null;
}
public AsyncContext startAsync() throws IllegalStateException {
return null;
}
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
return null;
}
public boolean isAsyncStarted() {
return false;
}
public boolean isAsyncSupported() {
return false;
}
public AsyncContext getAsyncContext() {
return null;
}
public DispatcherType getDispatcherType() {
return null;
}
}
Response类实现javax.servlet.Response
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(HttpServer1.WEB_ROOT,request.getUri());
if(file.exists()){
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);
}
}else {
String errorMessage = "HTTP/1.1 302 File Not Found\r\n"+
"Content-Type: text/html \r\n" +
"Content-Length: 23\r\n"+
"\r\n"+
"File Not Found
";
output.write(errorMessage.getBytes());
}
}catch (Exception e){
System.out.println(e.toString());
}finally {
if(fis !=null){
fis.close();
}
}
}
public String getCharacterEncoding() {
return null;
}
public String getContentType() {
return null;
}
public ServletOutputStream getOutputStream() throws IOException {
return null;
}
public PrintWriter getWriter(){
writer = new PrintWriter(output,true);
return writer;
}
public void setCharacterEncoding(String s) {
}
public void setContentLength(int i) {
}
public void setContentLengthLong(long l) {
}
public void setContentType(String s) {
}
public void setBufferSize(int i) {
}
public int getBufferSize() {
return 0;
}
public void flushBuffer() throws IOException {
}
public void resetBuffer() {
}
public boolean isCommitted() {
return false;
}
public void reset() {
}
public void setLocale(Locale locale) {
}
public Locale getLocale() {
return null;
}
}
ServletProcessor1类,该类动态实例化所请求的servlet类
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(Constant.WEB_ROOT);
String repository = (new URL("file",null,classpath.getCanonicalPath()+File.separator)).toString();
urls[0] = new URL(null,repository,streamHandler);
loader = new URLClassLoader(urls);
}catch (Exception e){
e.printStackTrace();
}
Class myClass =null;
try{
// myClass = loader.loadClass(servletName);
myClass = Class.forName("com.daiyutage.PrimitiveServlet");
}catch (Exception e){
e.printStackTrace();
}
Servlet servlet = null;
try{
servlet = (Servlet)myClass.newInstance();
servlet.service(request,response);
}catch (Exception e){
}
}
}
StaticResourceProcessor 静态资源处理类
public class StaticResourceProcessor {
public void process(Request request,Response response){
try{
response.sendStaticResource();
}catch (Exception e){
}
}
}
总结:这些类和第一章中的多了一些处理,其实javax.servlet.Servlet接口是servlet单元的定义,我们对web请求的具体处理,就在servlet中,只需要resquest和response两个参数,这两个参数分别拥有socket的InputStream和OutputStream流。而tomcat和servlet的关系就是,tomcat负责监听端口,收到请求的数据,然后将将输入的流封装到Request,将输出的流封装到Response。然后负责实例化每个servlet处理单元
Servlet接口
定义接口是为了让tomcat实例化servlet后,调用init方法,在这个方法中可以加入自己的一些初始化处理
public interface Servlet {
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
void destroy();
}