如何用JavaSE写一个简易的HTTP服务器

zbc12369 2012-02-28 07:40:53
请教如何用JavaSE写一个简易的HTTP服务器。如果要支持JSP,ASP或者PHP又需要如何改进?如果不能,请问其他语言可以实现否?
我是这样思考的,要自己写几个解析JSP,ASP,PHP的编译器不现实,所以我做的这个服务器当监听到动态网页请求时,把请求转发给IIS(如果请求是ASP的话),同理,如果是JSP就转发给TOMCAT,PHP就转发给Apache,然后再接收以上服务器解析之后返回的静态内容返回给客户端浏览器。那么这个转发功能用java要怎么实现呢?如果实现不了,其他语言可以否?
...全文
250 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
码无边 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bao110908 的回复:]

Java code
1.22 service-ref *
1.22.1 description *
1.22.2 display-name *
1.22.3 icon *
1.22.3.1 small-icon ?
……
[/Quote]楼上的大哥做程序应该不少于10年吧?
噢噢噢噢 2012-03-01
  • 打赏
  • 举报
回复
jdk1.6.0\sample\nio\server
zbc12369 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bao110908 的回复:]

Java code
1.22 service-ref *
1.22.1 description *
1.22.2 display-name *
1.22.3 icon *
1.22.3.1 small-icon ?
……
[/Quote]
这个是神马来的。。。
zbc12369 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]

我建议你还是别写了吧,JDK 的 sample 中有现成的,如果这都看不到还写什么呢?
[/Quote]
真有现成的?我没注意到?可以发上来看看吗,最好带例子的。谢谢
zbc12369 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lkclkc88 的回复:]

这个不需要去认真解析http协议吧。
只需要根据HTTP协议信息进行转发而已。只需要写一个中转服务而已,
首选你去尝试建一个socketServer,用于获取求情的数据信息,然后将请求的数据信息通过socket发往tomacat(jsp,其他ASP ,PHP根据情况而定),然后读取tomacat返回的数据,在通过socketServer返回到请求放下。
另外根据https协议,第一排的数据……
[/Quote]
这样做的话,如果后面带参数的话该如何处理,如果没有.jsp结尾(如servlet请求)的话该如何处理?
  • 打赏
  • 举报
回复
   1.22  service-ref  * 
1.22.1 description *
1.22.2 display-name *
1.22.3 icon *
1.22.3.1 small-icon ?
1.22.3.2 large-icon ?
1.22.4 service-ref-name 1
1.22.5 service-interface 1
1.22.6 service-ref-type ?
1.22.6 wsdl-file ?
1.22.7 jaxrpc-mapping-file ?
1.22.8 service-qname ?
1.22.9 port-component-ref *
1.22.10 (handler * | handler-chains ?) 1
a handler
1.22.10a.1 description *
1.22.10a.2 display-name *
1.22.10a.3 icon *
1.22.10a.3.1 small-icon ?
1.22.10a.3.2 large-icon ?
1.22.10a.4 handler-name 1
1.22.10a.5 handler-class 1
1.22.10a.6 init-param *
1.22.10a.6.1 description *
1.22.10a.6.2 param-name 1
1.22.10a.6.3 param-value 1
1.22.10a.7 soap-header *
1.22.10a.8 soap-role *
1.22.10a.9 port-name *

