Checking if URL has changed for use within a session check





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















The code below works to increase the counter for every page a visitor goes too however i'm needing to work out how to make it so it doesn't allow an increase in counts on the same page over one (to stop someone spamming refresh and increasing the view count). Is there a way to check if the URL has changed within a java servlet?



CODE



HttpSession session = request.getSession() ;    // Will create new 
session.setAttribute("timesLoggedIn", 0);
session.setAttribute("pictureNames", null);
// session.setAttribute("pictureNames", null);
Integer accessCount = (Integer)session.getAttribute("timesLoggedIn") ;
String pictureNames = request.getPathInfo();
String pathName = (String)session.getAttribute("pictureNames");
String id = session.getId();

//start session
//increment count by one
//if count already implemented, don't implement.

out.println(pathName);
if (accessCount < 1 && pathName == null) {

out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", 1);
session.setAttribute("pictureNames", pictureNames);
Utilities.IncreaseCount(out, pictureName);


}

out.println(pathName);









share|improve this question

























  • you are checking pathName for only null but not if it is changed?

    – secret super star
    Nov 17 '18 at 6:13


















0















The code below works to increase the counter for every page a visitor goes too however i'm needing to work out how to make it so it doesn't allow an increase in counts on the same page over one (to stop someone spamming refresh and increasing the view count). Is there a way to check if the URL has changed within a java servlet?



CODE



HttpSession session = request.getSession() ;    // Will create new 
session.setAttribute("timesLoggedIn", 0);
session.setAttribute("pictureNames", null);
// session.setAttribute("pictureNames", null);
Integer accessCount = (Integer)session.getAttribute("timesLoggedIn") ;
String pictureNames = request.getPathInfo();
String pathName = (String)session.getAttribute("pictureNames");
String id = session.getId();

//start session
//increment count by one
//if count already implemented, don't implement.

out.println(pathName);
if (accessCount < 1 && pathName == null) {

out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", 1);
session.setAttribute("pictureNames", pictureNames);
Utilities.IncreaseCount(out, pictureName);


}

out.println(pathName);









share|improve this question

























  • you are checking pathName for only null but not if it is changed?

    – secret super star
    Nov 17 '18 at 6:13














0












0








0








The code below works to increase the counter for every page a visitor goes too however i'm needing to work out how to make it so it doesn't allow an increase in counts on the same page over one (to stop someone spamming refresh and increasing the view count). Is there a way to check if the URL has changed within a java servlet?



CODE



HttpSession session = request.getSession() ;    // Will create new 
session.setAttribute("timesLoggedIn", 0);
session.setAttribute("pictureNames", null);
// session.setAttribute("pictureNames", null);
Integer accessCount = (Integer)session.getAttribute("timesLoggedIn") ;
String pictureNames = request.getPathInfo();
String pathName = (String)session.getAttribute("pictureNames");
String id = session.getId();

//start session
//increment count by one
//if count already implemented, don't implement.

out.println(pathName);
if (accessCount < 1 && pathName == null) {

out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", 1);
session.setAttribute("pictureNames", pictureNames);
Utilities.IncreaseCount(out, pictureName);


}

out.println(pathName);









share|improve this question
















The code below works to increase the counter for every page a visitor goes too however i'm needing to work out how to make it so it doesn't allow an increase in counts on the same page over one (to stop someone spamming refresh and increasing the view count). Is there a way to check if the URL has changed within a java servlet?



CODE



HttpSession session = request.getSession() ;    // Will create new 
session.setAttribute("timesLoggedIn", 0);
session.setAttribute("pictureNames", null);
// session.setAttribute("pictureNames", null);
Integer accessCount = (Integer)session.getAttribute("timesLoggedIn") ;
String pictureNames = request.getPathInfo();
String pathName = (String)session.getAttribute("pictureNames");
String id = session.getId();

//start session
//increment count by one
//if count already implemented, don't implement.

out.println(pathName);
if (accessCount < 1 && pathName == null) {

out.println("<h1>This is your first visit!</h1>");
session.setAttribute("timesLoggedIn", 1);
session.setAttribute("pictureNames", pictureNames);
Utilities.IncreaseCount(out, pictureName);


}

