Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaScript & JSP Please help

 
Vit Wipoo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i'm very new to JSP. i'm doing a page that shows names of members and some information. i want to have one button for each member that allow users to see more information in another page when they click. i have an array of memberBean and i'm trying to set a specific bean(the one that is clicked) for session by "session.setAttribute("bean", memberBean);" to pass bean to another page. i know it looks easy but i really can't do that. codes below are my test case. please suggest me what to do or alternative ways to do this.
thanks a lot,
Vit
ps: it works fine if i use session.setAttribute("bean", tb) outside JavaScript.
[testBean1.jsp]
<%@ page session="true" %>
<HTML>
<BODY>
<jsp:useBean id="tb" scope="session" class="testBean" />
<%
tb.setName("Chanvit");
tb.setAge(25);
%>
<FORM NAME="FORM" ACTION="testBean2.jsp" METHOD="POST" onSubmit="return setSession()">
<INPUT TYPE="SUBMIT" VALUE="Click">
</FORM>
<script language="JavaScript">
<!--
function setSession() {
session.setAttribute("bean", tb);
}
//-->
</script>
</BODY>
</HTML>
[testBean2.jsp]
<HTML>
<BODY>Hey <br>
<jsp:useBean id="tb" scope="session" class="testBean" />
<%
tb = (testBean)session.getAttribute("bean");
%>
<%= tb.getName() %> <br>
<%= tb.getAge() %>
</BODY>
</HTML>
[testBean.java]
public class testBean {
private String name = null;
private int age = 0;
public testBean() {
}
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like one of your problems is that you are trying to access the session object from JavaScript. The session object does not carry over into the JavaScript world and it is only available inside the Java code of the JSP page.
Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic