SlideShare a Scribd company logo
Java & JEE Training
Session 30 – Servlets - Part 6
Page 1Classification: Restricted
Agenda
• Understanding scope: parameters, attributes
• Servlets Continued
Page 2Classification: Restricted
Review of previous sessions…
….
Page 3Classification: Restricted
Web Server and Application Server
Presentation Tier:
JSP, Servlets
Run on Web Server
or container
JDBC on web server
or app server
Database
Client:
HTML JS CSS
(Run on client
machine on the
browser)
Page 4Classification: Restricted
Demo of complete Web app flow…
• Form which takes two fields… Student ID and Student Name…
• Output should show whether that row exists or not, and if it exists, how
many records in the table match the criteria (id, name)
• If match found, then print login successful, else login failed.
Page 5Classification: Restricted
Validation in web applications
• Validation logic should be implemented BOTH
• Frontend – JavaScript
• Backend - Java
Page 6Classification: Restricted
1
2
3
Page 7Classification: Restricted
RequestDispatcher forward()
RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>");
rd.forward(request, response);
Page 8Classification: Restricted
RequestDispatcher include()
out.print(“<Your message to include goes here>");
RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP");
rd.include(request, response);
Page 9Classification: Restricted
Request Dispatcher Demo
Page 10Classification: Restricted
What we will cover next…
• Parameters and Attributes
• Deployment Descriptor (web.xml)
Java & JEE Training
Understanding scope: parameters, attributes
Page 12Classification: Restricted
Web.xml file: Servlet mapping to URL
DECLARATIVE SECURITY…
Page 13Classification: Restricted
Parameters vs Attributes
• Parameters may come into our application from the client request, or may be
configured through deployment descriptor (web.xml) elements or their
corresponding annotations. When you submit a form, form values are sent as
request parameters to a web application. In case of a GET request, these
parameters are exposed in the URL as name value pairs and in case of
POST, parameters are sent within the body of the request.
• Servlet init parameters and context init parameters are set through the deployment
descriptor (web.xml) or their corresponding annotations. All parameters are read-
only from the application code. We have methods in the Servlet API to retrieve
various parameters.
• Attributes are objects that are attached to various scopes and can be
modified, retrieved or removed. Attributes can be read, created, updated and
deleted by the web container as well as our application code. We have methods in
the Servlet API to add, modify, retrieve and remove attributes. When an object is
added to an attribute in any scope, it is called binding as the object is bound into a
scoped attribute with a given name.
Page 14Classification: Restricted
Parameters vs Attributes
• Parameters are read only Strings, attributes are read/write objects.
• Parameters are String objects, attributes can be objects of any type.
Page 15Classification: Restricted
Methods to manipulate parameters
• Request: (Request time constant)
ServletRequest.getParameter(paramname);
ServletRequest.getParameterNames();
• ServletConfig init parameters: (Deployment time constant)
ServletConfig.getInitParameterNames()
ServletConfig.getInitParameter(String paramName)
• ServletContext init parameters: (Deployment time constant)
ServletContext.getInitParameterNames()
ServletContext.getInitParameter(String paramName)
Request
Scope
Servlet Scope
Application
Scope
Page 16Classification: Restricted
Web.xml : init parameters (Servlet Scope)
Page 17Classification: Restricted
Web.xml: Init parameters (Application scope)
Page 18Classification: Restricted
ServletConfig vs ServletContext
• ServletConfig // Available for a specific servlet
• ServletConfig available in javax.servlet.*; package
• ServletConfig object is one per servlet class
• Object of ServletConfig will be created during initialization process of the servlet
• This Config object is public to a particular servlet only
• Scope: As long as a servlet is executing, ServletConfig object will be available, it will be
destroyed once the servlet execution is completed.
• We should give request explicitly, in order to create ServletConfig object for the first time
• In web.xml – <init-param> tag will be appear under <servlet-class> tag
• ServletContext // Available for whole application
• ServletContext available in javax.servlet.*; package
• ServletContext object is global to entire web application
• Object of ServletContext will be created at the time of web application deployment
• Scope: As long as web application is executing, ServletContext object will be available, and
it will be destroyed once the application is removed from the server.
• ServletContext object will be available even before giving the first request
• In web.xml – <context-param> tag will be appear under <web-app> tag
Page 19Classification: Restricted
How to get servletContext?
getServletConfig( ).getServletContext( );
request.getSession( ).getServletContext( );
getServletContext( );
request.getServletContext();
Page 20Classification: Restricted
Understanding scope of attributes
• Similar to scope and lifetime of variables in Java as you have seen in blocks
and methods in java, parameters and attributes in a Java EE web
application also have scope and lifetime in the context of the web
application.
• The scope of a parameter/attribute denotes the availability of that
parameter/attribute for use. A web application serves multiple requests
from clients when it is up and running. These requests can be from same
client or different clients. We have seen from the servlet life cycle that a
servlet’s service() method is called every time a request comes.
Page 21Classification: Restricted
Scoping in Servlets and JSP
• Request scope
• Session scope
• Application or context scope
• Page scope (only for JSP)
• CASE 1:
• HTTP Request is sent to Servlet1, Servlet1 forwards the same request to Servlet2 (request-
dispatcher).  Request scope is enough.
• CASE 2:
• HTTP Request is sent to Servlet1 for login, Servlet1 responds to client with login successful.
Client sends another request for querying the database. This is a new request, but belongs to
the same session.  Session scope
• CASE 3:
• You want to set an attribute which should be available across multiple sessions and/or
multiple users  Context scope.
Page 22Classification: Restricted
Request Scope
• Request scope start from the moment an HTTP request hits a servlet in our web
container and end when the servlet is done with delivering the HTTP response.
• With respect to the servlet life cycle, the request scope begins on entry to a
servlet’s service() method and ends on the exit from that method.
• A ‘request’ scope parameter/attribute can be accessed from any of servlets or jsps
that are part of serving one request. For example, you call one servlet/jsp, it then
calls another servlet/jsp and so on, and finally the response is sent back to the
client.
• Request scope is denoted by javax.servlet.http.HttpServletRequest interface.
• Container passes the request object as an argument of type HttpServletRequest to
Servlet's service method.
• Request object is available in a JSP page as an implicit object called request. You
can set value for an attribute in request object from a servlet and get it from a JSP
within the same request using the implicit request object.
Page 23Classification: Restricted
Session scope
• A session scope starts when a client (e.g. browser window) establishes
connection with our web application till the point where the browser
window is closed.
• Session scope spans across multiple requests from the same client.
• A notable feature of tabbed browsing is that session is shared between the
tabs and hence you can requests from other tabs too during a session
without logging in again. For instance, you can load your Gmail inbox in
another tab without logging in again. This also means browsing an unknown
site and a secure site from different tabs from the same browser can
expose your secure session ID to malicious applications. So always open a
new browser window when you want to do secure transactions, especially
financial transactions.
• Session scope is denoted by javax.servlet.http.HttpSession interface.
• Session object is available in a JSP page as an implicit object called session.
• In a servlet, you can get Session object by calling request.getSession().
Page 24Classification: Restricted
Application or context scope
• Context scope or application scope starts from the point where a web
application is put into service (started) till it is removed from service
(shutdown) or the web application is reloaded.
Parameters/attributes within the application scope will be available to all
requests and sessions.
• Application scope is denoted by javax.servlet.ServletContext interface.
• Application object is available in a JSP page as an implicit
object called application.
• In a servlet, you can get application object by
calling getServletContext() from within the servlets code directly (the
servlet itself implements the ServletConfig interface that contains this
method) or by explicitly calling getServletConfig().getServletContext().
• The web container provides one ServletContext object per web application
per JVM.
Page 25Classification: Restricted
Page scope (Only for JSP, not for Servlets)
• The page scope restricts the scpoe and lifetime of attributes to the same
page where it was created.
• Page scope is denoted by javax.servlet.jsp.PageContext abstract class.
• It is available in a JSP page as an implicit object called pageScope
Page 26Classification: Restricted
Servlets – Scope at a glance
• Application scope
request.getServletContext();
request.getServletContext().setAttribute("attribute_name","value")
• Session scope
request.getSession(); //going to create the session if session is not exist.
request.getSession(false); // Not going to create the session.
session.getAttribute("attribute_name");
• Request scope
request.setAttribute("attribute_name","value");
request.getAttribute("attribute_name"); // return the Object you have to
cast it
Page 27Classification: Restricted
Topics to be covered in next session
• Session Management and Tracking
• Best Practices
• Design Patterns
• Cookies
Page 28Classification: Restricted
Thank you!

More Related Content

What's hot

Session 35 - Design Patterns
Session 35 - Design PatternsSession 35 - Design Patterns
Session 35 - Design Patterns
PawanMM
 
Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)
PawanMM
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4
PawanMM
 
