This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with JSP, Servlet and Bean  RSS feed

 
Sarthishari Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Am new to Java Server Side Programming.
I have a Login.html Page.
It calls 'Name' Servlet.
There it sets 'name' Property of 'NameBean'.
After that control is transferred to the 'Name.jsp' page.
There i retreive the Property Value and display it.
But its not displaying.
I knew Nothing regarding Sessions.
Help me in this.
This is my Name Servlet Code :
NameBean name = new NameBean();

name.setName("sathish");

RequestDispatcher dis = request.getRequestDispatcher("../Name.jsp");
dis.forward(request,response);
This is my Name.jsp Page :
<jsp:useBean id="urname" scope="session" class="Bean.NameBean" >
<jsp:getProperty name="urname" property="name" />
</jsp:useBean>
 
Chris Smith
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd need to set the bean in the session somewhere, so that your later useBean tag can find it. That would look something like this:
request.getSession(true).setAttribute("urname", bean);
 
Shadab Khan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea..chris is right..you've gotta put your bean in the session..only then you can retrieve the set values
something like this will work
//NameServlet.java
NameBean bean=new NameBean();
bean.setName("satish");
HttpSession session=request.getSession(true);
session.setAttribute("mybean",bean);
ServletContext context=getServletContext();
RequestDispatcher rd;
rd=context.getRequestDispatcher("/Name.jsp");
rd.forward(req,res);
And in Name.jsp
<jsp:useBean id="mybean" scope="session" class="Bean.NameBean"/>
<%!mybean=(Bean.NameBean)session.getAttribute("mybean");%>
<%=mybean.getName()%>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!