Lecture 11 - Servlets Part 1 Flashcards
what is a servlet?
- Servlets are Java programs that can be run dynamically from a Web Server
- Servlets are a server-side technology
- A servlet is an intermediating layer between an HTTP request of a client and the Web server
what do servlets do?
- Read data sent by the user (e.g., form data)
- Look up other information about request in the HTTP request (e.g., headers, cookies, etc.)
- Generate the results (may do this by talking to a database, file system, etc.)
- Format the results in a document (e.g., make it into HTML)
- Set the appropriate HTTP response parameters (e.g., cookies, content-type, etc.)
- Send the document to the user
why use a servlet?
•Servlets are used to create dynamic pages
–Page is based on data submitted by the user
–Page is derived from data that changes often
–Page uses information from server-side sources
What is the basic operation of a servlet?
What are the supporting servlets?
•The Web server must support servlets (since it must run the servlets):
–Apache Tomcat
What is required for writing servlets?
•You need to import
javax.servlet.*;
- The servlet interface defines methods that manage servlets and their communication with clients
- When a connection is formed, the servlet receives two objects that implement:
- ServletRequest
- ServletResponse
What is the object hierarchy?
How to create a servlet?
•Extend the class HTTPServlet
•Implement doGet or doPost (or both)
•Both methods get:
–ServletRequest: methods for getting form (query) data, HTTP request headers, etc.
–ServletResponse: methods for setting HTTP status codes, HTTP response headers, and get a PrintWriter used for sending data to the client
•Usually implement doPost by calling doGet, or vice-versa
How to return html?
•By default a text response is generated.
•In order to generate HTML
–Tell the browser you are sending HTML, by setting the Content-Type header
–Modify the println to create a legal HTML page
•You must set all headers before returning the document content.
What is the servlet life cycle about?
- No main() method!
- The server initializes and loads the servlet
- The servlet handles client requests
- The server can remove the servlet
- The servlet can remain loaded to handle additional requests
- Startup costs occur only once (hopefully)
What is the servlet life cycle diagram?
How do you initialize servlets?
•Performed when the servlet is loaded into the Web server
•Servlet’s init(ServletConfig) method is called
•ServletConfig has methods to get initialization parameters. You put these parameters in a file.
•What would you do in the init method?
–Things that you want done once
–create connection object to communicate with the database
What is the service method?
•Every time the server receives a request for the servlet it:
–creates a thread for the request
–calls the service method
•The service method checks the type of request
–(GET, POST, PUT, DELETE, TRACE, OPTION)
•and calls the appropriate method:
–doGet, doPost, doPut, doDelete, doTrace, doOption
•It is recommended to implement doPost and doGet instead of overriding service.
What are the main http methods?
•POST: Data sent in two steps
–Browser contacts server
–Sends data
•GET: Contacts server and sends data in single step
–Called when you enter a URL in the browser directly
–Can be called by a form: appends form data to action URL, separated by question mark.
What are other http methods?
•HEAD: Client sees only header of response to determine size, etc…
–Servlets offer no method for this
•PUT: Place documents directly on server
•DELETE: Opposite of PUT
•TRACE: Debugging aid
–Returns to client contents of its request
–Servlets automatically support this
•OPTIONS: what options are available on the server
–Servlets automatically support this