Vinod
Vinod

Reputation: 19

How to avoid hardcoding strings in web app

I have a jsp+servlet web application, which runs on tomcat server. All the strings which i use are hardcoded now. If possible I want to move all the hardcoded strings to one resource file and point to particular string in jsp code. How can i do it. Is there any other way other than resource file.For example: In below switch statement, i want to remove all hardcoded strings in case statement and move to one resource file or so and point to that string in my code.

switch (request.getParameter("mode")) {
                case "check1": {

                    break;
                }
                case "check2": {

                    break;
                }
                case "active_inactive": {

                    break;
                }
                default:
                    break;
            }

Upvotes: 1

Views: 319

Answers (1)

Sandunka Mihiran
Sandunka Mihiran

Reputation: 565

Use class named Constants for this pupose.

 public class Constants{

   public static String CHECK_1 = "Check1";
   public static String CHECK_2 = "Check2";


 }

And use this in anywhere you want.

      switch (request.getParameter("mode")) {
            case Constants.CHECK_1: {

                break;
            }
            case Constants.CHECK_2: {

                break;
            }
            default:
                break;
        }

Upvotes: 2

Related Questions