• Post Reply Bookmark Topic Watch Topic
  • New Topic

bean not populated

 
kelly goedert
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following jsp:
----index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Catalogus</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body topmargin="0" leftmargin="0">
<form name="formEstadoEditar" method="post" action="/Teste/edicao" >
Estado:<input type="text" name="nome" size="40" class="campo">
<input type="text" name="txtLogin" size="40" class="campo">

<input type="submit" name="btnOK" value="OK" class="botao">
<a href="/Teste/edicao">Enviar</a>

</form>
<%-- Create the bean only when the form is posted --%>
<%
if (request.getMethod().equals("POST")) { %>
<jsp:useBean id="estado" class="pacote.Estado">
<jsp:setProperty name="estado" property="nome" value="Hello"/>
</jsp:useBean>
<%
}
%>
</body>
</html>
I need that the values I type on the fields to be passed to a servlet. But the bean is always null. Here is the servlet code:
---- Servlet
package pacote;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import javax.servlet.ServletContext;
public class EdicaoAtivarServlet extends HttpServlet {
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession s = request.getSession();

//ServletContext context = getServletContext();
Object bean = request.getSession().getAttribute("estado");

if (bean == null)
System.out.println("bean null");
else{

System.out.println(((Estado)bean).getTxtLogin());
}


}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
And here is the bean code:
----Bean
package pacote;

public class Estado {
private String nome = "teste";
private String txtLogin = "";
public Estado() {
nome = "qqqqq";
txtLogin = "droga";

}

public String getNome() {
return nome;
}
public void setNome(String string) {
this.nome = string;
}
public String getTxtLogin() {
return txtLogin;
}
public void setTxtLogin(String string) {
this.txtLogin = string;
}
}

Could anyone help me with this please??
Thanx
[ November 12, 2003: Message edited by: Bear Bibeault ]
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like you want the bean to have SESSION scope when you create it, but it only has PAGE scope at the moment. #
add scope="session" to your jsp:usebean
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelly, please be sure to use the UBB code tags to surround your code examples in future posts. That way the formatting will be preserved and make it easier for all to read.
thanks,
bear
 
kelly goedert
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did put the scope = "session" but it didn�t work.
 
kelly goedert
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also included this tag in jsp
<jsp:setProperty name="estado" property="*"/>
In place of the one that was setting only one property. But it still doesn�t work. I don�t know what else to try.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code at the bottom of your JSP is in the same page as the FORM. The data from this form cannot be processed on the same JSP. The FORM POSTS to your servlet, so I think this is where you want to transfer this code.
 
kelly goedert
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i wanted, don�t know if it is possible or not, is the bean in the jsp to be populated with the values I typed in the form, then I could use this session bean in the servlet that also acesses the session. Did I make myself clear??
Is it possible?
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet can get the data itself by looking at the implicit request object and using request.getParameter("blah"); you don't need to go through a bean. If you had another JSP page instead of a Servlet then you might use that JSP code you had but only in the second page!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!