Chapter 5. Attributes and Listeners: Being a Web App Flashcards

1
Q

How can you put configuration values in your Deploy Descriptor, and fetch it from within the code?

A

Within your servlet, you can add an init-param, with a param-name & param-value.

Now in servlet we can call getServletConfig().getInitParameter(“param-name”);

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

When in the servlet lifecycle can we start getting the config values getServletConfig() ?

A

NOT in constructor

YES in init() method.

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

How can you access servlet init parameters from a JSP page?

A

We can attach it to the request within the servlet.

request.setAttribute(“key”, “value”);

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

What’s the difference between Context Params & Servlet Params?

A

Context Params are for the whole application

Servlet Params are just for a given servlet.

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

How do you define a Context Param?

A

Within the Deployment Descriptor, add context-param with the same param-name & param-value tags.

Important to note that it’s located within the whole web-app tag, NOT a single servlet tag.

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

In servlet code, how would you get Context Params?

A

getServletContext().getInitParameter(“ParamName”)

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

What’s the difference between a ServletContext and ServletConfig?

A

ServletContext – One for the whole application.

ServletConfig – Each servlet has a unique one. (Including JSP, they’re turned into first-class servlets)

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

What is a listener class useful for? Think of an example we could use…

A

Listening for events, and reacting to them.

Example: We want to connect to a database before any servlet is initialize, we can listen for contextInitialized event!

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

How do you make a listener class?

A

extend ServletContextListener. Implement any methods you want to listen for such as contextInitialized, or contextDestroyed.

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

How do you tell the container about a listener class you have?

A

In the deployment descripter you add a listener & listener-class tag.

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

How do we lock the servlet context? What is this useful for?

A

synchronized(getServletContext())

It’s useful if we are getting/setting things on the ServletContext to prevent race conditions.

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

Are HttpSession thread safe?

A

No, users can open a new browser window…

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

Attached image, will this code work? What exception will be thrown if any?

A

IllegalStateException

You can’t forward the request after you’ve already committed a response.

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