SlideShare a Scribd company logo
1 of 33
Java & JEE Training
Session 29 – Servlets - Part 5
Page 1Classification: Restricted
Agenda
• Validation of Web Applications
• Redirecting requests
• Understanding Scope: parameters, attributes
Page 2Classification: Restricted
Review of previous session…
….
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
Redirecting requests to other sources
Page 7Classification: Restricted
7
Redirecting Requests to Other Resources
• Servlet RedirectServlet
• Redirects the request to a different resource
• Why not just have links to those resources?
• Could determine browser type and choose link accordingly
• Could add parameters before redirecting (those sent originally are
included automatically)
• response.sendRedirect(URL);
• redirects to that URL
• immediately terminates current program
• Use relative paths whenever possible to make Website portable
Page 8Classification: Restricted
1
2
3
Page 9Classification: Restricted
RedirectServlet redirecting requests to other resources.
1 // RedirectServlet.java
2 // Redirecting a user to a different Web page.
3
4 import javax.servlet.*;
5 import javax.servlet.http.*;
6 import java.io.*;
7
8 public class RedirectServlet extends HttpServlet {
9
10 // process "get" request from client
11 protected void doGet( HttpServletRequest request,
12 HttpServletResponse response )
13 throws ServletException, IOException
14 {
15 String location = request.getParameter( "page" );
16
17 if ( location != null )
18
19 if ( location.equals( "deitel" ) )
20 response.sendRedirect( "http://www.deitel.com" );
21 else
22 if ( location.equals( "welcome1" ) )
23 response.sendRedirect( "welcome1" );
24
9
Obtains the page parameter from the
request.
Redirects the request to
www.deitel.com.
Redirects the request to the servlet
WelcomeServlet.
Page 10Classification: Restricted
CRUD…
• Select
• Update
• Insert
• Delete
• 1. /Hello?op=Insert OR /Hello?op=Delete
• 2. /Hello/Insert OR /Hello/Delete
Page 11Classification: Restricted
RequestDispatcher forward()
RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>");
rd.forward(request, response);
Page 12Classification: Restricted
RequestDispatcher include()
out.print(“<Your message to include goes here>");
RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP");
rd.include(request, response);
Page 13Classification: Restricted
Request Dispatcher Demo
Java & JEE Training
Understanding scope: parameters, attributes
Page 15Classification: Restricted
Requests, Sessions, Application
• Request: The client sending an HTTP request to the server.
• Session: Is a set of requests coming in from the client from the point that
the client logs in to the point that the client logs out.
• Application: The time that the application (or server) is up and running.
• An application may have multiple sessions.
• A session may have multiple requests.
Page 16Classification: Restricted
HTTP IS STATELESS
CLIENT SERVER
LOGIN ID AND
PASSWORD
LOGIN SUCCESSFUL
ENQUIRE ACCOUNT
DETAILS
I DON’T KNOW YOU.
PLEASE LOGIN…
SESSION
- COOKIES…
HOW DO I MAINTAIN
SESSIONS?
Page 17Classification: Restricted
HTTP IS STATELESS
CLIENT SERVER
LOGIN ID AND
PASSWORD
LOGIN SUCCESSFUL;
SESSIONID=1234;
ENQUIRE ACCOUNT
DETAILS;SESSIONID=1234;
I DON’T KNOW YOU.
PLEASE LOGIN…
SESSION
- COOKIES
HOW DO I MAINTAIN
SESSIONS?
Page 18Classification: 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 19Classification: Restricted
Parameters vs Attributes
• Parameters are read only, attributes are read/write objects.
• Parameters are String objects, attributes can be objects of any type.
Page 20Classification: Restricted
Methods to manipulate parameters
• Request: (Request time constant)
ServletRequest.getParameter(paramname);
ServletRequest.getParameterNames();
• ServletContext init parameters: (Deployment time constant)
ServletContext.getInitParameterNames()
ServletContext.getInitParameter(String paramName)
• ServletConfig init parameters: (Deployment time constant)
ServletConfig.getInitParameterNames()
ServletConfig.getInitParameter(String paramName)
Page 21Classification: 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 22Classification: Restricted
Example.. Identify the init parameters here..
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>ApplicationName</param-name>
<param-value>ApplicationName</param-value>
</context-param>
</web-app>
Page 23Classification: Restricted
How to get servletContext?
getServletConfig( ).getServletContext( );
request.getSession( ).getServletContext( );
getServletContext( );
request.getServletContext();
All are correct. Which one would you use?
Page 24Classification: 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 25Classification: Restricted
Scoping in Servlets and JSP
• Request scope
• Session scope
• Application or context scope
• Page scope (only for JSP)
Page 26Classification: 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 27Classification: 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 28Classification: 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 29Classification: 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 30Classification: 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 31Classification: Restricted
Topics to be covered in next session
• Understanding scope: parameters, attributes
• Servlets Continued
Page 32Classification: Restricted
Thank you!

More Related Content

What's hot

Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design PatternsSession 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design PatternsPawanMM
 
Session 36 - JSP - Part 1
Session 36 - JSP - Part 1Session 36 - JSP - Part 1
Session 36 - JSP - Part 1PawanMM
 
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 35 - Design Patterns
Session 35 - Design PatternsSession 35 - Design Patterns
Session 35 - Design PatternsPawanMM
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2PawanMM
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
 
Designing REST services with Spring MVC
Designing REST services with Spring MVCDesigning REST services with Spring MVC
Designing REST services with Spring MVCSerhii Kartashov
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2 Hitesh-Java
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3sandeep54552
 
REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)Jef Claes
 
Web Basics
Web BasicsWeb Basics
Web BasicsHui Xie
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: ServletsFahad Golra
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RSFahad Golra
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configurationAditya Bhuyan
 

What's hot (19)

Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design PatternsSession 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design Patterns
 
Session 36 - JSP - Part 1
Session 36 - JSP - Part 1Session 36 - JSP - Part 1
Session 36 - JSP - Part 1
 
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 35 - Design Patterns
Session 35 - Design PatternsSession 35 - Design Patterns
Session 35 - Design Patterns
 
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
 
Designing REST services with Spring MVC
Designing REST services with Spring MVCDesigning REST services with Spring MVC
Designing REST services with Spring MVC
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
 
REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)
 
Web Basics
Web BasicsWeb Basics
Web Basics
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
Web api
Web apiWeb api
Web api
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RS
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configuration
 
Restful webservices
Restful webservicesRestful webservices
Restful webservices
 

Similar to Session 29 - Servlets - Part 5

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 KuteTushar B Kute
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtapVikas 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 2010Arun Gupta
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptxssuser92282c
 
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
 
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, environmentjoearunraja2
 
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
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slidesSasidhar Kothuru
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/ServletSunil OS
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4Ben Abdallah Helmi
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0megrhi haikel
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicIMC Institute
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 

Similar to Session 29 - Servlets - Part 5 (20)

Servlets
ServletsServlets
Servlets
 
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
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Basics Of Servlet
Basics Of ServletBasics Of Servlet
Basics Of Servlet
 
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
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
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...
 
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
 
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)
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
 
