reload jsp page after logout on back button click - javascript

In every page of mine, there is a logout option. When the user clicks this logout option, they get log out session.invalidate(); .When I press the back button, even if the session is invalidated, I get the previous page with all the contents in the input tag.How can I resolve this problem.I tried window.location.reload(),and many other options, but it's not working in here.\
My website is this. and credentials are. test123#gmail.com and pass is Amal123

you can copy and paste this code in every page of your site which clears the cached page.
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
if(session.getAttribute("some_token")==null)
response.sendRedirect("login/login.html");
%>
In logout you must be invalidating session so when you click back it would check the some_token attribute value in session and if not there it will redirect you to login page . But remember after login you are setting some_token attribute in session.

Before all pages you want to open only when session is active you have to put a filter that checks for session before your pages get open.
And on loguot you have to invalidate the current session.
And then your will be able to press back button but can not perform any funtionality

You can use a session attribute... for example when you log in, set a variable value to username e.g:
HTML
<form name="login" action="/login.jsp" method="post" >
<input type="text" name="username" >
<input type="password" name="password" >
<input type="submit" >
JSP:
String userName=request.getParameter("username");
request.getSession().setAttribute("username",userName);
Now On every page,add this code
<% if(request.getSession().getAttribute("username")==null)
response.sendRedirect("index.jsp");%>
Also, at the time of logout do not forget to remove the session attribute.
request.getSession().removeAttribute("username");
Try it and let me know.

Related

Php Go back to previous page form resubmit

I have the following three php files:
reports.php, bwdates-reports-details.php and visitor-detail.php
reports.php contains my form which inputs the from and to date (erased some parts for brevity):
<form method="post" action="bwdates-reports-details.php">
...
<input type="date" id="fromdate" name="fromdate">
...
<input type="date" id="todate" name="todate">
<button type="submit" name="submit">Submit</button></p>
</form>
bwdates-reports-details.php outputs whatever parameters inputted in "fromdate" and "todate" of reports.php, see image below
As you can see in the image, there is an option to view the details of a transaction by clicking the "View Details" icon - i class="fa fa-edit fa-1x".
In view details (visitor-detail.php), There is a "Go Back" button.
<div align="center">
<button onclick="goBack()">Go Back</button>
</div>
There is also a goBack javascript function:
<script>
function goBack() {
window.history.back();
}
</script>
But when the go back button is clicked, I am getting Confirm Form Resubmission error. What I wanted to achieve with the "go back" button is to be able to go back to bwdates-reports-details.php with all the variables I passed through in reports.php. How do I achieve what I wanted to do? Please take note that this isn't a purchase related website. php or javascript will do. Thanks!
If a form is submitted (probably with the POST method), you send this data to the new page that is then opened. This page is also added to the history not only with the URL but also the sent data.
If you then leave this page and want to navigate back to this previous page you will get an "Resubmission" error, because the browser would need to send the data again to do so, but that is probaby a bad thing to do, because the data of the form would be sent twice. (Imagine that was a purchase form and the client would buy everything again.)
To make your goBack function work there are no other ways than get rid of that form submission page somehow or put a page in between that is not a form-data-receiver.
The best way would be to not go back in the browser history if you don't know from what pages the user navigated to this. Instead just navigate to an overview or dashboard.
Edit:
If that page your working on is only accessible by that response page of the form, maybe going two pages back will fix your error: history.back(2);
Because your form is using POST method and when you click on goBack button, it goes on previous page without variables. You may use put dates variable with goBack button and also use
if(isset($_GET['your variable name'];
{`fetch your variables`}
on your previous page.
Desired result was achieved by using the GET method instead of POST method (see https://www.tutorialspoint.com/php/php_get_post.htm). reports.php form code was replaced from:
<form method="post" action="bwdates-reports-details.php">
to:
<form method="get" action="bwdates-reports-details.php">
In bwdates-reports-details.php where I have the following variables:
$fdate=$_POST['fromdate'];
$tdate=$_POST['todate'];
Instead I replaced it with:
$fdate=$_GET['fromdate'];
$tdate=$_GET['todate'];
The goback javascript function I have in visitor-detail.php stays the same. And works like a charm. Thanks and cheers!

page reload with success message php

i am trying above but message is displayed on php page.instead of reloading on index page AND DISPLAY MESSAGE ABOVE SUBSCRIBE FORM ITS REDIRECTING TO PHP PAGE.You can check on test site link attached.Form is on index
page.I tried to reload page through jquery onload and onclick onsubmit but didn't worked.below are test which i did.
//form is on index.html page
<!-- your form here -->
<form action="forms/subscribe.php"id="form" method="post">
<input type="email" name="email" placeholder="Enter your Email">
<input type="submit" value="Subscribe">
</form>
//form is on index.html page
My php page
<?php
// My Code goes here form to email
header.location(Location: /index.html)
?>
You cannot use js variable values after reload. You need to do things without reloading the page means you just need to update the content and control your HTML tags.
The above example you mentioned. They are not reloading the page, they are getting values from input box then hide that div. After hiding the div they are showing another div with the information.
For your solutions you can do the same but remember to reset input values for every request.
You can do it in PHP with page reloads if you store a message in the session. At the top of each page make sure that the first line is
session_start()
Then on the page that receives the data set a session message
$_SESSION['message'] = 'Some Value';
Then on your page with the form you check to see if the session has a message. If it does, display it and then clear it.
if(isset($_SESSION['message']) {
echo $_SESSION['message'];
session_unset('message');
}
some silly syntax error
using onsubmit="alert()"
<script>
function alert() {
alert("Thank you for contacting us. We will be in touch with you very soon.!");
}
</script>
on subscribe pageheader('Location: /index?message=success');

Hide Confirm Form Resubmission PHP

After I submit a form, and I click the back button, the page displays the confirm form resubmission page. Instead of that, I actually just want the browser to display the form itself. It there a way to do that? Here is a part of my code below, and it works except that it will not stop refreshing. I just want it to refresh once when you click the back button.
<meta http-equiv="refresh" content="0">
<form method = 'post' action = 'submitPost.php?section=$section'>
<textarea rows='10' cols='100' name = 'post'></textarea> </br>
<input type = 'submit' value = 'Submit'>
</form>
This is a browser security policy - when you want to go back to a page you loaded using the POST http verb it will always ask you for confirmation.

Resubmitting the same form is not working

I am making an HTML page "Login Pro.html". I am making such a page where just clicking on a link I can login to an account (say Facebook).
HTML Code:
Login to my Account
The Form input fields for Username and Password (The value attribute will be set using JQuery):
<input type="text" id="username" value="" />
<input type="password" id="password" value="" />
<input type="submit" id="loginButton" />
In the JQuery code, I am setting the values and submitting the form like this:
$("#loginLink").click(function()//when the link is clicked this will be executed.
{
$("#username").attr("value","My Username");//Setting value for the username
$("#password").attr("value","My Password");//Setting value for the password
$("#loginButton").click();//Submitting the form by implicitly clicking the submit button
});
In this case I am able to login into my account successfully and the "Login Pro.html"(From the Page I am doing all these) is opening in a new page (because I have set target="_new" and the form is being submitted and taking me to the Home Page of the Account I am logging in into, is opening successfully in the first page (tab). But when I am trying to login again through the "Login Pro.html" page, opened in the new tab, I am being landed to the same page i.e. "Login Pro.html" page instead of getting logged in (In short in the second instance I can't login). After closing this page, when I am opening the page again, the same saga continues i.e. I am able to login in the first instance, but not able to do so while trying in the new tab.
Can somebody help me in getting rid of this. Is this because, the page is already loaded?
The "name" attribute is missing in the inputs. Without name you are not sending anything.

How to create a JSP variable that does not reset on page reload

I have two buttons, one called "Previous" the other is called "Next". When they are clicked, they reload the JSP page in which they are defined. They are also supposed to either increment or decrement a variable.
<form name="input" action="MyPage.jsp" method="post">
<html>
<input type="submit" value="Previous" onclick="<% hitsCount--; %>">
</html>
<html>
<input type="submit" value="Next" onclick="<% hitsCount++; %>">
</html>
The problem I am having is that I cannot figure out how to define the variable in such a way that it is not reset each time the page loads.
For example if "hitsCount" is 0, and I click the "Next" button the page should reload and the value of "hitsCount" should be incremented by one. The next time I hit the "Next" button, the page reloads and "hitsCount" should contain a value of 2.
you can use session storage for keep the values in the client side
Here is a link where you can see how session storage works.
You could always use html5 local storage:
var hitsCount = localStorage.getItem("hitsCount");
localStorage.setItem("hotsCount", hitsCount++);
If the user is using an older browser that doesn't support local storage, try setting a cookie instead. If you don't like either of these options then you'll have to pass the hitsCount variable to the next screen using a url paramater.

Categories

Resources