Subsections
- Understand servlet framework.
- Write basic servlets.
- Used to deliver dynamic content to web pages.
- Request
- Response
- GET and POST
- The servlet API
- Request, service(), doGet()/doPost(), response.
- A sample HelloWorldServlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>Hello, world</body></html>");
out.close();
}
}
- Handled by the servlet container.
- Create and initialize the servlet.
- Handle zero or more service calls.
- Destroy and garbage collect the servlet.
- A single servlet instance to handle every request.
- Override doGet() to handle GET requests.
- Override doPost() to handle POST requests.
- Both methods take HTTPServletRequest and HTTPServletResponse as arguments.
- A few other methods, but used much less often - doDelete, doTrace, doOptions, doPut
- getMethod()
- getQueryString()
- getRemoteHost()
- getRemoteAddr()
- getAuthType()
- getContentType()
- getWriter()
- setContentType()
- getOutputStream()
|
|