chapter 5 being a web app. very few servlet or jsp stands alone many times in our application,...
TRANSCRIPT
![Page 1: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/1.jpg)
Chapter 5 Being a Web App
![Page 2: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/2.jpg)
Very few servlet or JSP stands alone
Many times in our application, different servlets or JSPs need to share information For example, you entered your name and credit
card info in one page and this info is displayed (shared) in another page, e.g. , order confirmation page
Your application may also want to hide information
Your application also needs to be thread-safe
![Page 3: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/3.jpg)
Make your servlet configurable
Think about an example web application where you want to put your email address in a servletInstead of hard code your email in the servlet, it
is better to make it configurable
![Page 4: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/4.jpg)
Init parameters to the rescue
![Page 5: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/5.jpg)
You cannot use servlet init parameters until the servlet is initialized
Your servlet inherits getServletConfig() automajtically, so you can call that from any method in your servlet to get a reference to a ServletConfigOnce you have a ServletConfig reference, you
can call getInitParameter()Remember you cannot call it from your constructor
![Page 6: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/6.jpg)
![Page 7: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/7.jpg)
You cannot use servlet init parameters until the servlet is initialized
When the container initializes a servlet, it makes a unique ServletConfig for the servlet
The container “reads” the servlet init parameters from the DD and gives them to the ServletConfig, the passes the ServletConfig to the servlet’s init() method
![Page 8: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/8.jpg)
The servlet init parameters are read only ONCE
When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig
Once the parameters are in the ServletConfig, they won’t be read again until/unless you redeploy the servlet
![Page 9: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/9.jpg)
How can a JSP get servlet init parameters
A ServletConfig is for servlet configuration (it doesn’t say JSPConfig)
How to pass the init parameters for a servlet to a JSP?The request object lets you set attributes (think
of them as a name/value pair where the value can be any object) that any other servlet or JSP that gets the request can useThat means any servlet or JSP to which the request
is forwarded using a RequestDispatcher
![Page 10: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/10.jpg)
• Setting a request attribute works… but only for the JSP to which you forward the request
![Page 11: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/11.jpg)
How about I want some configurable info to be shared by all servlets and JSPs in my web application?
You can use context init parametersIt work just like servlet init parameters, except
context parameters are available to the entire webapp, not just a single servlet
![Page 12: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/12.jpg)
Context init parameters
![Page 13: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/13.jpg)
Context init parameters
![Page 14: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/14.jpg)
ServletConfig & ServletContext
ServletConfig is one per servletServletContext is one per web appThe container makes a ServletContext
when a web app is deployed, and makes the context available to each Servlet and JSP (which becomes a servlet) in the web app
![Page 15: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/15.jpg)
ServletConfig & ServletContext
![Page 16: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/16.jpg)
Question
Why would I ever use <init-param> anyway? Wouldn’t I always want to use <context-param> so that other parts of my application could reuse the values and I won’t have to duplicate XML code for every servlet declaration?
Answer: It all depends on which part of your app is supposed to see the value
![Page 17: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/17.jpg)
What else can you do with your ServletContext?
![Page 18: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/18.jpg)
Listener
What if you want an app init parameter that is a database DataSource?You can certainly put the DataSource lookup
name in a context init parameter, and that is probably the most common use of context parameter today
But then who does the work of turning the String parameter into an actual DataSource reference that all parts of the web app can share?
![Page 19: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/19.jpg)
Listener (Cont.)
You cannot really put that code in a servlet, because you cannot guarantee one servlet in particular will always run first
You need to listen for a context initialization event, so that you can get the context init parameters and run some code before the rest of the app can service a client
ServletContextListener can help It is not servlet or JSP
![Page 20: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/20.jpg)
What can listener do?
![Page 21: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/21.jpg)
Example listener
![Page 22: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/22.jpg)
Deploy the listener
You need to create the listener class and put the class in WEB-INF/classes
You also need to put a <listener> element in the web.xml Deployment Descriptor
![Page 23: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/23.jpg)
An listener example
public void contextInitialized (ServletContextEvent event){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
System.out.println("*****Error loading the driver*******");
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/j2ee?user=root&password=");
ServletContext sc = event.getServletContext();
sc.setAttribute("dbconnection1", conn);
System.out.println("*******set dbconnection attribute *******");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
![Page 24: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/24.jpg)
An listener example
public void contextDestroyed (ServletContextEvent event){
try{
conn.close();
conn = null;
}catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
![Page 25: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/25.jpg)
web.xml for the listener
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>
edu.nku.j2ee.MysqlContextListener
</listener-class>
</listener>
</web-app>
![Page 26: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/26.jpg)
What is an attribute?
You can think of it as simply a name/value pair (where the name is a String and the value is an Object) in a map instance variable
In reality, we really care about the scope in which the attribute exists.In other words, who can see it and how long
does it live.
![Page 27: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/27.jpg)
Attributes
Three scopes: Context Request Session
![Page 28: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/28.jpg)
Attributes are not parameters
![Page 29: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/29.jpg)
Context attributes
![Page 30: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/30.jpg)
Session attributes
![Page 31: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/31.jpg)
Request attributes
![Page 32: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/32.jpg)
Attribute API
The three attribute scopes – context, request, and session – are handled by the ServletContext, ServletRequest, HttpSession interfaces
The API methods for attributes are exactly the same in every interface
![Page 33: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/33.jpg)
![Page 34: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/34.jpg)
![Page 35: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/35.jpg)
![Page 36: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/36.jpg)
The dark side of attributes...
Kim decides to test out attributes. He sets attribute and then immediately gets the value of the attribute
![Page 37: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/37.jpg)
![Page 38: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/38.jpg)
Here is what he sees the first time he runs it
![Page 39: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/39.jpg)
But then something goes horribly wrong
Kim put this code in a test servlet that is part of a larger test web app. In other words, the servlet that holds this doGet() method was deployed as part of a larger app.Can you figure out what happened? How to fix it?
![Page 40: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/40.jpg)
Context scope is not thread-safe
Remember everyone in the app has access to context attributesSince requests are concurrently handled in
separate threads, You may have multiple servlets (mean you might
have multiple threads) accessing the context attributes concurrently
You may have multiple threads from one servlet accessing the context attributes concurrently
Either of the above two cases can cause problems
![Page 41: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/41.jpg)
![Page 42: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/42.jpg)
![Page 43: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/43.jpg)
![Page 44: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/44.jpg)
context attributes’ thread-safe issue
Can we synchronize doGet() method to make it thread safe?
![Page 45: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/45.jpg)
context attributes’ thread-safe issue
No. Synchronize doGet() will not only kiss your
concurrency goodbye, but also does not protect context attributes at allSynchronizing the service method would stop other
threads from the same servlet from accessing the context attributes, but it won’t do anything to stop a completely different servlet
![Page 46: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/46.jpg)
![Page 47: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/47.jpg)
context attributes’ thread-safe issue
By the way, the figure in Page 192 and 196 of our book has a little bit of problem…It draws two instance of servlet A, it is a little
confusingBy default, one instances of a servlet is responsible
for taking care of multiple requests coming in to the same servlet
If you look at figure at P101 of the book, you will be very clear
![Page 48: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/48.jpg)
context attributes’ thread-safe issue
You don’t need a lock on the servlet… you need the lock on the context
If everyone accessing the context has to first get the lock on the context object, then you’re guaranteed that only one thread at a time can be getting or setting the context attributeIt only works if all of the other code that manipulates
the same context attributes ALSO synchronize on the ServletContext
If code doesn’t ask for the lock, then that code is still free to hit the context attribute
![Page 49: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/49.jpg)
The right way to synchronize context attribute’s multiple concurrent access
![Page 50: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/50.jpg)
Are session attributes thread-safe?
We have not talked about HTTP sessions in detail yet, but you already know that a session is an object used to maintain conversational state with a client
The session persists across multiple requests from the same client
And if it’s one client, and a single client can be in only one request at a time, doesn’t that automatically mean that session are thread-safe?
![Page 51: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/51.jpg)
Are session attributes thread-safe?
No.The client could open a new browser window
(from the same browser)
The container can see the request from the second window (of the same browser) as coming from the same session
![Page 52: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/52.jpg)
Correct way to synchronize accessing to session attributes
You can protect session attributes by synchronizing on the HttpSession object
![Page 53: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/53.jpg)
Question
Isn’t it a bad idea to synchronize code, because it causes a lot of overhead and hurts concurrency?
Answer: You should always think carefully before synchronizing any code, because you’re right-it does add some expense in checking, acquiring, and releasing locks. If you need protection, then use synchronization but remember the standard rule of all forms of locking-keep the lock for the shortest amount of time to accomplish your goal!
![Page 54: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/54.jpg)
Request attributes and local variables
Only request attributes and local variables are thread-safe
Everything else is subject to manipulation by multiple threads, unless you do something to stop it
![Page 55: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/55.jpg)
Request attributes and request dispatching
Request attributes make sense when you want some other component of the app to take over all or part of the requestThe component that take over the request will be able
to access the request attributes
Our typical simple example is an MVC app that starts with a servlet controller, but ends with a JSP view
We can make another part of the component take over the request with a RequstDispatcher
![Page 56: Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information](https://reader034.vdocuments.site/reader034/viewer/2022051416/56649e885503460f94b8d1ec/html5/thumbnails/56.jpg)
Request attributes and request dispatching