Session 26 - Servlets Part 2
Session 26 - Servlets Part 2Session 26 - Servlets Part 2
Session 26 - Servlets Part 2
PawanMM
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
PawanMM
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
JainamParikh3
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & Ajax
Ang Chen
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552
 
Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2
sandeep54552
 
Web Basics
Web BasicsWeb Basics
Web Basics
Hui Xie
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
IMC Institute
 
ATG Architecture
ATG ArchitectureATG Architecture
ATG Architecture
phanishankar
 
Jsp
JspJsp
Building RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPIBuilding RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPI
Gert Drapers
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configuration
Aditya Bhuyan
 

What's hot (18)

Session 35 - Design Patterns
Session 35 - Design PatternsSession 35 - Design Patterns
Session 35 - Design Patterns
 
Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4
 
Session 26 - Servlets Part 2
Session 26 - Servlets Part 2Session 26 - Servlets Part 2
Session 26 - Servlets Part 2
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & Ajax
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
 
Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2Enterprise java unit-1_chapter-2
Enterprise java unit-1_chapter-2
 
Web Basics
Web BasicsWeb Basics
Web Basics
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
ATG Architecture
ATG ArchitectureATG Architecture
ATG Architecture
 
Jsp
JspJsp
Jsp
 
Building RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPIBuilding RESTfull Data Services with WebAPI
Building RESTfull Data Services with WebAPI
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configuration
 

