Chapter 4. Request and Response: Being a Servlet Flashcards

1
Q

What are the 3 big lifecycle moments of a servlet?

A
  1. init()
  2. service()
  3. doGet()/doPost()… etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Servlet lifecycle, when is init() called? What is it used for? Would you override it?

A

It’s called AFTER servlet instance is created, but BEFORE it can service any client requests.

It gives us a chance to initialize a servlet before handling client requests.

Would you override it? Possibly, example being you want to establish a database connection and have it ready for when a request comes in.

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

Servlet lifecycle, when is service() called? What’s it good for? Would you override it?

A

It’s called when the first client request comes in. The container starts a new thread or allocates from a pool and calls the service() method.

It’s used for determining wether doGet() or doPost() or any other method is called.

Would you override it? NO, Very very very unlikely. It works the way it is.

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

Sevlet lifecycle, when is doGet()/doPost() etc called? What is it used for? Should you override it?

A

It will be called by the service() method when a request comes in.

It’s used for doing stuff to react to an incoming request.

Should you override it? ALWAYS if you want to support a request…

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

How can you get a single parameter from a request?

A

getParameter(“name”)

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

How can you get an array of parameters from a request?

A

getParameterValues(“name)

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

When you have access to the HttpServletResponse how do you write back to the client?

A

Typically you do 2 things:

setContentType(“”) – Set it to “text/html” whatever.

getWriter()/getOutputStream() – Get’s the stream where you can write the content to send back.

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

With an HttpServletResponse what’s the key differences/use cases between getWriter() & getOutputStream()?

A

getWriter() – returns a writer that we use println(“Text”). It’s primary used to send text response.

getOutputStream() – returns a writer that we use write(aByteArray) It’s used for everything else! Basically non-text, we return a byte array by using the output stream.

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

What does a response.sendRedirect() method do?

A

Tells client to request a new URL. Important thing is the user sees a new URL.

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

What does the requestDispatcher.forward(request, response) method do?

A

Calls another request dispatcher to handle a request, user doesn’t see a URL change but something else will handle the request… maybe even code from another url.

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

What’s a Request Dispatch? What’s the code look like?

A

Forwarding a request to a new request, transparent to the client.

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