b handler-chains
1.22.10b.1 handler-chain *
1.22.10b.1.1 (service-name-pattern | port-name-pattern | protocol-bindings) ?
1.22.10b.1.2 handler +
1.22.10b.1.2.1 description *
1.22.10b.1.2.2 display-name *
1.22.10b.1.2.3 icon *
1.22.10b.1.2.3.1 small-icon ?
1.22.10b.1.2.3.2 large-icon ?
1.22.10b.1.2.4 handler-name 1
1.22.10b.1.2.5 handler-class 1
1.22.10b.1.2.6 init-param *
1.22.10b.1.2.6.1 description *
1.22.10b.1.2.6.2 param-name 1
1.22.10b.1.2.6.3 param-value 1
1.22.10b.1.2.7 soap-header *
1.22.10b.1.2.8 soap-role *
1.22.10b.1.2.9 port-name *
1.22.11 mapped-name ?
1.22.12 injection-target *
1.22.12.1 injection-target-class 1
1.22.12.2 injection-target-name 1
1.22.13 lookup-name ? [SERVLET3.0]
1.23 resource-ref *
1.23.1 description *
1.23.2 res-ref-name 1
1.23.3 res-type 1
1.23.4 res-auth 1
1.23.5 res-sharing-scope ?
1.23.6 mapped-name ?
1.23.7 injection-target *
1.23.7.1 injection-target-class 1
1.23.7.2 injection-target-name 1
1.23.8 lookup-name ? [SERVLET3.0]
1.24 resource-env-ref *
1.24.1 description *
1.24.2 resource-env-ref-name 1
1.24.3 resource-env-ref-type 1
1.24.4 mapped-name ?
1.24.5 injection-target *
1.24.5.1 injection-target-class 1
1.24.5.2 injection-target-name 1
1.24.6 lookup-name ? [SERVLET3.0]
1.25 message-destination-ref *
1.25.1 description *
1.25.2 message-destination-ref-name 1
1.25.3 message-destination-type ?
1.25.4 message-destination-usage ?
1.25.5 message-destination-link ?
1.25.6 mapped-name ?
1.25.7 injection-target *
1.25.7.1 injection-target-class 1
1.25.7.2 injection-target-name 1
1.25.8 lookup-name ? [SERVLET3.0]
1.26 persistence-context-ref *
1.26.1 description *
1.26.2 persistence-context-ref-name 1
1.26.3 persistence-unit-name ?
1.26.4 persistence-context-type ?
1.26.5 persistence-property *
1.26.5.1 name 1
1.26.5.2 value 2
1.26.6 mapped-name ?
1.26.7 injection-target *
1.26.7.1 injection-target-class 1
1.26.7.2 injection-target-name 1
1.27 persistence-unit-ref *
1.27.1 description *
1.27.2 persistence-unit-ref-name 1
1.27.3 persistence-unit-name ?
1.27.4 mapped-name ?
1.27.4 injection-target *
1.27.4.1 injection-target-class 1
1.27.4.2 injection-target-name 1
1.28 post-construct *
1.28.1 lifecycle-callback-class ?
1.28.2 lifecycle-callback-method 1
1.29 pre-destroy *
1.29.1 lifecycle-callback-class ?
1.29.2 lifecycle-callback-method 1
1.30 data-source * [SERVLET3.0]
1.30.1 description ?
1.30.2 name 1
1.30.3 class-name ?
1.30.4 server-name ?
1.30.5 port-number ?
1.30.6 database-name ?
1.30.7 url ?
1.30.8 user ?
1.30.9 password ?
1.30.10 property *
1.30.10.1 name 1
1.30.10.2 value 1
1.30.11 login-timeout ?
1.30.12 transactional ?
1.30.13 isolation-level ?
1.30.14 initial-pool-size ?
1.30.15 max-pool-size ?
1.30.16 min-pool-size ?
1.30.17 max-idle-time ?
1.30.18 max-statements ?
1.31 message-destination *
1.31.1 description *
1.31.2 display-name *
1.31.3 icon *
1.31.3.1 small-icon ?
1.31.3.2 large-icon ?
1.31.4 message-destination-name 1
1.31.5 mapped-name ?
1.31.7 lookup-name ? [SERVLET3.0]
1.32 locale-encoding-mapping-list *
1.32.1 locale-encoding-mapping +
1.32.1.1 locale 1
1.32.1.2 encoding 1
1.33 module-name * [SERVLET3.0]
1.34 absolute-ordering * [SERVLET3.0]
1.34.1 (name * | others ?) *
  • 打赏
  • 举报
回复
什么能支持 JSP 的,就省省吧,给你看看 web.xml 里有多少东西,光这个文件的解析就能把你弄摊:

光 Servlet 的部署描述符 web.xml 就有一大堆的东西

下面是我整理的 Servlet 2.5、Servlet 3.0 规范的 web.xml XML 元素层次结构:

1 表示必须出现 1 次
? 表示出现 0 次或 1 次
+ 表示至少出现 1 次
* 表示出现 0 次或多次
| 出现其中一个
0..4 出现 0 次到 4 次
[SERVLET30] Servlet 3.0 规范新增

1  web-app  1 
1.1 description *
1.2 display-name *
1.3 icon *
1.3.1 small-icon ?
1.3.2 large-icon ?
1.4 distribution *
1.5 context-param *
1.5.1 description *
1.5.2 param-name 1
1.5.3 param-value 1
1.6 filter *
1.6.1 description *
1.6.2 display-name *
1.6.3 icon *
1.6.3.1 small-icon ?
1.6.3.2 large-icon ?
1.6.4 filter-name 1
1.6.5 filter-class 1
1.6.6 async-supported ? [SERVLET3.0]
1.6.7 init-param *
1.6.7.1 description *
1.6.7.2 param-name 1
1.6.7.3 param-value 1
1.7 filter-mapping *
1.7.1 filter-name 1
1.7.2 (url-pattern | servlet-name) +
1.7.3 dispatcher 0..4
dispatcher 0..5 [SERVLET3.0]
1.8 listener *
1.8.1 description *
1.8.2 display-name *
1.8.3 icon *
1.8.3.1 small-icon ?
1.8.3.2 large-icon ?
1.8.4 listener-class 1
1.9 servlet *
1.9.1 description *
1.9.2 display-name *
1.9.3 icon *
1.9.3.1 small-icon ?
1.9.3.2 large-icon ?
1.9.4 servlet-name 1
1.9.5 (servlet-class | jsp-file) 1
(servlet-class | jsp-file) ? [SERVLET3.0]
1.9.6 init-param *
1.9.6.1 description *
1.9.6.2 param-name 1
1.9.6.3 param-value 1
1.9.7 load-on-startup ?
1.9.8 enabled ? [SERVLET3.0]
1.9.9 asyn-supported ? [SERVLET3.0]
1.9.10 run-as ?
1.9.11 sercurity-role-ref *
1.9.11.1 description *
1.9.11.2 role-name 1
1.9.11.3 role-link ?
1.9.12 multipart-config ? [SERVLET3.0]
1.9.12.1 location ? [SERVLET3.0]
1.9.12.2 max-file-size ? [SERVLET3.0]
1.9.12.3 max-request-size ? [SERVLET3.0]
1.9.12.4 file-size-threshold ? [SERVLET3.0]
1.10 servlet-mapping *
1.10.1 servlet-name 1
1.10.2 url-pattern +
1.11 session-config *
1.11.1 session-timeout ?
1.11.2 cookie-config ? [SERVLET3.0]
1.11.3 tracking-mode 0..3 [SERVLET3.0]
1.12 mime-mapping *
1.12.1 extension 1
1.12.2 mime-type 1
1.13 welcome-file-list *
1.13.1 welcome-file +
1.14 error-page *
1.14.1 (error-code | exception-type) 1
(error-code | exception-type) ? [SERVLET3.0]
1.14.2 location 1
1.15 jsp-config *
1.15.1 taglib *
1.15.1.1 taglib-uri 1
1.15.1.2 taglib-location 1
1.15.2 jsp-property-group *
1.15.2.1 description *
1.15.2.2 display-name *
1.15.2.3 icon *
1.15.2.3.1 small-icon ?
1.15.2.3.2 large-icon ?
1.15.2.4 url-pattern +
1.15.2.5 el-ignored ?
1.15.2.6 page-encoding ?
1.15.2.7 scripting-invalid ?
1.15.2.8 is-xml ?
1.15.2.9 include-prelude *
1.15.2.10 include-coda *
1.15.2.11 deferred-syntax-allowed-as-literal ?
1.15.2.12 trim-directive-whitespaces ?
1.15.2.13 default-content-type ? [SERVLET3.0]
1.15.2.14 buffer ? [SERVLET3.0]
1.15.2.15 error-on-undeclared-namespace ? [SERVLET3.0]
1.16 security-constraint *
1.16.1 display-name *
1.16.2 web-resource-collection +
1.16.2.1 web-resource-name 1
1.16.2.2 description *
1.16.2.3 url-pattern +
1.16.2.4 http-method *
(http-method + | http-method-omission +) ? [SERVLET3.0]
1.16.3 auth-constraint ?
1.16.3.1 description *
1.16.3.2 role-name *
1.16.4 user-data-constraint ?
1.16.4.1 description *
1.16.4.2 transport-guarantee 1
1.17 login-config *
1.17.1 auth-method ?
1.17.2 realm-name ?
1.17.3 form-login-config ?
1.17.3.1 form-login-page 1
1.17.3.2 form-error-page 1
1.18 security-role *
1.18.1 description *
1.18.2 role-name 1
1.19 env-entry *
1.19.1 description *
1.19.2 env-entry-name 1
1.19.3 env-entry-type 1
1.19.4 env-entry-value ?
1.19.5 mapped-name ?
1.19.6 injection-target *
1.19.6.1 injection-target-class 1
1.19.6.2 injection-target-name 1
1.19.7 lookup-name ? [SERVLET3.0]
1.20 ejb-ref *
1.20.1 description *
1.20.2 ejb-ref-type 1
1.20.3 ejb-ref-type 1
1.20.4 home 1
1.20.5 remote 1
1.20.6 ejb-link ?
1.20.7 mapped-name ?
1.20.8 injection-target *
1.20.8.1 injection-target-class 1
1.20.8.2 injection-target-name 1
1.20.9 lookup-name ? [SERVLET3.0]
1.21 ejb-local-ref *
1.21.1 description *
1.21.2 ejb-ref-type 1
1.21.3 ejb-ref-type 1
1.21.4 local-home 1
1.21.5 local 1
1.21.6 ejb-link ?
1.21.7 mapped-name ?
1.21.8 injection-target *
1.21.8.1 injection-target-class 1
1.21.8.2 injection-target-name 1
1.21.9 lookup-name ? [SERVLET3.0]
  • 打赏
  • 举报
回复
我建议你还是别写了吧,JDK 的 sample 中有现成的,如果这都看不到还写什么呢?
  • 打赏
  • 举报
回复
这个不需要去认真解析http协议吧。
只需要根据HTTP协议信息进行转发而已。只需要写一个中转服务而已,
首选你去尝试建一个socketServer,用于获取求情的数据信息,然后将请求的数据信息通过socket发往tomacat(jsp,其他ASP ,PHP根据情况而定),然后读取tomacat返回的数据,在通过socketServer返回到请求放下。
另外根据https协议,第一排的数据为(get index.jsp HTTP/1.1,没记错的话就是如此,get代表提交的方式,也可以是post,index.jsp代表请求的路径,如 http://127.0.0.1:8080/index.jsp,HTTP/1.1代表http协议的版本)中路径的信息来判断是否为jsp,asp,php(大体思路,个人之用过jsp,不知道SAP界面是否为asp结尾。)而建立特定的socket中。
可以更具数据生成相应的http提交数据,发送到web server中。
探花 2012-02-28
  • 打赏
  • 举报
回复
Http服务器 用JsonRPC 或者 XMLRPC试试看~ 应该是可以的

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