To see these, browse to http://sf08.cs.umb.edu:11600/ and follow the link to Example Servlets
Source Code for HelloWorld Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
Execution: User navigates to (say from link on tomcat’s home page)
http://sf08.cs.umb.edu:11600/examples/servlets/servlet/HelloWorldExample
Browser connects to sf08.cs.umb.edu on TCP port 11600
GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1
… (header)
<html>
<head>
<title>Hello
World!</title>
</head>
<h1>Hello
World!</h1>
</body>
</html>
The browser displays HTML:
Note: The actual servlet source code has additional code to supply an image on the right.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestInfo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Request Information Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>Request Information Example</h3>");
out.println("Method: " + request.getMethod());
out.println("Request URI: " + request.getRequestURI());
out.println("Protocol: " + request.getProtocol());
out.println("PathInfo: " + request.getPathInfo());
out.println("Remote Address: " + request.getRemoteAddr());
out.println("</body>");
out.println("</html>");
}
/**
* We are going to perform the same operations for POST requests
* as for GET methods, so this method just sends the request to
* the doGet method.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Method: |
GET |
Request URI: |
/examples/servlets/servlet/RequestInfoExample |
Protocol: |
HTTP/1.1 |
Path Info: |
null |
Remote Address: |
76.24.21.194 |
Note: For REST,
we will use the term URI for the whole string http://server:port/request-path, so
above terminology “request URI” is unfortunate but stuck in the API. A request
URI for a GET can include a query string. In a POST, any query string
information is in the body of the request.
Note: Here
we see doGet, doPost. Follow the Java EE API link in class
web page to see other methods of HttpServlet: doDelete, doHead, doPut, etc.
Also see APIs for HttpServletRequest and
HttpServletResponse. These are the first
Java methods we’ve covered that are not in the JSE API (i.e., the normal JDK).