HTTP servers allow browers to remotely fetch files but also to launch some java (or other) code that runs on the server machine and sends HTML back.
A servlet is the exact same concept--a piece of Java code that runs on the server in response to a browser request. Servlets support the request/response client/server model and are able to service HTTP GET and POST requests. They are a replacement for CGI bin and php etc...
Many dynamic pages on the net are generated by Java code on a server and spit back to the browser, which pretends everything is a static file.
Another way to think of a GET request is as a method call over the internet where method parameters are URL parameters. The return value is simply the (usually XML) data spit back by the server.
A POST request is almost the opposite of a GET request: it is meant to send data to the server from HTML forms (i.e., pages with <form> tags and submit buttons). POST pages normally process the data and then redirect the browser to another page that says anything from "thanks" to "here is your requested data" (like when doing airline reservations). You can think of GET services as display pages and POST services as processing pages.
You have one servlet execute per page for your site; each servlet acquires the appropriate data and then computes an HTML page, which is sent back to the browser.
Web servers normally follow a model-view-controller (MVC) architecture where the web server is controller, the pages collectively represent theview, and your core system usually embodies the model (such as EmailManager, ...).
What is JSP? Java Server Pages are "inside out" servlets where, instead of Java with embedded print HTML statements, you have HTML with embedded Java code snippets. We will discuss these in more detail later.
In principle, "skins" or multiple web page "looks" are possible, but they require strictly separated model and view. JSP encourages entangling model and view, thus, preventing skins. StringTemplate, something I built during years of server development, strictly enforces separation of model and view. You can trivially radically change the look of your site.
Where is the main method in a server? For most deployed applications, there isn't one. The first execution of one of your pages must launch the application initiation procedures. In our case, you will be launching Jetty, the web server, from a main method so you could in principle do whatever initialization you wanted before the first page is served.
HTTP is stateless. How can your server know when the same person comes back to the site? That is, when I click on a link on a site that takes me to another page on the same site, how does the server know that a particular GET request is from the same person? Further, how does the server track a chunk of data for each person logged into the site? For example, you want to track their name, ID, and pages visited perhaps. Sessions are used for that and we will worry about them in another lecture. But they exist, and every servlet can ask for a site visitor's session object across pages. These sessions are normally implemented via cookies, little pieces of key/value pair data stored on a person's client machine. Each request to site foo.com sends all foo.com cookies on the client machine to the server along with each GET/POST request.
See package doc:
Useful class summary:
Generates HTML that is sent back to the browser.
http servlets have methods are associated with the http protocol. GET, POST, etc... The HTTP GET request invokes doGet() method.
The servlet life cycle:
One instance of a servlet per server. Watch out for threading issues. Can tell it to be singlethreaded, but very slow. Reference example of using instance vars setting/getting from within service (see section below).
Later we'll talk about generating html properly and can discuss in detail.
How do they work? Unfortunately that really depends on the servlet container you are using. Some let you place your .java file inside a special directory and your server will be able to send URL requests to that code. The HTTP server compiles the code for you (and can replace an old one on a running server). With Jetty, it will find your servlet code from the CLASSPATH so make sure you start your app with the path set to see both jetty stuff and your stuff.
A servlet runs in the same address space as the server and typically represents a middle tier in a three tier system.
Note that the servlet receives a request and response object. The request object contains information about the HTTP request, plus parameters, and other header info. The response object lets you set response headers, cookies, and lets you write to the output stream.
Used as target of form processing. Can handle much more data (I think) in terms of parameters. request.getParameter is used to get URL parameters.
Servlet responding to form (note both doGet and doPost methods):
Use HttpUtils.getRequestURL(request) to reconstruct URL minus query (args).
Other useful methods in: getRemoteHost() http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html
Converts meaningful or special chars to ascii code.
' '->%20 etc....
Here is an example calling the doGet method using a space in arg.
Jetty is a simple embeddable or standalone web server for static pages or dynamic pages like JSP or Servlets. For our purposes, it's probably easier to have your application embed the web server directly.
Here is a simple program that starts up a web server at port 8080 and has a document root specified by args:
You will need the following additions to your CLASSPATH:
which are the jars used by Jetty. Start your server like this:
which will start serving files underneath ~/foo. URLs like host:8080/t.html will get file ~/foo/t.html from the disk.
At this point, Jetty will serve files, but you are not recording requests to your server. Add the following method:
And add this line before the server.start():
You still get a warning from Jetty, but ignore it:
To get Jetty to handle servlets:
Servlet HelloServlet would be visible at host:8080/Hello
The safety issue for servlets with instance variables can be illustrated with the following:
One thread can set the id then switch to another thread which resets id. When that second thread finishes the first thread would start up and proceed to print out the same value as the second thread. I.e., if url s?ID=1 and s?ID=2 result in simultaneous exec of this servlet, you risk seeing the same id value printed out.
JSP: autogenerated servlets from html + java page.
Can do file uploads and so on. For example, answer the challenge here:
and see source of resulting page; looks like: