Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sharing data between jsp, bean, servlet

Status
Not open for further replies.

bigDaftie

Programmer
Feb 28, 2003
36
0
0
GB
Hi
I'm trying to do a simple web app. A jsp with a form fills a beans properties. A servlet then uses that bean data but I can't seem to get it to work.

This is a simplified version.....

my jsp page is as follows
<%@ page contentType=&quot;text/html; charset=iso-8859-1&quot; language=&quot;java&quot; import=&quot;java.sql.*&quot; errorPage=&quot;&quot; %>
<%@ taglib prefix=&quot;c&quot; uri=&quot; %>
<jsp:useBean id=&quot;test&quot; scope=&quot;session&quot; class=&quot;servletlesson.TestBean&quot; >
<jsp:setProperty name=&quot;test&quot; property=&quot;*&quot; />
</jsp:useBean>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<body>
<form method=&quot;post&quot; action=&quot;test&quot;>
<table width=&quot;&quot; border=&quot;0&quot; cellspacing=&quot;&quot; cellpadding=&quot;&quot;>
<tr>
<td>test</td>
<td><input name=&quot;test&quot; type=&quot;text&quot; ></td>
</tr>
<tr>
<td><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;submit&quot; ></td>
</tr>
</table>
</form>
</body>
</html>

my bean is
import java.io.*;
public class TestBean implements Serializable{

private String test;

public TestBean() {
}

public void setTest(String aTest){
this.test=aTest;
}

public String getTest(){
return test;
}
}

and my servlet is
public class TestServlet extends HttpServlet {

public void init(){
}//init

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{

HttpSession sess=req.getSession();
TestBean test=(TestBean)sess.getAttribute(&quot;test&quot;);
PrintWriter out=res.getWriter();
out.println(&quot;This is a test &quot;+test.getTest());
}//doPost

}

Am I going about this the wrong way?? Is it wrong to share data between beans and servlets in this way?

All i get from test.getTest() is null whereas I want to return my form value that the user types in.

If anyone can help I'll be eternally grateful.
Thanks a lot,
B
 
You may consider using session to share information between JSPs and servlets
 
Thanks for getting back to me. I was trying to use the bean because I really like the idea of the lines

<jsp:useBean id=&quot;test&quot; scope=&quot;session&quot; class=&quot;servletlesson.TestBean&quot; >
<jsp:setProperty name=&quot;test&quot; property=&quot;*&quot; />
</jsp:useBean>


It seems to do all the work for you. I'm not sure what you mean by using session to share the data, cos I thought I was doing that! Do you mean that you can set the form data to have session scope? I know that I can get the form values from doing something like request.getParameter(&quot;name of attribute&quot;) but I like the idea of storing the data in a bean, maybe putting it in a database, storing it in a cookie etc but calling different servlets that can then all access this one bean. Maybe my bean code is wrong? Any other suggestions? Thanks a lot,
Bruce
 
Ok, solved it!! Got the page to call itself, and then forwarded to the servlet.

<body>
<form method=&quot;post&quot; action=&quot;test.jsp&quot;>
<table width=&quot;&quot; border=&quot;0&quot; cellspacing=&quot;&quot; cellpadding=&quot;&quot;>
<tr>
<td>test</td>
<td><input type=&quot;text&quot; name=&quot;test&quot; value=&quot;hello there is this working&quot;></td>
</tr>
<tr>
<td><input type=&quot;submit&quot; value=&quot;Submit&quot; ></td>
</tr>
</table>
</form>
<%
if (request.getMethod().equals(&quot;POST&quot;)) {
%>
<jsp:useBean id=&quot;test&quot; scope=&quot;session&quot; class=&quot;servletlesson.TestBean&quot; >
<jsp:setProperty name=&quot;test&quot; property=&quot;*&quot; />
</jsp:useBean>
<jsp:forward page=&quot;test&quot; />
<%
}
%>
</body>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top