Maintaining session timeout globally in java spring configuration

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












Below is my code to maintain a session timeout globally in Java spring configuration and to load the session timeout value from a properties file. Do you have any suggestions to improve my code?



public class MySessionListener implements HttpSessionListener 
@Override
public void sessionCreated(HttpSessionEvent event)
try
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("bimspring.properties");
Properties properties = new Properties();
properties.load(stream);
String sessionTimeout = properties.getProperty("cookieName", "No Value Found");
event.getSession().setMaxInactiveInterval(Integer.parseInt(sessionTimeout));
catch (IOException e)
e.printStackTrace();



@Override
public void sessionDestroyed(HttpSessionEvent event)










share|improve this question



























    up vote
    2
    down vote

    favorite












    Below is my code to maintain a session timeout globally in Java spring configuration and to load the session timeout value from a properties file. Do you have any suggestions to improve my code?



    public class MySessionListener implements HttpSessionListener 
    @Override
    public void sessionCreated(HttpSessionEvent event)
    try
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream stream = classLoader.getResourceAsStream("bimspring.properties");
    Properties properties = new Properties();
    properties.load(stream);
    String sessionTimeout = properties.getProperty("cookieName", "No Value Found");
    event.getSession().setMaxInactiveInterval(Integer.parseInt(sessionTimeout));
    catch (IOException e)
    e.printStackTrace();



    @Override
    public void sessionDestroyed(HttpSessionEvent event)










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Below is my code to maintain a session timeout globally in Java spring configuration and to load the session timeout value from a properties file. Do you have any suggestions to improve my code?



      public class MySessionListener implements HttpSessionListener 
      @Override
      public void sessionCreated(HttpSessionEvent event)
      try
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      InputStream stream = classLoader.getResourceAsStream("bimspring.properties");
      Properties properties = new Properties();
      properties.load(stream);
      String sessionTimeout = properties.getProperty("cookieName", "No Value Found");
      event.getSession().setMaxInactiveInterval(Integer.parseInt(sessionTimeout));
      catch (IOException e)
      e.printStackTrace();



      @Override
      public void sessionDestroyed(HttpSessionEvent event)










      share|improve this question













      Below is my code to maintain a session timeout globally in Java spring configuration and to load the session timeout value from a properties file. Do you have any suggestions to improve my code?



      public class MySessionListener implements HttpSessionListener 
      @Override
      public void sessionCreated(HttpSessionEvent event)
      try
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      InputStream stream = classLoader.getResourceAsStream("bimspring.properties");
      Properties properties = new Properties();
      properties.load(stream);
      String sessionTimeout = properties.getProperty("cookieName", "No Value Found");
      event.getSession().setMaxInactiveInterval(Integer.parseInt(sessionTimeout));
      catch (IOException e)
      e.printStackTrace();



      @Override
      public void sessionDestroyed(HttpSessionEvent event)












      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 3 at 8:43









      Daniel

      4,1132836




      4,1132836









      asked Jun 3 at 8:09









      user171106

      111




      111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Quite a number of points for this short piece of code:



          • As the properties file will probably not change at runtime, loading it every single time a session gets created is a total waste. See whether you can cache the session timeout in a static variable.

          • If the value is not set, the default "no value found" will lead to a number format exception. Better use a sensible parseable default.

          • Similar context: by setting the value to a non-parseable string, you can kill the server with a misconfiguration.

          • "sessionTimeout" should not be named "cookieName"

          • Separation of concerns: a method should do one thing. This is a mixup of reading the program configuration and using the determined value to set the timeout.

          • Error handling: printStackTrace does not accomplish anything. Do something real (e.g. log an error for the server admin, shutdown, whatever.) If you decide to continue the process in case of an I/O exception, set a sensible default on the session.





          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195733%2fmaintaining-session-timeout-globally-in-java-spring-configuration%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Quite a number of points for this short piece of code:



            • As the properties file will probably not change at runtime, loading it every single time a session gets created is a total waste. See whether you can cache the session timeout in a static variable.

            • If the value is not set, the default "no value found" will lead to a number format exception. Better use a sensible parseable default.

            • Similar context: by setting the value to a non-parseable string, you can kill the server with a misconfiguration.

            • "sessionTimeout" should not be named "cookieName"

            • Separation of concerns: a method should do one thing. This is a mixup of reading the program configuration and using the determined value to set the timeout.

            • Error handling: printStackTrace does not accomplish anything. Do something real (e.g. log an error for the server admin, shutdown, whatever.) If you decide to continue the process in case of an I/O exception, set a sensible default on the session.





            share|improve this answer

























              up vote
              1
              down vote













              Quite a number of points for this short piece of code:



              • As the properties file will probably not change at runtime, loading it every single time a session gets created is a total waste. See whether you can cache the session timeout in a static variable.

              • If the value is not set, the default "no value found" will lead to a number format exception. Better use a sensible parseable default.

              • Similar context: by setting the value to a non-parseable string, you can kill the server with a misconfiguration.

              • "sessionTimeout" should not be named "cookieName"

              • Separation of concerns: a method should do one thing. This is a mixup of reading the program configuration and using the determined value to set the timeout.

              • Error handling: printStackTrace does not accomplish anything. Do something real (e.g. log an error for the server admin, shutdown, whatever.) If you decide to continue the process in case of an I/O exception, set a sensible default on the session.





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Quite a number of points for this short piece of code:



                • As the properties file will probably not change at runtime, loading it every single time a session gets created is a total waste. See whether you can cache the session timeout in a static variable.

                • If the value is not set, the default "no value found" will lead to a number format exception. Better use a sensible parseable default.

                • Similar context: by setting the value to a non-parseable string, you can kill the server with a misconfiguration.

                • "sessionTimeout" should not be named "cookieName"

                • Separation of concerns: a method should do one thing. This is a mixup of reading the program configuration and using the determined value to set the timeout.

                • Error handling: printStackTrace does not accomplish anything. Do something real (e.g. log an error for the server admin, shutdown, whatever.) If you decide to continue the process in case of an I/O exception, set a sensible default on the session.





                share|improve this answer













                Quite a number of points for this short piece of code:



                • As the properties file will probably not change at runtime, loading it every single time a session gets created is a total waste. See whether you can cache the session timeout in a static variable.

                • If the value is not set, the default "no value found" will lead to a number format exception. Better use a sensible parseable default.

                • Similar context: by setting the value to a non-parseable string, you can kill the server with a misconfiguration.

                • "sessionTimeout" should not be named "cookieName"

                • Separation of concerns: a method should do one thing. This is a mixup of reading the program configuration and using the determined value to set the timeout.

                • Error handling: printStackTrace does not accomplish anything. Do something real (e.g. log an error for the server admin, shutdown, whatever.) If you decide to continue the process in case of an I/O exception, set a sensible default on the session.






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 4 at 7:57









                mtj

                2,675212




                2,675212






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195733%2fmaintaining-session-timeout-globally-in-java-spring-configuration%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?