Curso Online Introdução do Desenvolvimento Java para Web: JSP, JSTL e Sevlets
Estou com o seguinte problema na aula 4 do curso "Introdução ao Desenvolvimento Java para Web: JSP, JSTL e Servlets":
Se alguém puder me ajudar ficarei grato.
No submit do form de index.jsp a url só passou a enviar os parâmetros (add.jsp?name=Ricardo&age=20) após a adição da propriedade "name" na tag input dos campos name e age bem como a recuperação dos parâmetros só está sendo possível via request no add.jsp.
Os trechos destacados em vermelho foram acrescentados por mim como forma de contornar o problema.
Abaixo, segue o código-fonte e o resultado para análise:
index.jsp:
URL: http://localhost:8084/NextWebAppExample/add.jsp?name=Ricardo&age=20
Saída:
Using resquest the name is RicardoUsing request the age is 20
The name is nullThe age is 0
Se alguém puder me ajudar ficarei grato.
No submit do form de index.jsp a url só passou a enviar os parâmetros (add.jsp?name=Ricardo&age=20) após a adição da propriedade "name" na tag input dos campos name e age bem como a recuperação dos parâmetros só está sendo possível via request no add.jsp.
Os trechos destacados em vermelho foram acrescentados por mim como forma de contornar o problema.
Abaixo, segue o código-fonte e o resultado para análise:
index.jsp:
<%-- Document : index Created on : Dec 16, 2010, 2:04:25 PM Author : Ricardo--%>
<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%><%@page import="example.*"%><jsp:useBean class="example.MyClass" id="myBean" scope="session"/><jsp:setProperty name="myBean.member" property="*"/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>JSP Page!</h1> <% myBean.doProcessParam(request.getParameter("theParam"));%> <br/> <form action="add.jsp" method="get"> Enter the name: <input name=name id="name" size="30"/><br/> Enter the age: <input name=age id="age" size="10"/><br/> <input type="submit"/> </form> </body></html>
add.jsp:
<%-- Document : add Created on : Dec 16, 2010, 2:31:02 PM Author : Ricardo--%>
<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%><jsp:useBean class="example.MyClass" id="myBean" scope="session"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <% out.print("Using resquest the name is " + request.getParameter("name") + "<br/>"); out.print("Using request the age is " + Integer.valueOf(request.getParameter("age")) + "<br/><br/>");
out.print("The name is " + myBean.getMember().getName() + "<br/>"); out.print("The age is " + myBean.getMember().getAge()); %> </body></html>
MyPojo.java:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package example;
/** * * @author Ricardo */public class MyPojo { private static final long serialVersionUID = 1L;
private String name; private int age;
public MyPojo() { }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
MyClass.java:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package example;
/** * * @author Ricardo */public class MyClass {
private static final long serialVersionUID = 1L;
private MyPojo member = new MyPojo();
public MyClass() { System.out.println("My Class has been created!"); }
public void doProcessParam(String theParam) { System.out.println("The param is " + theParam); }
public MyPojo getMember() { return member; }
public void setMember(MyPojo member) { this.member = member; }}
URL: http://localhost:8084/NextWebAppExample/add.jsp?name=Ricardo&age=20
Saída:
Using resquest the name is RicardoUsing request the age is 20
The name is nullThe age is 0
Ricardo Silva
Curtidas 0
Respostas
Dyego Carmo
18/12/2010
coloque no cabecalho tambem:
<jsp:getProperty name="myBean.member" property="*"/>
e teste.
<jsp:getProperty name="myBean.member" property="*"/>
e teste.
GOSTEI 0