out.println(pathName);






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 14:19

























asked Nov 16 '18 at 13:10







user10463530




















  • you are checking pathName for only null but not if it is changed?

    – secret super star
    Nov 17 '18 at 6:13



















  • you are checking pathName for only null but not if it is changed?

    – secret super star
    Nov 17 '18 at 6:13

















you are checking pathName for only null but not if it is changed?

– secret super star
Nov 17 '18 at 6:13





you are checking pathName for only null but not if it is changed?

– secret super star
Nov 17 '18 at 6:13












1 Answer
1






active

oldest

votes


















0














You can get the current page path with the servlet request API, such as request.getPathInfo()



You could then use this to create a session attribute key unique to the page. If there would be characters in the page path that are not permitted to be part of a session attribute key then you could obviously replace the characters in the string. It would be up to you to determine whether you would want to also consider query parameters as unique pages, e.g. /page1?a=1 vs /page1?a=2



Then it would be trivial in your if/else to say if the key does not exist, put the value there with 1. If the key is already in the session, do not increment it, just read the value to display it.



Be mindful that if you do consider query params unique pages and this would have different keys for /page1?a=1 vs /page1?a=2 it could be potentially dangerous for memory consumption as a nasty use who figures this out could in theory write a script to request many different pages using unique query params which would slowly but surely fill the session with lots of small bit of data but could eventually over a sustained period of time be some sort of denial of service via polluting your session storage.






share|improve this answer
























  • I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

    – user10463530
    Nov 16 '18 at 13:57













  • I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

    – David
    Nov 16 '18 at 14:08











  • I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

    – David
    Nov 16 '18 at 14:09











  • I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

    – user10463530
    Nov 16 '18 at 14:19












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338597%2fchecking-if-url-has-changed-for-use-within-a-session-check%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









0














You can get the current page path with the servlet request API, such as request.getPathInfo()



You could then use this to create a session attribute key unique to the page. If there would be characters in the page path that are not permitted to be part of a session attribute key then you could obviously replace the characters in the string. It would be up to you to determine whether you would want to also consider query parameters as unique pages, e.g. /page1?a=1 vs /page1?a=2



Then it would be trivial in your if/else to say if the key does not exist, put the value there with 1. If the key is already in the session, do not increment it, just read the value to display it.



Be mindful that if you do consider query params unique pages and this would have different keys for /page1?a=1 vs /page1?a=2 it could be potentially dangerous for memory consumption as a nasty use who figures this out could in theory write a script to request many different pages using unique query params which would slowly but surely fill the session with lots of small bit of data but could eventually over a sustained period of time be some sort of denial of service via polluting your session storage.






share|improve this answer
























  • I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

    – user10463530
    Nov 16 '18 at 13:57













  • I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

    – David
    Nov 16 '18 at 14:08











  • I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

    – David
    Nov 16 '18 at 14:09











  • I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

    – user10463530
    Nov 16 '18 at 14:19
















0














You can get the current page path with the servlet request API, such as request.getPathInfo()



You could then use this to create a session attribute key unique to the page. If there would be characters in the page path that are not permitted to be part of a session attribute key then you could obviously replace the characters in the string. It would be up to you to determine whether you would want to also consider query parameters as unique pages, e.g. /page1?a=1 vs /page1?a=2



Then it would be trivial in your if/else to say if the key does not exist, put the value there with 1. If the key is already in the session, do not increment it, just read the value to display it.



Be mindful that if you do consider query params unique pages and this would have different keys for /page1?a=1 vs /page1?a=2 it could be potentially dangerous for memory consumption as a nasty use who figures this out could in theory write a script to request many different pages using unique query params which would slowly but surely fill the session with lots of small bit of data but could eventually over a sustained period of time be some sort of denial of service via polluting your session storage.






