Hallo,
Ich habe ein problem mit JSP und Beans. Es sieht so aus, als ob die Bean nicht befuellt wird. Vielleicht kann mir jemand weiterhelfen?
Der Code ist:
Formular:
Die Bean:
Und in diesem Teil vom Servlet soll es verarbeitet werden:
Auch mit dem Debugger bekomme ich nur leere Werte ("").
Kann mir jemand einen Tip geben?
Danke schon mal.
Fantasma
Ich habe ein problem mit JSP und Beans. Es sieht so aus, als ob die Bean nicht befuellt wird. Vielleicht kann mir jemand weiterhelfen?
Der Code ist:
Formular:
HTML:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="newPersona" class="personapack.Persona" scope="session"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AgregarPersona</title>
</head>
<body>
<jsp:setProperty name="newPersona" property="*"/>
<form action="/Ej3personas/ServiceHandler?opcion=agregarPers" method=post>
Nombre: <input type="text" name="nombre"/><br>
Apellido: <input type="text" name ="apellido"/><br>
Nacimiento: <input type="text" name ="nacimiento"/><br>
Email: <input type="text" name ="email"/><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
</body>
</html>
Die Bean:
Java:
package personapack;
public class Persona {
private String nombre= "";
private String apellido="";
private String nacimiento="";
private String email="";
private int numero =0;
private static int contador =0;
public Persona(){
}
/*public Persona(String nombre, String apellido, String nacimiento, String email){
this.nombre=nombre;
this.apellido=apellido;
this.nacimiento=nacimiento;
this.email=email;
this.numero =0;
}*/
public void setNombre(String nombre){
this.nombre=nombre;
}
public void setApellido(String apellido){
this.apellido=apellido;
}
public void setNacimiento(String nacimiento){
this.nacimiento=nacimiento;
}
public void setEmail(String email){
this.email=email;
}
public void setNumero(){
this.numero = ++contador;
}
public String getNombre(){
return nombre;
}
public String getApellido(){
return apellido;
}
public String getNacimiento(){
return nacimiento;
}
public String getEmail(){
return email;
}
public int getNumero(){
return numero;
}
}
Und in diesem Teil vom Servlet soll es verarbeitet werden:
Java:
protected void ejecutar (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String opcion = (String) request.getParameter("opcion");
request.removeAttribute("opcion");
System.out.println(opcion);
else if(opcion.equals("agregarPers")){
HttpSession session = request.getSession();
personapack.Persona tmPersona=
(personapack.Persona)session.getAttribute("newPersona");
session.removeAttribute("newPersona");
ControlPersonas.getControlPersonas().agregarPersona(tmPersona);
System.out.println(tmPersona.getApellido());//--> Zeigt nichts an!
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.print("<html>");
out.print("<body>");
out.print(opcion); //----------------------> Zeigt den wert der Operation an = OK
out.print(tmPersona.getNombre()); //------> Zeigt nichts an!
out.print(tmPersona.getNumero());//-------> Zeigt 0 an = OK
out.print("</body>");
}
Auch mit dem Debugger bekomme ich nur leere Werte ("").
Kann mir jemand einen Tip geben?
Danke schon mal.
Fantasma