Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:48:58+00:00 2026-06-16T05:48:58+00:00

I want to delivered the URL of a servlet (MyServlet.java) to a java class

  • 0

I want to delivered the URL of a servlet (MyServlet.java) to a java class (Test.java). Test.java is located in a Project.jar in folder lib of WEB-INF. So I have tried to pass the Servlet URL to Test.java



    import projct.Test;

    public class MyServlet extends HttpServlet {

        @Override
        public void init() throws ServletException {..}                

        @Override
        public void destroy() {..}

        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

        @Override
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

        protected void processRequest(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {

             new Test().setServleturl(request.getRequestURL().toString());
             request.getRequestDispatcher("/index.html").forward(request, response);

        }
    }

The Test.java class looks like this:



    public class Test {
        private static String var;

        public static String getVar() {
            return var;
        }

        public static void setVar(String var) {
            Test.var = var;
        }    
    }

After Diployment is the value of var still null.

What am I doing wrong?
And how can you write the URL from MyServlet.java in var from Test.java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:48:59+00:00Added an answer on June 16, 2026 at 5:48 am

    I assume that setServleturl() is in fact setVar(), because it wouldn’t compile.

    Of course it’s null after deployment. processRequest() is only called when a request… is processed. Make an HTTP request to your servlet, and the code will be executed.

    That said, what you have there seems like a really bad idea, and it’s not thread-safe (setVar() and getVar() should be synchronized). Moreover, these methods should be called using Test.setVar(), and not new Test().setVar(), since they’re static methods. What are you trying to achieve?

    EDIT: it seems the problem is to read files from WEB-INF at deployment time. To do that, use a ServletContextListener, and in its contextInitialized() method, get the servlet context from the event, and call servletContext.getResourceAsStream("/WEB-INF/theFileToLoad").

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Composite C1 is delivered as a Website Project for Visual Studio 2010. I want
I have made a java project and want to deliver it to a client
I want to deliver an rpm but the number of files to be delivered
Today my new HP iPAQ was delivered at my home, but now I want
I want to test some features that needs delayed jobs to work, on cucumber.
I want to create a service that allows diverse web site owners to integrate
I am making a RoR site that is delivered in several languages, and want
I have a class as follows: Class Scan Delivered As Boolean ScanDate As Date
I inherited a Ruby on Rails project. It was delivered all zipped up so
I collect user time zone and a time they want something delivered in that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.