简体   繁体   English

Java-创建Web服务器-处理传入的HTTP请求

[英]java - creating web server - handling incoming http requests

I am trying to write a web server in Java (this is my first time so an extremely simple web server). 我正在尝试用Java编写Web服务器(这是我第一次使用非常简单的Web服务器)。 I am not sure how to process the http get and post requests. 我不确定如何处理http的get和post请求。

Write now all I have is a loop with a server socket excepting connections and using a BufferedReader to take in the incoming message (as text). 现在我写的就是一个带有服务器套接字的循环(除了连接),并使用BufferedReader接收传入的消息(作为文本)。

I know that you can extend a Servlet class and make use of 我知道您可以扩展Servlet类并利用

  • protected void doGet(HttpServletRequest servlet_request, HttpServletResponse servlet_response) , and protected void doGet(HttpServletRequest servlet_request, HttpServletResponse servlet_response)
  • protected void doPost(HttpServletRequest servlet_request, HttpServletResponse servlet_response)

but I cannot work out how to transform/extract a raw incoming message from the socket into the parameters needed for the doGet() and doPost(). 但是我无法解决如何将套接字中的原始传入消息转换/提取为doGet()和doPost()所需的参数。

Would someone be able to give a basic example and/or explanation of what to do? 有人能够给出基本示例和/或说明如何做吗?

Many thanks 非常感谢

Also, as an after thought, would it be simple to extend the web server to process JSP pages? 此外,经过一番思考,将Web服务器扩展为处理JSP页面是否简单?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

for that you need 1st to know http packet format. 为此,您需要1知道http数据包格式。 this link would help 这个链接会有所帮助

once you know how the packet is formatted, you can extract method (GET/POST/...) and other required info to proceed. 一旦知道数据包的格式,就可以提取方法(GET/POST/...)和其他所需的信息以继续进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM