0
<html>
<script type="text/javascript">
    function logged()
    {
        <% session.setAttribute("logtype", "hire"); %>
        var x=<%=session.getAttribute("loggedin")%>;
        if(x===true)
        {
            window.location="home.jsp";
            return false;
        }
        else if(x===false)
        {
            return true;
        }
    }
</script>
<body>
   <form method="post" action="login.jsp" onsubmit="return logged()">
      <input type="submit" value="HIRE"/>
   </form>
   <form method="post" action="displayTasks.jsp" onsubmit="<% session.setAttribute("logtype", "work"); %>">
      <input type="submit" value="WORK"/>
   </form>   
</body>
</html>

My session variable "logtype" is creating a problem. It stays as "WORK" no matter whatever I click. What am I doing wrong?

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • 1
    You're trying to run Java on a JS event. Java == server. JS == client. You set the session attribute on the server. You *think* you're setting it after a JS event, long after the Java has executed and been sent to the client. – Dave Newton Jul 15 '13 at 20:22
  • where should i set my session value then? –  Jul 15 '13 at 20:41
  • Not "where", "when". You need to take a step back and understand how Java web apps work first. "When" depends on what you're actually trying to do; you could use Ajax, a normal form submit, etc depending on your requirements. – Dave Newton Jul 15 '13 at 20:48
  • not familiar with ajax man. just started working with jsp's –  Jul 15 '13 at 20:51
  • All the more reason to take a step back. Scriptlets are Java, compiled into the JSP's servlet, and executed on the server side. *After* the servlet has been rendered it's sent to the client. JavaScript is executed on the *client* (e.g., the browser). For the purposes of this discussion, the browser (a) knows *nothing* about Java, and (b) can only make HTTP requests back to the server if something needs to be modified on the Java side. – Dave Newton Jul 15 '13 at 21:01

1 Answers1

0

The problem is that you're setting the session attribute twice in your page, so when the JSP generated the view, it will execute all your scriptlets. Basically, your JSP is doing this:

session.setAttribute("logtype", "hire");
//later...
session.setAttribute("logtype", "work");

Note that all the scriptlet code is executed when rendering the view (in simple words, when translating the JSP to HTML).

As stated by David Newton in comments the problem is Not "where", "when". Since it looks you want to change the session attribute in the form POST, this should be handled in your action POST request. And again, since you're doing all in JSP, this should be in your displayTasks.jsp page:

session.setAttribute("logtype", "work");

And remove this line from your current page (whatever name it has).


Not directly related to the current problem, but you have serious problems developing Java web applications. First thing to know, Java runs at server (as explained before, when rendering the view) while JavaScript runs at client browser, so it's not a good idea to make both interact at the same level directly. After realizing this, you must avoid scriptlets usage as is heavily explained here: How to avoid Java code in JSP files? and then move to EL and JSTL.

Now, you will realize that there are something else wrong with your current development approach. You're handling the GET and POST requests directly using JSPs. If you're in learning phase, I strongly discourage to do this and use Servlets instead for server side data processing. Know with all these tools, your development willbe accordingly to the MVC pattern, I provide you a basic login implementes in JSP + Servlet example: Creating a user object from login parameters.

Then after earning more experience in web development, you would want to try ajax. Here's a good tutorial on JSP, Servlets and Ajax: How to use Servlets and Ajax?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • how should i know when to immplement my code in jsp and servlets? –  Jul 16 '13 at 06:51
  • could u provide me a sample project so i can learn to create efficient projects. –  Jul 16 '13 at 09:14
  • @Saurabh the links in my answer are not for decoration purposes. Access to each one and thr example about creating a user object from login parameters to see a basic real world example of development using jsp and servlets. – Luiggi Mendoza Jul 16 '13 at 13:05