- 浏览: 461397 次
- 性别:
- 来自: 陕西.西安
文章分类
最新评论
-
gaodadawei:
登录失败,请重试楼主,我目前遇到这样一个错误,claros i ...
James+Claros+intouch2.1配置 -
VerRan:
qq346448412 写道请问。你上一节、 用的ORACLE ...
James+Claros+intouch2.1配置 -
qq346448412:
请问。你上一节、 用的ORACLE数据库、 这一节又用的是MY ...
James+Claros+intouch2.1配置 -
paladin1988:
good,我喜欢..
Hibernate自关联关系 -
lygxy12:
请问,能给163发邮件吗?该怎么配置?我安装上面的操作,发给1 ...
James+Claros+intouch2.1配置
1. HttpServer1
2. Request
3. response
4. 用于处理servlet的类ServletProcessor1
- 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 {
- /** WEB_ROOT is the directory where our HTML and other files reside.
- * For this package, WEB_ROOT is the "webroot" directory under the working
- * directory.
- * The working directory is the location in the file system
- * from where the java command was invoked.
- */
- // shutdown command
- private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
- // the shutdown command received
- 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);
- // check if this is a request for a servlet or a static resource
- // a request for a servlet begins with "/servlet/"
- 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();
- System.exit(1);
- }
- }
- }
- }
2. Request
java 代码
- package ex02.pyrmont;
- import java.io.InputStream;
- import java.io.IOException;
- import java.io.BufferedReader;
- 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 String getUri() {
- return uri;
- }
- 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 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
- request.append((char) buffer[j]);
- }
- System.out.print(request.toString());
- uri = parseUri(request.toString());
- }
- /* implementation of the ServletRequest*/
- public Object getAttribute(String attribute) {
- return null;
- }
- public Enumeration getAttributeNames() {
- return null;
- }
- public String getRealPath(String path) {
- return null;
- }
- public RequestDispatcher getRequestDispatcher(String path) {
- return null;
- }
- public boolean isSecure() {
- return false;
- }
- public String getCharacterEncoding() {
- return null;
- }
- public int getContentLength() {
- return 0;
- }
- public String getContentType() {
- return null;
- }
- public ServletInputStream getInputStream() throws IOException {
- return null;
- }
- public Locale getLocale() {
- return null;
- }
- public Enumeration getLocales() {
- return null;
- }
- public String getParameter(String name) {
- return null;
- }
- public Map getParameterMap() {
- return null;
- }
- public Enumeration getParameterNames() {
- return null;
- }
- public String[] getParameterValues(String parameter) {
- return null;
- }
- public String getProtocol() {
- return null;
- }
- public BufferedReader getReader() throws IOException {
- return null;
- }
- public String getRemoteAddr() {
- return null;
- }
- public String getRemoteHost() {
- return null;
- }
- public String getScheme() {
- return null;
- }
- public String getServerName() {
- return null;
- }
- public int getServerPort() {
- return 0;
- }
- public void removeAttribute(String attribute) {
- }
- public void setAttribute(String key, Object value) {
- }
- public void setCharacterEncoding(String encoding)
- throws UnsupportedEncodingException {
- }
- }
3. response
java 代码
- package ex02.pyrmont;
- import java.io.OutputStream;
- import java.io.IOException;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.File;
- import java.io.PrintWriter;
- import java.util.Locale;
- import javax.servlet.ServletResponse;
- import javax.servlet.ServletOutputStream;
- 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;
- }
- /* This method is used to serve a static page */
- public void sendStaticResource() throws IOException {
- byte[] bytes = new byte[BUFFER_SIZE];
- FileInputStream fis = null;
- try {
- /* request.getUri has been replaced by request.getRequestURI */
- File file = new File(Constants.WEB_ROOT, request.getUri());
- fis = new FileInputStream(file);
- /*
- HTTP Response = Status-Line
- *(( general-header | response-header | entity-header ) CRLF)
- CRLF
- [ message-body ]
- Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
- */
- 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) {
- String errorMessage = "HTTP/1.1 404 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());
- }
- finally {
- if (fis!=null)
- fis.close();
- }
- }
- /** implementation of ServletResponse */
- public void flushBuffer() throws IOException {
- }
- public int getBufferSize() {
- return 0;
- }
- public String getCharacterEncoding() {
- return null;
- }
- public Locale getLocale() {
- return null;
- }
- public ServletOutputStream getOutputStream() throws IOException {
- return null;
- }
- public PrintWriter getWriter() throws IOException {
- // autoflush is true, println() will flush,
- // but print() will not.
- writer = new PrintWriter(output, true);
- return writer;
- }
- public boolean isCommitted() {
- return false;
- }
- public void reset() {
- }
- public void resetBuffer() {
- }
- public void setBufferSize(int size) {
- }
- public void setContentLength(int length) {
- }
- public void setContentType(String type) {
- }
- public void setLocale(Locale locale) {
- }
- }
4. 用于处理servlet的类ServletProcessor1
java 代码
- package ex02.pyrmont;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.net.URLStreamHandler;
- import java.io.File;
- import java.io.IOException;
- import javax.servlet.Servlet;
- import javax.servlet.ServletRequest;
- 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 {
- // create a URLClassLoader
- URL[] urls = new URL[1];
- URLStreamHandler streamHandler = null;
- File classPath = new File(Constants.WEB_ROOT);
- // the forming of repository is taken from the createClassLoader method in
- // org.apache.catalina.startup.ClassLoaderFactory
- String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;
- // the code for forming the URL is taken from the addRepository method in
- // org.apache.catalina.loader.StandardClassLoader class.
- urls[0] = new URL(null, repository, streamHandler);
- loader = new URLClassLoader(urls);
- }
- catch (IOException e) {
- System.out.println(e.toString() );
- }
- Class myClass = null;
- try {
- myClass = loader.loadClass(servletName);
- }
- catch (ClassNotFoundException e) {
- System.out.println(e.toString());
- }
- Servlet servlet = null;
- try {
- servlet = (Servlet) myClass.newInstance();
- servlet.service((ServletRequest) request, (ServletResponse) response);
- }
- catch (Exception e) {
- System.out.println(e.toString());
- }
- catch (Throwable e) {
- System.out.println(e.toString());
- }
- }
- }
5.处理静态资源的类
java 代码
- 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();
- }
- }
- }
6. 用于测试的servlet
java 代码
- import javax.servlet.*;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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;
- }
- }
这个servlet服务器是一个很简单的实现,只能处理很简单的功能,比如将字符创输出到网页上。。
ServletProcessor1类实现了利用自建的ClassLoader加载自己的servlet,然后实例化这个servlet实现他的service方法。。
发表评论
-
springboot学习 - hello world
2017-03-15 18:15 497引子: 开始之前允许我介绍下我认识的spr ... -
Ext显示乱码问题
2012-04-03 13:27 1155转自:http://blog.csdn.net/raren/a ... -
Hadoop学习资料
2011-10-21 10:20 853http://www.cnblogs.com/wayne101 ... -
NodeJs和 mongodb初识
2011-10-20 14:41 1077NodeJS: 提供javascirpt 实现服务器端功能的引 ... -
WebService 非阻塞模式
2011-03-30 16:05 1694package com.datastruct.sort; ... -
利用 Java dump 进行 JVM 故障诊断
2011-01-11 15:58 1485转自:http://jimmyleeee.blog.163.c ... -
LocalTransactionContainment 期间回滚了一个或多个本地事务资源。
2011-01-09 10:29 1984此问题查过很多,但是大家解决方法不一。下面列出 YuLiMin ... -
RETE 算法的描述(转)
2010-07-20 16:57 1288转自:http://www.cnblogs.com/ipoin ... -
Hermes配置
2010-02-02 18:09 1131一直报错UnmarshalException 后来发现 ... -
界面原型设计工具–Balsamiq Mockups
2009-12-09 13:31 1771原文地址:http://www.pbdigg.net/s ... -
JTA 事务使用
2009-11-23 15:20 1559业务场景: 客户下发订单后,订单到竣工需要走三个岗位1,2, ... -
webSphere 下消息驱动Bean 与队列JNDI的关联
2009-09-21 17:44 14691. 消息驱动Bean配置ejb-jar.xml ... -
Hibernate 二级缓存
2008-07-15 10:17 3266Hibernate二级缓存 1. HIbernate.cfg ... -
webService-小记
2008-03-24 18:57 1045A web service has one or more p ... -
Hessian
2008-02-16 11:16 1527Hessian is a simple binary pro ... -
利用反射机制动态将XML信息设置入对象
2007-12-05 14:23 2287引言:XML和J2EE密切的程度是不用说的了,由于我们的接口程 ... -
Action – JSP – Javascript之间的参数传递
2007-11-19 19:04 3653Action – JSP – Javascript之间的参数传 ... -
java 获取存储过程 输出参数
2007-11-13 15:21 5245connection = session.c ... -
js获得<table>的单元格信息
2007-11-08 16:41 54641. 获取表格中的某个单元格的内容 var tid= ... -
Eclipse快捷键
2007-10-23 10:47 920作用域 功能 快捷键 全 ...
相关推荐
本主题将深入探讨“一个简单的Servlet容器”的实现,参考自《深入剖析Tomcat》这本书的第二章。 Servlet容器的主要职责是接收HTTP请求,然后调用相应的Servlet来处理这些请求,并将Servlet的响应返回给客户端。在...
Servlet容器通过实现Servlet API来与Servlet交互,提供了Web应用部署、安全控制、会话管理等高级功能。 Servlet容器模型通常包含以下组件: 1. **Web应用**:一组相关的资源(HTML、CSS、JavaScript、图片、...
在讨论Servlet容器的具体实现之前,我们先来看看构成Servlet容器的关键组件。 1. **Servlet容器本身**:负责处理HTTP请求、管理Servlet实例等。 2. **Servlet实例**:实际处理客户端请求的组件。 3. **Servlet配置*...
嵌入式Servlet容器是SpringBoot中的重要组件,能够将Web服务器(例如Tomcat、Jetty或Undertow)嵌入到应用程序的内部运行,使得部署更为简单便捷。接下来我们将根据给定的文件内容,深入探讨SpringBoot配置嵌入式...
Servlet容器是Java Web技术的核心组成部分,它为Servlet提供运行环境,使得开发者无需关注底层细节,专注于业务逻辑的实现。本文以Tomcat为例,深入解析Servlet容器的工作原理。 首先,Servlet容器,如Tomcat,是一...
"JAVA WEB中Servlet和Servlet容器的区别" 在 Java Web 开发中,Servlet 和 Servlet 容器是两个非常重要的概念,但是很多人对它们的区别却不甚了解。本文将对 Servlet 和 Servlet 容器进行详细的介绍,并阐述它们...
Jetty 9是一款轻量级、高性能且开源的Servlet容器,它主要负责处理基于Java Servlet规范的应用程序。作为Java服务的一部分,Jetty9为开发者提供了高效、稳定且灵活的平台来部署和运行Web应用程序。 首先,Jetty 9...
【Servlet实现简单购物车】 Servlet是Java Web开发中的一个核心组件,主要用于处理HTTP请求和响应。在本项目中,"Servlet实现简单购物车"是指利用Servlet技术来构建一个基础的在线购物车系统。这个系统可能包括添加...
Jetty 是一个用 Java 实现、开源、基于标准的,并且具有丰富功能的 Http 服务器和 Web 容器,可以免费的用于商业行为。Jetty 这个项目成立于 1995 年,现在已经有非常多的成功产品基于 Jetty,比如 Apache Geromino...
在源码层面,我们可以深入研究Servlet容器如何实现这些功能。通常,Servlet容器会维护一个属性映射表,当调用`setAttribute()`时,它会在表中添加键值对。同时,`getAttribute()`则会根据给定的键从表中查找并返回...
- 要确保Servlet容器(如Tomcat)已经配置了Servlet的映射,例如在web.xml中添加如下配置: ```xml <servlet> <servlet-name>UploadServlet</servlet-name> <servlet-class>...
在"利用servlet监听器,系统启动时创建自定义容器简单例子"中,我们主要关注`ServletContextListener`接口。要创建一个监听器,你需要编写一个类,实现`ServletContextListener`接口,并重写它的两个方法:`context...
6. **Web容器**:为了运行JSP和Servlet,我们需要一个支持Java EE的Web容器,如Tomcat,它可以解析JSP页面并执行Servlet。 7. **项目结构**:ShopCart这个文件夹可能包含了JSP文件、Servlet类、配置文件(如web.xml...
按照一种约定俗成的称呼习惯,通常我们也把实现了servlet接口的java程序,称之为Servlet 二、Servlet的运行过程 Servlet程序是由WEB服务器调用,web服务器收到客户端的Servlet访问请求后: ①Web服务器首先检查...
Servlet是一种用Java编写的服务器端小程序,它可以在服务器上的Servlet容器内运行。Servlet容器负责管理Servlet实例,并处理来自客户端的HTTP请求。这些请求和响应都是通过标准的HTTP协议进行的。Servlet的主要功能...
tomcatTomcat(全称为Apache Tomcat)是一个开源的Java Servlet容器,由Apache软件基金会下属的Jakarta项目开发。Tomcat实现了Java Servlet、JavaServer Pages(JSP)和Java Expression Language(EL)等Java技术,...