1

I am trying to write a web server in Java (this is my first time so an extremely simple web server). I am not sure how to process the http get and post requests.

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).

I know that you can extend a Servlet class and make use of

  • protected void doGet(HttpServletRequest servlet_request, HttpServletResponse servlet_response), and
  • 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().

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?

Any help is greatly appreciated.

SimonC
  • 6,590
  • 1
  • 23
  • 40
Saad Attieh
  • 1,396
  • 3
  • 21
  • 42
  • 1
    Is this homework/learning or for a real project? If the latter you probably should not be implementing an HTTP server yourself, instead, use something like Jetty or a servlet container like Tomcat. – SimonC Mar 28 '13 at 05:29
  • 1
    Hi sorry good question forgot to mention. It's for my own learning purposes. Basically in my course we were shown how to make use of Jetty and I was wondering whether I could make a very simple/basic implementation of it myself. – Saad Attieh Mar 28 '13 at 05:49
  • 1
    In that case, learn to love RFCs, in this case RFC 2616 which defines the HTTP protocol: http://tools.ietf.org/html/rfc2616 – SimonC Mar 28 '13 at 06:18
  • 1
    Please check, this might help [Simple HTTP Server][1] [1]: http://stackoverflow.com/questions/3732109/simple-http-server-in-java-using-only-java-se-api – Avinash K.P Mar 28 '13 at 06:30

1 Answers1

2

for that you need 1st to know http packet format. this link would help

once you know how the packet is formatted, you can extract method (GET/POST/...) and other required info to proceed.

Apurv
  • 3,723
  • 3
  • 30
  • 51
Ankit
  • 6,554
  • 6
  • 49
  • 71