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;
java jsp servlets
|
show 2 more comments
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;
java jsp servlets
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
example:case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39
|
show 2 more comments
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;
java jsp servlets
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;
java jsp servlets
java jsp servlets
edited Nov 12 '18 at 10:36
Vinod
asked Nov 12 '18 at 10:30
VinodVinod
12
12
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
example:case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39
|
show 2 more comments
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
example:case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
example:
case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39
example:
case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39
|
show 2 more comments
1 Answer
1
active
oldest
votes
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;
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
add a comment |
Your Answer
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: "1"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260255%2fhow-to-avoid-hardcoding-strings-in-web-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
add a comment |
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;
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
add a comment |
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;
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;
answered Nov 12 '18 at 10:43
Sandunka MihiranSandunka Mihiran
241116
241116
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
add a comment |
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
Thats one way, but is there any way that i can copy all strings to an xml or any other resource file and point to it
– Vinod
Nov 12 '18 at 10:48
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
I'm sure that's possible. Though I do not know the exact reason for your requirement , I would not recommend such approach to store constants as it would cost you performance and management issues
– Sandunka Mihiran
Nov 12 '18 at 10:51
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Vinod You can only use constants or literals in a switch, so if you want to get those strings from a resource file, you'll need to switch to another solution (eg using a map).
– Mark Rotteveel
Nov 12 '18 at 16:54
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Mark Rotteveel That was just an example. I have strings which i want to move to resource file such as xml. This is my primary concern.
– Vinod
Nov 13 '18 at 5:44
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
@Vinod Then you shouldn't be asking about about your actual problem and not the switch construct, because there strings can't be externalized.
– Mark Rotteveel
Nov 13 '18 at 10:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260255%2fhow-to-avoid-hardcoding-strings-in-web-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
show an example of what you're doing currently. Post some code
– Jonathan Laliberte
Nov 12 '18 at 10:33
Possible duplicate of how to use property file in jsp
– Nirekin
Nov 12 '18 at 10:35
@Jonathan Laliberte I have edited code with example
– Vinod
Nov 12 '18 at 10:37
you could create a class where you can store those strings, then just refer to the class whenever you want to use a string from it...
– Jonathan Laliberte
Nov 12 '18 at 10:38
example:
case Globals.CHECK_2: {
– Jonathan Laliberte
Nov 12 '18 at 10:39