⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpservlet.java

📁 java写的一个很小但是实用的http server
💻 JAVA
字号:
// IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT
//
// This class must only be used for TINI until all of the stuff
// required by javax.servlet.http.HttpServlet is supported by TINI.
//
// The missing elements are:
//
// java.lang.reflect.Method
// java.text.MessageFormat
//
// HttpServlet.java - TINI version of javax.servlet.http.HttpServlet.
//
// Copyright (C) 1999-2002  Smart Software Consulting
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA  94526-3079
// USA
//
// http://www.smartsc.com
//

package javax.servlet.http;

import java.io.*;
import java.util.Enumeration;

import javax.servlet.*;
import javax.servlet.http.*;

import com.smartsc.http.HttpHeaders;

public abstract class HttpServlet extends GenericServlet
implements Serializable
{
	protected void doDelete(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = req.getProtocol();
		String s1 = "Http method DELETE is not supported by this URL";
		if(s.endsWith("1.1"))
			res.sendError(405, s1);
		else
			res.sendError(400, s1);
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = req.getProtocol();
		String s1 = "Http method GET is not supported by this URL";
		if(s.endsWith("1.1"))
			res.sendError(405, s1);
		else
			res.sendError(400, s1);
	}

	protected void doOptions(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS";
		res.setHeader("Allow", s);
	}

	protected long getLastModified(HttpServletRequest req)
	{
		return -1;
	}

	private void doHead (HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{
		NoBodyResponse response = new NoBodyResponse( resp);

		doGet( req, response);
		response.setContentLength();
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = req.getProtocol();
		String s1 = "Http method POST is not supported by this URL";
		if(s.endsWith("1.1"))
			res.sendError(405, s1);
		else
			res.sendError(400, s1);
	}

	protected void doPut(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = req.getProtocol();
		String s1 = "Http method PUT is not supported by this URL";
		if(s.endsWith("1.1"))
			res.sendError(405, s1);
		else
			res.sendError(400, s1);
	}

	protected void doTrace(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = "\r\n";
		String s1 = "TRACE " + req.getRequestURI() + " " + req.getProtocol();
		for(Enumeration enumeration = req.getHeaderNames(); enumeration.hasMoreElements();)
		{
			String s2 = (String)enumeration.nextElement();
			s1 = s1 + s + s2 + ": " + req.getHeader(s2);
		}

		s1 = s1 + s;
		int i = s1.length();
		res.setContentType("message/http");
		res.setContentLength(i);
		javax.servlet.ServletOutputStream servletoutputstream = res.getOutputStream();
		servletoutputstream.print(s1);
		servletoutputstream.close();
	}

	public void service(ServletRequest servletrequest, ServletResponse servletresponse)
	throws ServletException, IOException
	{
		HttpServletRequest req;
		HttpServletResponse res;
		try
		{
			req = (HttpServletRequest)servletrequest;
			res = (HttpServletResponse)servletresponse;
		}
		catch(ClassCastException _ex)
		{
			throw new ServletException("non-HTTP request or response");
		}
		service(req, res);
	}

	protected void service(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		String s = req.getMethod();
		if(s.equals("GET"))
		{
			long lastModified = getLastModified( req);
			if (lastModified == -1)
			{
				// servlet doesn't support if-modified-since, no reason
				// to go through further expensive logic
				doGet( req, res);
			}
			else
			{
				long ifModifiedSince = req.getDateHeader(
					HttpHeaders.IF_MODIFIED_SINCE);
				if (ifModifiedSince == -1)
				{
					// if the client didn't ask for a if-modifed-since
					// no need to go further -- just do it.
					doGet( req, res);
				}
				else
				{
					// this is the most expensive path through
					// but we know we need to do it at this point
					maybeSetLastModified( res, lastModified);
					long now = System.currentTimeMillis();
					if( now < ifModifiedSince
					||  ifModifiedSince < lastModified)
					{
						doGet( req, res);
					}
					else
					{
						res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
					}
				}
			}
		}
		else if(s.equals("POST"))
		{
			doPost(req, res);
		}
		else if(s.equals("PUT"))
		{
			doPut(req, res);
		}
		else if(s.equals("DELETE"))
		{
			doDelete(req, res);
		}
		else if(s.equals("TRACE"))
		{
			doTrace(req, res);
		}
		else
		{
			String s1 = "Method " + s + " is not supported by this server.";
			res.sendError(501, s1);
		}
	}

	private void maybeSetLastModified(
		HttpServletResponse res, long lastModified)
	{
		if( !res.containsHeader( HttpHeaders.LAST_MODIFIED)
		&&  lastModified >= 0 )
		{
			res.setDateHeader( HttpHeaders.LAST_MODIFIED, lastModified);
		}
	}

	private static final String METHOD_DELETE = "DELETE";
	private static final String METHOD_GET = "GET";
	private static final String METHOD_POST = "POST";
	private static final String METHOD_PUT = "PUT";
	private static final String METHOD_TRACE = "TRACE";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -