Ajuda! Como Atualizar JSP?

3 respostas
G

Estou com a seguinte página JSP:

<%!
	public void jspInit() {
	}
%>
<jsp:useBean id="theBean" class="com.gilsonap.CalculatorBean2"/>
<%
  int i = 2;
%>
<jsp:setProperty name="theBean" property="memory" value="<%= 100 * i %>"/>
The value of memory is <jsp:getProperty name="theBean" property="memory"/>
A página emprega o CalculatorBean2 mostrado abaixo:
package com.gilsonap;
public class CalculatorBean2 {
  private int memory;
  
  public void setMemory(int number) {
    memory = number;
  }
 
  public int getMemory() {
    return memory;
  }
}

E produz o seguinte resultado no navegador:
The value of memory is 200
Até aí normal.

Agora se eu quisesse, por exemplo de 5 em 5 segundos, aumentar i = i+1 e apresentar o resuldado novamente no navegador. Como faço?

3 Respostas

J

Você pode utilizar uma função javascript para recarregar a página:

<%! 
   public void jspInit() { 
   } 
%> 
<jsp:useBean id="theBean" class="com.gilsonap.CalculatorBean2"/> 
<% 
int i = 1;
 try {
  i = Integer.parseInt(request.getParameter("i")); 
 } catch (Exception e) {
 }
%> 
<jsp:setProperty name="theBean" property="memory" value="<%= 100 * i %>"/> 
The value of memory is <jsp:getProperty name="theBean" property="memory"/>

<javascript>
    function recarregar() {
        window.location.href="minhapagina.jsp?i=<%=i + 1%>";
    }

    window.setTimeOut("recarregar()", 5000);
</javascript>

Flw…

G

Não funcionou,
o browser está imprimindo o seguinte:

The value of memory is 100 [color=“blue”]mais o código javascript[/color] (que aqui não estou conseguindo postar…)

e a página permanece estática não alterando o valor.

Ajuda!

G

Com o código abaixo deu certo! Valeu!

<%! 
   public void jspInit() { 
	System.out.println("Iniciou");
   } 
%> 
<jsp:useBean id="theBean" class="com.gilsonap.CalculatorBean2"/> 
<% 
int i = 1; 
 try { 
  i = Integer.parseInt(request.getParameter("i")); 
 } catch (Exception e) { 
 } 
%> 

<jsp:setProperty name="theBean" property="memory" value="<%= 1 * i %>"/> 
The value of memory is <jsp:getProperty name="theBean" property="memory"/> 

<HTML>
<HEAD>
<TITLE>Testando Atualização</TITLE>
<javascript> 
  setTimeout("location='Atualiza.jsp?i=<%=i + 1%>'", 5000);
<javascript> 
</HEAD>
</HTML>
Criado 6 de janeiro de 2005
Ultima resposta 6 de jan. de 2005
Respostas 3
Participantes 2