0

i have a form which is to be submitted.but what i want to check is whether the user is inside session or not???if the user is inside session then the values must be submitted .if not it must show a message saying ('Please login using facebook').so how can i check whether the user is in session or not???

Script code:

<script type="text/javascript">
function submitform(){


    alert("In Submit form");
    var frm=document.getElementById("myForm");
     frm.action="Electionservlet?formidentity=editYPName";
     alert("before submit");
     frm.submit(); 
}

</script>

html code:

<table style="width: 100%; border: 0px; border-spacing: 0px; padding: 0px; text-align: center;">
<tr>

                                <td  align="center">
                                     <a style="color: #000;" href="javascript:submitform();">Submit</a>
                                </td>
                            </tr>
</table>
User2413
  • 605
  • 6
  • 22
  • 51

3 Answers3

0

Set an attribute variable in your session by:

session.setAttribute("var_name","value"); 

and check the variable in JSP page by:

 <script>
    <%if(session.getAttribute("var_name")!=null){%>
     //write your form validation code 
    alert("In Submit form");
    var frm=document.getElementById("myForm");
    frm.action="Electionservlet?formidentity=editYPName";
    alert("before submit");
    frm.submit();
    <%}
    else
    {%>
     //redirect the user to Error page or
    alert("Please login again");
    <%}%> 
    </script>
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
  • i want to show the message('please login') when the submit button is clicked but not before submiting?? – User2413 Mar 26 '14 at 09:49
0

try this

Set this in the action of your login page

session.setAttribute("username","username"); 

Next add this script in the jsp page that contains the form

<script type="text/javascript">
function submitform(){

    <%if(session.getAttribute("username")!=null){%>
    alert("In Submit form");
    var frm=document.getElementById("myForm");
     frm.action="Electionservlet?formidentity=editYPName";
     alert("before submit");
     frm.submit(); 
 <%}else{%>
    alert("Please log in");
 %<}%>
}

</script>
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52
0

You can check whether user is logged in or not using FB.getSession(), when you perform login through facebook.

function submitform(){

if(FB.getSession() != null) {

//logged user id: FB.getSession().uid
//Perform form submission
var frm=document.getElementById("myForm");
frm.submit();

} else {
//user is not logged in
  alert("Please log in");
}
Raju Rudru
  • 1,102
  • 12
  • 19
  • where should i write this code('session.setAttribute("username","username");')?? – User2413 Mar 26 '14 at 10:26
  • i Dont have login page i am doing login through facebook?? – User2413 Mar 26 '14 at 10:27
  • Since you are using facebook login, your user session is also related to facebook, right ?. What I mean to ask is when user signs out from Facebook, will he be signed out from your app? If that is the case you don't need to put any values server session. Facebook API provides some functions to check whether the user is logged in or not. check this link http://stackoverflow.com/questions/3348543/check-if-user-is-logged-in-new-facebook-api – Raju Rudru Mar 26 '14 at 10:42
  • yes my login is through facebook but i am using my own form here and when i click submit button then it must check whether session is available or not??if available then the servlet must be called .if not show an alert to login?? – User2413 Mar 26 '14 at 10:53