What are scopes in Java Servlet API?

Posted on 2011-10-04
Medium Priority
Last Modified: 2012-05-12

I was asked this question and I immediately thought of: request, session, application but I think this may be a trick question.  Does a servlet really have scopes?
Question by:cgray1223
1 Comment
LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36914670

Servlet per se is not an objectand not a varaible, it is  a program that does not have scope but it deals with objects which charcterize different scopes.

At least that is what everyone writes about servlet scope objects:

Servlet Scope Objects

There are four scope objects in servlets which enables sharing information between web components. The scope objects and their corresponding Java classes are listed below:

    • Web Context – javax.servlet.ServletContext
    • Session – javax.servlet.http.HttpSession
    • Request – javax.servlet.HttpServletRequest
    • Page – javax.servlet.jsp.PageContext

You can get attribute values from servlet scope objects using getAttribute method and set new values of attributes using setAttribute method. For example, you can get the client’s IP address by calling getRemoteAddr method of HttpServletRequest class.

Let’s develop an example on how to use servlet scope objects. In this example, we will print the information about the server which hosts our servlet.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question