Similar to Session 30 - Servlets - Part 6

Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11
Smita B Kumar
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Tushar B Kute
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
ssuser92282c
 
Servlets
ServletsServlets
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)
Amit Ranjan
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
megrhi haikel
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packagesvamsi krishna
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
joearunraja2
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
Ben Abdallah Helmi
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
MathivananP4
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
Vikas Jagtap
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Arun Gupta
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
bharathiv53
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
ssbd6985
 
Basics Of Servlet
Basics Of ServletBasics Of Servlet
Basics Of Servlet
Shubhani Jain
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
deepak kumar
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and ServletsRaghu nath
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
sivakumarmcs
 

Similar to Session 30 - Servlets - Part 6 (20)

Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
Servlets
ServletsServlets
Servlets
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Basics Of Servlet
Basics Of ServletBasics Of Servlet
Basics Of Servlet
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
 

More from PawanMM

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAX
PawanMM
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVC
PawanMM
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PawanMM
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 Introduction
PawanMM
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
PawanMM
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
PawanMM
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
PawanMM
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
PawanMM
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review Session
PawanMM
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
PawanMM
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
PawanMM
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
PawanMM
 

More from PawanMM (17)

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAX
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVC
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate Integration
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 Introduction
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review Session
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
 

Recently uploaded

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 

Recently uploaded (20)

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 