share|improve this answer
























  • I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

    – user10463530
    Nov 16 '18 at 13:57













  • I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

    – David
    Nov 16 '18 at 14:08











  • I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

    – David
    Nov 16 '18 at 14:09











  • I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

    – user10463530
    Nov 16 '18 at 14:19














0












0








0







You can get the current page path with the servlet request API, such as request.getPathInfo()



You could then use this to create a session attribute key unique to the page. If there would be characters in the page path that are not permitted to be part of a session attribute key then you could obviously replace the characters in the string. It would be up to you to determine whether you would want to also consider query parameters as unique pages, e.g. /page1?a=1 vs /page1?a=2



Then it would be trivial in your if/else to say if the key does not exist, put the value there with 1. If the key is already in the session, do not increment it, just read the value to display it.



Be mindful that if you do consider query params unique pages and this would have different keys for /page1?a=1 vs /page1?a=2 it could be potentially dangerous for memory consumption as a nasty use who figures this out could in theory write a script to request many different pages using unique query params which would slowly but surely fill the session with lots of small bit of data but could eventually over a sustained period of time be some sort of denial of service via polluting your session storage.






share|improve this answer













You can get the current page path with the servlet request API, such as request.getPathInfo()



You could then use this to create a session attribute key unique to the page. If there would be characters in the page path that are not permitted to be part of a session attribute key then you could obviously replace the characters in the string. It would be up to you to determine whether you would want to also consider query parameters as unique pages, e.g. /page1?a=1 vs /page1?a=2



Then it would be trivial in your if/else to say if the key does not exist, put the value there with 1. If the key is already in the session, do not increment it, just read the value to display it.



Be mindful that if you do consider query params unique pages and this would have different keys for /page1?a=1 vs /page1?a=2 it could be potentially dangerous for memory consumption as a nasty use who figures this out could in theory write a script to request many different pages using unique query params which would slowly but surely fill the session with lots of small bit of data but could eventually over a sustained period of time be some sort of denial of service via polluting your session storage.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 13:43









DavidDavid

1,43741651




1,43741651













  • I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

    – user10463530
    Nov 16 '18 at 13:57













  • I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

    – David
    Nov 16 '18 at 14:08











  • I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

    – David
    Nov 16 '18 at 14:09











  • I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

    – user10463530
    Nov 16 '18 at 14:19



















  • I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

    – user10463530
    Nov 16 '18 at 13:57













  • I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

    – David
    Nov 16 '18 at 14:08











  • I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

    – David
    Nov 16 '18 at 14:09











  • I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

    – user10463530
    Nov 16 '18 at 14:19

















I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

– user10463530
Nov 16 '18 at 13:57







I've tried implementing your suggestion. However i'm unable to increment different pictures. My logic for checking if the path has changed must be wrong. Updated my OP with my current thinking

– user10463530
Nov 16 '18 at 13:57















I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

– David
Nov 16 '18 at 14:08





I'm not sure I follow correctly what the issue you're having now is. In the else block, I think you no longer want to add 1 to i because you actually have the value 1 in there. Alternatively, you need not store an int at all any more, you could perhaps put the value null in there and just test for the presence or absence of the key - if it's no there put the key, if it is just display "logged in once". What are you trying to do here, e.g. if I hit the URL /pictures/1 and /pictures/2 you want to track the fact I've seen that at least once?

– David
Nov 16 '18 at 14:08













I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

– David
Nov 16 '18 at 14:09





I'm a little confused about why there are counters or the message "You have visited this page " + i + " times! " if you want i to always be at most 1? Either I load the page that I've never seen and it says i've been here 0 times or I have been here before and it says 1 even if i've been here 100 times?

– David
Nov 16 '18 at 14:09













I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

– user10463530
Nov 16 '18 at 14:19





I was just having the messages in for testing to try and help me understand. I'll update my OP with my now current thinking, it increments all the time. Yea to answer your question i want to track that you've hit both pages once, but i don't want you to be able to refresh your page and keep the view count on increasing. I want one view per page, per session

– user10463530
Nov 16 '18 at 14:19




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338597%2fchecking-if-url-has-changed-for-use-within-a-session-check%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."