my dog learned polymorphism*
The moose likes JSP and the fly likes request attribute problem in Jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "request attribute problem in Jsp" Watch "request attribute problem in Jsp" New topic
Author

request attribute problem in Jsp

Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Sep 03, 2003
Posts: 173
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Sep 03, 2003
Posts: 173
Thanks David. you have helped me alot today. looking forward to your words of wisdom again soon.
Gul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request attribute problem in Jsp
 
Similar Threads
validate method is not being called
HttpSessionListener is not getting invoked
session problem using Tomcat 3.3.1
how to avoid show buttons
form submit not working!