I presume that by back button you are referring to a browser and you have implemented that with JSPs.

In that case when the username and password are correct, save the username in the session before you redirect to the next page. Then when you go to the next page (and every page) always take the username from the session and check if it is not null.
When you logout remove the username from the session, so when you hit the back button and try to get the username from the session it will be null. Then you can redirect the user to the login page

Thankyou very much for ur early reply, I will try the precodure told by u, and ask u if i am unable to do so...

I presume that by back button you are referring to a browser and you have implemented that with JSPs.

In that case when the username and password are correct, save the username in the session before you redirect to the next page. Then when you go to the next page (and every page) always take the username from the session and check if it is not null.
When you logout remove the username from the session, so when you hit the back button and try to get the username from the session it will be null. Then you can redirect the user to the login page

I am doing a project where there are many html forms to be filled by the user. Now i should retrieve the data from the form and insert it into the database. This all can i do with the help of JSP or Servlet? Which is the best?? How can i validate each and every field of the form e.g. is every field filled, do the phone no. filed have numbers in it , etc. How can i do it? Can u provide me a source code in java for this please??

You can have the <form> action be empty:
<form action="" ....>
If you do that you will be redirected in the same page. Then at the beginning of the page before you start displaying any GUI check if the form was submitted and if it was do your validation and if it is correct redirect to the page you want.
Or else continue displaying the rest of the page

<%
String phone=request.getParameter("phone");
String user=request.getParameter("user");
if ((phone!=null)&&(user!=null)) {
if (phone.trim().length()==0) {
   //error message
} else if (user.trim().length()==0) {
  //error message
} else {
   //do something with user and phone ....
   //maybe use javascript to do the redirect
%>
<!-- I am not sure about this, you might want to check it out on your own at the site that I provide -->
<script>
window.open(....)
</script>
<%
}
}
%>  

<body>

</body>

OR

you can send the <form> action to a servlet, do the validation there and then redirect to the page you want. Code at the servlet to decide where you want to go:

RequestDispatcher dispatcher =
    request.getRequestDispatcher(address);
  dispatcher.forward(request, response);

OR

much better solution than the other two, use javascript, without submitting the form
instead of submit use a button, and do:

<input type="button" value="Submit" onclick="validate()">

Then in the <head> tag write the validate() method using javascript and if all are correct submit the form

<form name="form1" action="someURL">

use: document.form1.submit()

For all the comments about javascript use:
http://w3schools.com/
to learn javascript

This article has been dead for over six months. Start a new discussion instead.