Session 30 - Servlets - Part 6

  • 1. Java & JEE Training Session 30 – Servlets - Part 6
  • 2. Page 1Classification: Restricted Agenda • Understanding scope: parameters, attributes • Servlets Continued
  • 3. Page 2Classification: Restricted Review of previous sessions… ….
  • 4. Page 3Classification: Restricted Web Server and Application Server Presentation Tier: JSP, Servlets Run on Web Server or container JDBC on web server or app server Database Client: HTML JS CSS (Run on client machine on the browser)
  • 5. Page 4Classification: Restricted Demo of complete Web app flow… • Form which takes two fields… Student ID and Student Name… • Output should show whether that row exists or not, and if it exists, how many records in the table match the criteria (id, name) • If match found, then print login successful, else login failed.
  • 6. Page 5Classification: Restricted Validation in web applications • Validation logic should be implemented BOTH • Frontend – JavaScript • Backend - Java
  • 8. Page 7Classification: Restricted RequestDispatcher forward() RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>"); rd.forward(request, response);
  • 9. Page 8Classification: Restricted RequestDispatcher include() out.print(“<Your message to include goes here>"); RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP"); rd.include(request, response);
  • 11. Page 10Classification: Restricted What we will cover next… • Parameters and Attributes • Deployment Descriptor (web.xml)
  • 12. Java & JEE Training Understanding scope: parameters, attributes
  • 13. Page 12Classification: Restricted Web.xml file: Servlet mapping to URL DECLARATIVE SECURITY…
  • 14. Page 13Classification: Restricted Parameters vs Attributes • Parameters may come into our application from the client request, or may be configured through deployment descriptor (web.xml) elements or their corresponding annotations. When you submit a form, form values are sent as request parameters to a web application. In case of a GET request, these parameters are exposed in the URL as name value pairs and in case of POST, parameters are sent within the body of the request. • Servlet init parameters and context init parameters are set through the deployment descriptor (web.xml) or their corresponding annotations. All parameters are read- only from the application code. We have methods in the Servlet API to retrieve various parameters. • Attributes are objects that are attached to various scopes and can be modified, retrieved or removed. Attributes can be read, created, updated and deleted by the web container as well as our application code. We have methods in the Servlet API to add, modify, retrieve and remove attributes. When an object is added to an attribute in any scope, it is called binding as the object is bound into a scoped attribute with a given name.
  • 15. Page 14Classification: Restricted Parameters vs Attributes • Parameters are read only Strings, attributes are read/write objects. • Parameters are String objects, attributes can be objects of any type.
  • 16. Page 15Classification: Restricted Methods to manipulate parameters • Request: (Request time constant) ServletRequest.getParameter(paramname); ServletRequest.getParameterNames(); • ServletConfig init parameters: (Deployment time constant) ServletConfig.getInitParameterNames() ServletConfig.getInitParameter(String paramName) • ServletContext init parameters: (Deployment time constant) ServletContext.getInitParameterNames() ServletContext.getInitParameter(String paramName) Request Scope Servlet Scope Application Scope
  • 17. Page 16Classification: Restricted Web.xml : init parameters (Servlet Scope)
  • 18. Page 17Classification: Restricted Web.xml: Init parameters (Application scope)
  • 19. Page 18Classification: Restricted ServletConfig vs ServletContext • ServletConfig // Available for a specific servlet • ServletConfig available in javax.servlet.*; package • ServletConfig object is one per servlet class • Object of ServletConfig will be created during initialization process of the servlet • This Config object is public to a particular servlet only • Scope: As long as a servlet is executing, ServletConfig object will be available, it will be destroyed once the servlet execution is completed. • We should give request explicitly, in order to create ServletConfig object for the first time • In web.xml – <init-param> tag will be appear under <servlet-class> tag • ServletContext // Available for whole application • ServletContext available in javax.servlet.*; package • ServletContext object is global to entire web application • Object of ServletContext will be created at the time of web application deployment • Scope: As long as web application is executing, ServletContext object will be available, and it will be destroyed once the application is removed from the server. • ServletContext object will be available even before giving the first request • In web.xml – <context-param> tag will be appear under <web-app> tag
  • 20. Page 19Classification: Restricted How to get servletContext? getServletConfig( ).getServletContext( ); request.getSession( ).getServletContext( ); getServletContext( ); request.getServletContext();
  • 21. Page 20Classification: Restricted Understanding scope of attributes • Similar to scope and lifetime of variables in Java as you have seen in blocks and methods in java, parameters and attributes in a Java EE web application also have scope and lifetime in the context of the web application. • The scope of a parameter/attribute denotes the availability of that parameter/attribute for use. A web application serves multiple requests from clients when it is up and running. These requests can be from same client or different clients. We have seen from the servlet life cycle that a servlet’s service() method is called every time a request comes.
  • 22. Page 21Classification: Restricted Scoping in Servlets and JSP • Request scope • Session scope • Application or context scope • Page scope (only for JSP) • CASE 1: • HTTP Request is sent to Servlet1, Servlet1 forwards the same request to Servlet2 (request- dispatcher).  Request scope is enough. • CASE 2: • HTTP Request is sent to Servlet1 for login, Servlet1 responds to client with login successful. Client sends another request for querying the database. This is a new request, but belongs to the same session.  Session scope • CASE 3: • You want to set an attribute which should be available across multiple sessions and/or multiple users  Context scope.
  • 23. Page 22Classification: Restricted Request Scope • Request scope start from the moment an HTTP request hits a servlet in our web container and end when the servlet is done with delivering the HTTP response. • With respect to the servlet life cycle, the request scope begins on entry to a servlet’s service() method and ends on the exit from that method. • A ‘request’ scope parameter/attribute can be accessed from any of servlets or jsps that are part of serving one request. For example, you call one servlet/jsp, it then calls another servlet/jsp and so on, and finally the response is sent back to the client. • Request scope is denoted by javax.servlet.http.HttpServletRequest interface. • Container passes the request object as an argument of type HttpServletRequest to Servlet's service method. • Request object is available in a JSP page as an implicit object called request. You can set value for an attribute in request object from a servlet and get it from a JSP within the same request using the implicit request object.
  • 24. Page 23Classification: Restricted Session scope • A session scope starts when a client (e.g. browser window) establishes connection with our web application till the point where the browser window is closed. • Session scope spans across multiple requests from the same client. • A notable feature of tabbed browsing is that session is shared between the tabs and hence you can requests from other tabs too during a session without logging in again. For instance, you can load your Gmail inbox in another tab without logging in again. This also means browsing an unknown site and a secure site from different tabs from the same browser can expose your secure session ID to malicious applications. So always open a new browser window when you want to do secure transactions, especially financial transactions. • Session scope is denoted by javax.servlet.http.HttpSession interface. • Session object is available in a JSP page as an implicit object called session. • In a servlet, you can get Session object by calling request.getSession().
  • 25. Page 24Classification: Restricted Application or context scope • Context scope or application scope starts from the point where a web application is put into service (started) till it is removed from service (shutdown) or the web application is reloaded. Parameters/attributes within the application scope will be available to all requests and sessions. • Application scope is denoted by javax.servlet.ServletContext interface. • Application object is available in a JSP page as an implicit object called application. • In a servlet, you can get application object by calling getServletContext() from within the servlets code directly (the servlet itself implements the ServletConfig interface that contains this method) or by explicitly calling getServletConfig().getServletContext(). • The web container provides one ServletContext object per web application per JVM.
  • 26. Page 25Classification: Restricted Page scope (Only for JSP, not for Servlets) • The page scope restricts the scpoe and lifetime of attributes to the same page where it was created. • Page scope is denoted by javax.servlet.jsp.PageContext abstract class. • It is available in a JSP page as an implicit object called pageScope
  • 27. Page 26Classification: Restricted Servlets – Scope at a glance • Application scope request.getServletContext(); request.getServletContext().setAttribute("attribute_name","value") • Session scope request.getSession(); //going to create the session if session is not exist. request.getSession(false); // Not going to create the session. session.getAttribute("attribute_name"); • Request scope request.setAttribute("attribute_name","value"); request.getAttribute("attribute_name"); // return the Object you have to cast it
  • 28. Page 27Classification: Restricted Topics to be covered in next session • Session Management and Tracking • Best Practices • Design Patterns • Cookies