Lecture 11 - Servlets Part 1 Flashcards

1
Q

what is a servlet?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what do servlets do?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

why use a servlet?

A

•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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the basic operation of a servlet?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the supporting servlets?

A

•The Web server must support servlets (since it must run the servlets):
–Apache Tomcat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is required for writing servlets?

A

•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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the object hierarchy?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to create a servlet?

A

•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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to return html?

A

•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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the servlet life cycle about?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the servlet life cycle diagram?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you initialize servlets?

A

•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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the service method?

A

•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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the main http methods?

A

•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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are other http methods?

A

•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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens when destroying servlets?

A

•The server may remove a loaded servlet
–Asked to do so by administrator
–Servlet was idle a long time
•Before removing, calls the destroy() method
–can perform cleanup, e.g., close database connections
•Is it possible for the servlet to end without destroy being called?
–Yes, but we need to program this
–Non-trivial