Ports and Protocols

As we saw in Chapter 25, you use input and output streams on sockets just as though you were reading/writing a file. The class java.net.ServerSocket lets your server program accept incoming data by spawning off a thread with a socket that you can read client requests from. The class java.net.Socket lets you send data to a server socket on another computer, or read response data that is coming back into the local host.

Servlets are a higher-level alternative to reading/writing sockets. Servlets can be used to service any request that is made via a socket (such as FTP), not just web page requests via HTTP. A servlet that talks something other than HTTP is called a “generic servlet” and it will extend the class javax.servlet.GenericServlet. The vast majority of servlets are used to serve HTTP requests. These are known as “HTTP servlets,” and they extend the class javax.servlet.http.HttpServlet.

The computer science term “protocol” means “an agreement on how to talk to each other.” Browsers and servers talk to each other using HTTP, HyperText Transfer Protocol. The browser starts the conversation, and then each end takes its turn to say something. It goes back and forth over the net like a game of tennis. Each HTTP request from a browser is replied to with a response from the server.

A commercial web server is multithreaded and typically deals with many clients (browsers) at any moment, but is either reading a request or sending the response to each. The key concept of servlets is that when you browse a page on the client, it causes the servlet to run on the server.

The servlet does whatever processing was coded, and then (usually) writes some HTML to represent the answer The web server sends that newly generated HTML back to the browser for display. Just as with a regular HTML page, a servlet can be invoked many times. A servlet can cope with several concurrent requests, and it may call another servlet or forward the original request to it for processing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.164.34