Servlets
ServletsServlets
Servlets
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 

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 AJAXPawanMM
 
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 MVCPawanMM
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPPawanMM
 
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 ConfigurationPawanMM
 
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 BeansPawanMM
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationPawanMM
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionPawanMM
 
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 1PawanMM
 
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 JavaPawanMM
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBCPawanMM
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, SerializationPawanMM
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner ClassesPawanMM
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - MapsPawanMM
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review SessionPawanMM
 
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 InterviewsPawanMM
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsPawanMM
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsPawanMM
 

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

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

Session 29 - Servlets - Part 5

  • 1. Java & JEE Training Session 29 – Servlets - Part 5
  • 2. Page 1Classification: Restricted Agenda • Validation of Web Applications • Redirecting requests • Understanding Scope: parameters, attributes
  • 3. Page 2Classification: Restricted Review of previous session… ….
  • 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
  • 7. Redirecting requests to other sources
  • 8. Page 7Classification: Restricted 7 Redirecting Requests to Other Resources • Servlet RedirectServlet • Redirects the request to a different resource • Why not just have links to those resources? • Could determine browser type and choose link accordingly • Could add parameters before redirecting (those sent originally are included automatically) • response.sendRedirect(URL); • redirects to that URL • immediately terminates current program • Use relative paths whenever possible to make Website portable
  • 10. Page 9Classification: Restricted RedirectServlet redirecting requests to other resources. 1 // RedirectServlet.java 2 // Redirecting a user to a different Web page. 3 4 import javax.servlet.*; 5 import javax.servlet.http.*; 6 import java.io.*; 7 8 public class RedirectServlet extends HttpServlet { 9 10 // process "get" request from client 11 protected void doGet( HttpServletRequest request, 12 HttpServletResponse response ) 13 throws ServletException, IOException 14 { 15 String location = request.getParameter( "page" ); 16 17 if ( location != null ) 18 19 if ( location.equals( "deitel" ) ) 20 response.sendRedirect( "http://www.deitel.com" ); 21 else 22 if ( location.equals( "welcome1" ) ) 23 response.sendRedirect( "welcome1" ); 24 9 Obtains the page parameter from the request. Redirects the request to www.deitel.com. Redirects the request to the servlet WelcomeServlet.
  • 11. Page 10Classification: Restricted CRUD… • Select • Update • Insert • Delete • 1. /Hello?op=Insert OR /Hello?op=Delete • 2. /Hello/Insert OR /Hello/Delete
  • 12. Page 11Classification: Restricted RequestDispatcher forward() RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>"); rd.forward(request, response);
  • 13. Page 12Classification: Restricted RequestDispatcher include() out.print(“<Your message to include goes here>"); RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP"); rd.include(request, response);
  • 15. Java & JEE Training Understanding scope: parameters, attributes
  • 16. Page 15Classification: Restricted Requests, Sessions, Application • Request: The client sending an HTTP request to the server. • Session: Is a set of requests coming in from the client from the point that the client logs in to the point that the client logs out. • Application: The time that the application (or server) is up and running. • An application may have multiple sessions. • A session may have multiple requests.
  • 17. Page 16Classification: Restricted HTTP IS STATELESS CLIENT SERVER LOGIN ID AND PASSWORD LOGIN SUCCESSFUL ENQUIRE ACCOUNT DETAILS I DON’T KNOW YOU. PLEASE LOGIN… SESSION - COOKIES… HOW DO I MAINTAIN SESSIONS?
  • 18. Page 17Classification: Restricted HTTP IS STATELESS CLIENT SERVER LOGIN ID AND PASSWORD LOGIN SUCCESSFUL; SESSIONID=1234; ENQUIRE ACCOUNT DETAILS;SESSIONID=1234; I DON’T KNOW YOU. PLEASE LOGIN… SESSION - COOKIES HOW DO I MAINTAIN SESSIONS?
  • 19. Page 18Classification: 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.
  • 20. Page 19Classification: Restricted Parameters vs Attributes • Parameters are read only, attributes are read/write objects. • Parameters are String objects, attributes can be objects of any type.
  • 21. Page 20Classification: Restricted Methods to manipulate parameters • Request: (Request time constant) ServletRequest.getParameter(paramname); ServletRequest.getParameterNames(); • ServletContext init parameters: (Deployment time constant) ServletContext.getInitParameterNames() ServletContext.getInitParameter(String paramName) • ServletConfig init parameters: (Deployment time constant) ServletConfig.getInitParameterNames() ServletConfig.getInitParameter(String paramName)
  • 22. Page 21Classification: 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
  • 23. Page 22Classification: Restricted Example.. Identify the init parameters here.. <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>ApplicationName</param-name> <param-value>ApplicationName</param-value> </context-param> </web-app>
  • 24. Page 23Classification: Restricted How to get servletContext? getServletConfig( ).getServletContext( ); request.getSession( ).getServletContext( ); getServletContext( ); request.getServletContext(); All are correct. Which one would you use?
  • 25. Page 24Classification: 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.
  • 26. Page 25Classification: Restricted Scoping in Servlets and JSP • Request scope • Session scope • Application or context scope • Page scope (only for JSP)
  • 27. Page 26Classification: 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.
  • 28. Page 27Classification: 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().
  • 29. Page 28Classification: 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.
  • 30. Page 29Classification: 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
  • 31. Page 30Classification: 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
  • 32. Page 31Classification: Restricted Topics to be covered in next session • Understanding scope: parameters, attributes • Servlets Continued