• Post Reply Bookmark Topic Watch Topic
  • New Topic

using servlet to set bean property and display it in JSP

 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using a form to activate a servlet that will set the property of a bean and a JSP to retrieve the bean property. But, the Servlet fails to set the property of the bean.
The following are the codes written for the respective file:
Form:
*************************************************
<html><titile>Transaction Form</title>
<head><body>
<form action="http://localhost:8080/examples/servlet/TransactionHandler" method="post">
<input type="submit" value="click here to set the bean">
</form></body></head></html>
*****************************************************
TransactionHandler.java:
******************************************************
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import beans.TransactionBean;
public class TransactionHandler extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
TransactionBean tt= new TransactionBean();
tt.setGivenName("my given name");
tt.setFamilyName("my family name");
tt.setAddress("my address");
req.setAttribute("student", "tt");
res.sendRedirect("../jsp/transactionDisplay.jsp");
}
}
**************************************************************************
TransactionBean.java:
************************************************************
package beans;
public class TransactionBean
{
private String givenName;
private String familyName;
private String address;

public void setGivenName(String name)
{
givenName=name;
}
public void setFamilyName(String name)
{
familyName=name;
}
public void setAddress(String input)
{
address=input;
}

public String getGivenName()
{
return givenName;
}

public String getFamilyName()
{
return familyName;
}
public String getAddress()
{
return address;
}
}
********************************************************
transactionDisplay.jsp:
**********************************************************
<%@ page import="java.lang.*" %>
<html>
<head><title>Transaction Display Page</title>
</head>
<body>
<jsp:useBean id="student" class="beans.TransactionBean" scope="request" />
<%
String givenName=student.getGivenName();
String familyName=student.getFamilyName();
String address=student.getAddress();
%>

<p><b>Student Name:<%=givenName%> <%=familyName%></b></p>
<p><b>Address:<%=address%></b></p>
</body></html>
************************************************************************
This is the result i get:
Student Name:null null
Address:null
May I know what mistakes i have made?
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
When you use response.sendRedirect(req,res), it will send a response to the client with a new request for the JSP. so its like the client gets the bean set in the servlet but when the client requests for the jsp, it does not send the java bean back with the request.
try using the RequestDispatcher instead of the sendRedirect, it will work.
Also check out the threads on response.sendRedirect pro's and con's on this forum.
 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply.
I am new in programming and I have never use RequestDispatcher method before. Can someone show me how this method can be used for this problem?
Is it correct if i replace the line in TransactionHandler.java:
res.sendRedirect("../jsp/transactionDisplay.jsp");
with these:
String url= "../jsp/transactionDisplay.jsp";
RequestDispatcher dis=getServletContext().getRequestDispatcher(url);
dis.forward(req,res);
 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i try the codes above, I get this error:
java.lang.IllegalArgumentException: Path ../jsp/transactionDisplay.jsp does not start with a "/" character
at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:570)
at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:174)
at TransactionHandler.doPost(TransactionHandler.java:21)
..................................
.........somemore error codes......
When I replace the relative url with absolute url, I still get the following error:
java.lang.IllegalArgumentException: Path http://localhost:8080/examples/jsp/transactionDisplay.jsp does not start with a "/" character
at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:570)
at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:174)
....................................
What will the correct codes look like?
 
Afroz Ahmed
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yap,
First make sure that you are making the bean class object setted to the request not the text "tt".Change that as below
req.setAttribute("student", tt); and not "tt"
And when comes to RequestDispatcher below is the best rule....
1)When you are using ServletContext to get the RequestDiparcher object,use only relative path starting with /. (and not ..)
RequestDispatcher r=getServletContext().getRequestDispatcher("/jsp/a");
Because here in this context,servlet container do not know in which file you are presently and ../jsp/a can not be determined.
2)If you are getting RequestDispatcher from request,you can use ../jsp/a
since here container knows your present file location by request and can determine the exact file you need to send this request back.
RequestDispatcher r=req.getRequestDispatcher("../jsp/a");
rd.forward(req,resp);
Regards
Afroz Ahmed.
SCJP 1.4,SCWCD on track;
 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohd Afroz Ahmed,
I have changed the codes as what you have suggested. The jsp can display the bean property now.
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!