What are scopes in Java Servlet API?

Posted on 2011-10-04
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


    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 Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Connect to IP Camera using Java 4 62
    array6 challenfge 6 48
    countPairs challenge 7 39
    Running Jira on Raspberry PI 2? 3 46
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now