Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request attribute problem in Jsp

 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
function doLogin(){
var id= loginForm.UID.value;

if(id.length > 0 && id.charAt(0)!=" "){
<% request.setAttribute("action","checkUser"); %>
//<% session.setAttribute("action","checkUser"); %>

document.loginForm.submit();
}else{
alert("Enter User Name");

}
}
In the code above i am trying to set the request parameter "Action" to a value.. but when i try to access it in the servlet it is posted to i get a null value. But if i put it in the session than it works fine.
My servlet is acting as a controller and forwards the request to another bean or servlet depending upon the "action" value passed in the request.
Can anyone help me with what the problem is and what solution can be. Personally i dont want to use the session for it.
Thanks Dave for help, and everyone else also.
Gul
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah (or AAAarrrrrr), now it becomes clear.
You can't mix JavaScript and KSP code in the way. JavaScript occurs on the client, but Java code occurs on the server.
Check out this thread for someone having the same problem.
The code you are executing always gets executed before the response is sent to the client. Nothing happens with the Java code when they run the function on the client, it is already too late.
Dave
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if it is working in the session.setAttribute it means it gets set before the page is loaded and not after the button is clicked?
And will it be better to use hidden fields or is there any other way i can use to pass a value to the servlet.
Gul
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
Passing the parameter back via a hidden field is the standard.
It is often prefered to keep data in the request scope rather than using the session. A fairly broad definition I use is that the only data that goes on the session is data that is true for the life of the session. ie it should be valid regardless of the page the user goes to. Getting it roight is a matter of practice
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David. you have helped me alot today. looking forward to your words of wisdom again soon.
Gul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic