Jsp Basico..Mas nao consigo fazer aonde estou errand?

5 respostas
A

Gostaria de Fazer o a tag pegar um campo do form digitado pelo usuario e duplica-lo.

erro:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: passorg.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

root cause

java.lang.NumberFormatException: For input string: “pass”

java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

java.lang.Integer.parseInt(Integer.java:447)

java.lang.Integer.parseInt(Integer.java:497)

br.estacio.Duplicar.otherDoStartTagOperations(Duplicar.java:52)

br.estacio.Duplicar.doStartTag(Duplicar.java:148)

org.apache.jsp.TPExe01_jsp._jspx_meth_minhatag_005fDuplicar_005f0(TPExe01_jsp.java:111)

org.apache.jsp.TPExe01_jsp._jspService(TPExe01_jsp.java:82)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.

5 Respostas

A

classe dulpicar :

  • Duplicar.java
  • Created on 20 de Setembro de 2007, 19:37
    */

package br.estacio;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.BodyContent;

import javax.servlet.jsp.tagext.BodyTagSupport;

import javax.swing.*;

/**
  • Generated tag handler class.
  • @author aluno
  • @version
    */

public class Duplicar extends BodyTagSupport {

/**
 * Initialization of numero property.
 */
private int numero;
private String numero2;

/** Creates new instance of tag handler */
public Duplicar() {
    super();
}

////////////////////////////////////////////////////////////////
///                                                          ///
///   User methods.                                          ///
///                                                          ///
///   Modify these methods to customize your tag handler.    ///
///                                                          ///
////////////////////////////////////////////////////////////////

/**
 * Method called from doStartTag().
 * Fill in this method to perform other operations from doStartTag().
 *
 */
private void otherDoStartTagOperations()  {
   
    
    JspWriter out = pageContext.getOut();
    try {
        numero = Integer.parseInt(numero2);
        out.println(numero*2);
    } catch (IOException ex) {
        ex.printStackTrace();
    }

     //
    // TODO: code that performs other operations in doStartTag
    //       should be placed here.
    //       It will be called after initializing variables,
    //       finding the parent, setting IDREFs, etc, and
    //       before calling theBodyShouldBeEvaluated().
    //
    //       For example, to print something out to the JSP, use the following:
    //
    //   try {
    //       JspWriter out = pageContext.getOut();
    //       out.println("something");
    //   } catch (IOException ex) {
    //       // do something
    //   }
    //
    //
}

/**
 * Method called from doEndTag()
 * Fill in this method to perform other operations from doEndTag().
 *
 */
private void otherDoEndTagOperations() {
    //
    // TODO: code that performs other operations in doEndTag
    //       should be placed here.
    //       It will be called after initializing variables,
    //       finding the parent, setting IDREFs, etc, and
    //       before calling shouldEvaluateRestOfPageAfterEndTag().
    //
}

/**
 * Fill in this method to process the body content of the tag.
 * You only need to do this if the tag's BodyContent property
 * is set to "JSP" or "tagdependent."
 * If the tag's bodyContent is set to "empty," then this method
 * will not be called.
 */
private void writeTagBodyContent(JspWriter out, BodyContent bodyContent) throws IOException {
    //
    // TODO: insert code to write html before writing the body content.
    // e.g.:
    //
    // out.println("<strong>" + attribute_1 + "</strong>");
    // out.println("   <blockquote>");
    
    //
    // write the body content (after processing by the JSP engine) on the output Writer
    //
    bodyContent.writeOut(out);
    
    //
    // Or else get the body content as a string and process it, e.g.:
    //     String bodyStr = bodyContent.getString();
    //     String result = yourProcessingMethod(bodyStr);
    //     out.println(result);
    //
    
    // TODO: insert code to write html after writing the body content.
    // e.g.:
    //
    // out.println("   </blockquote>");
    
    
    // clear the body content for the next time through.
    bodyContent.clearBody();
}

////////////////////////////////////////////////////////////////
///                                                          ///
///   Tag Handler interface methods.                         ///
///                                                          ///
///   Do not modify these methods; instead, modify the       ///
///   methods that they call.                                ///
///                                                          ///
////////////////////////////////////////////////////////////////

/**
 * This method is called when the JSP engine encounters the start tag,
 * after the attributes are processed.
 * Scripting variables (if any) have their values set here.
 * @return EVAL_BODY_BUFFERED if the JSP engine should evaluate the tag body, otherwise return SKIP_BODY.
 * This method is automatically generated. Do not modify this method.
 * Instead, modify the methods that this method calls.
 */

public int doStartTag() throws JspException, JspException {
    otherDoStartTagOperations();
    
    if (theBodyShouldBeEvaluated()) {
        return EVAL_BODY_BUFFERED;
    } else {
        return SKIP_BODY;
    }
}

/**
 * This method is called after the JSP engine finished processing the tag.
 * @return EVAL_PAGE if the JSP engine should continue evaluating the JSP page, otherwise return SKIP_PAGE.
 * This method is automatically generated. Do not modify this method.
 * Instead, modify the methods that this method calls.
 */
public int doEndTag() throws JspException, JspException {
    otherDoEndTagOperations();
    
    if (shouldEvaluateRestOfPageAfterEndTag()) {
        return EVAL_PAGE;
    } else {
        return SKIP_PAGE;
    }
}

/**
 * This method is called after the JSP engine processes the body content of the tag.
 * @return EVAL_BODY_AGAIN if the JSP engine should evaluate the tag body again, otherwise return SKIP_BODY.
 * This method is automatically generated. Do not modify this method.
 * Instead, modify the methods that this method calls.
 */
public int doAfterBody() throws JspException {
    try {
        //
        // This code is generated for tags whose bodyContent is "JSP"
        //
        BodyContent bodyContent = getBodyContent();
        JspWriter out = bodyContent.getEnclosingWriter();
        
        writeTagBodyContent(out, bodyContent);
    } catch (Exception ex) {
        handleBodyContentException(ex);
    }
    
    if (theBodyShouldBeEvaluatedAgain()) {
        return EVAL_BODY_AGAIN;
    } else {
        return SKIP_BODY;
    }
}

/**
 * Handles exception from processing the body content.
 */
private void handleBodyContentException(Exception ex) throws JspException {
    // Since the doAfterBody method is guarded, place exception handing code here.
    throw new JspException("error in NewTag: " + ex);
}

/**
 * Fill in this method to determine if the rest of the JSP page
 * should be generated after this tag is finished.
 * Called from doEndTag().
 */
private boolean shouldEvaluateRestOfPageAfterEndTag()  {
    //
    // TODO: code that determines whether the rest of the page
    //       should be evaluated after the tag is processed
    //       should be placed here.
    //       Called from the doEndTag() method.
    //
    return true;
}

/**
 * Fill in this method to determine if the tag body should be evaluated
 * again after evaluating the body.
 * Use this method to create an iterating tag.
 * Called from doAfterBody().
 */
private boolean theBodyShouldBeEvaluatedAgain() {
    //
    // TODO: code that determines whether the tag body should be
    //       evaluated again after processing the tag
    //       should be placed here.
    //       You can use this method to create iterating tags.
    //       Called from the doAfterBody() method.
    //
    return false;
}

/**
 * Fill in this method to determine if the tag body should be evaluated.
 * Called from doStartTag().
 */
private boolean theBodyShouldBeEvaluated() {
    //
    // TODO: code that determines whether the body should be
    //       evaluated should be placed here.
    //       Called from the doStartTag() method.
    //
    return true;
}

/**
 * Setter for the numero attribute.
 */
public void setNumero(int value) {
    this.numero = value;
}

 public void setNumero2(String numero2) {
    this.numero2 = numero2;
}

}

A

index jsp :

<%@taglib uri="/WEB-INF/tlds/duplicar" prefix=“minhatag” %>
<%@page contentType=“text/html”%>
<%@page pageEncoding=“UTF-8”%>
<%–
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library… action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
–%>
<%–
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
–%>

<!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>

&lt;h1&gt;JSP Page&lt;/h1&gt;

    &lt;form action="TPExe01.jsp" method="POST"&gt;
        &lt;input type="submit" value="envia" /&gt;
        &lt;input type="password" name="pass" value="" /&gt;
     &lt;/form&gt;
&lt;/body&gt;

</html>

A

classe TPExe01

<%@taglib uri="/WEB-INF/tlds/duplicar" prefix=“minhatag” %>
<%@page contentType=“text/html”%>
<%@page pageEncoding=“UTF-8”%>

<%–
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library… action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
–%>
<%–
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
–%>

<!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>

&lt;minhatag:Duplicar numero2="pass" /&gt;

   
&lt;/body&gt;

</html>

A

tld:

<?xml version=“1.0” encoding=“UTF-8”?>

<taglib version=“2.0” xmlns=“<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a> xmlns:xsi=“<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a> xsi:schemaLocation=“<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a> web-jsptaglibrary_2_0.xsd”>

<tlib-version>1.0</tlib-version>

<short-name>duplicar</short-name>

<uri>/WEB-INF/tlds/duplicar</uri>

<!-- A validator verifies that the tags are used correctly at JSP

translation time. Validator entries look like this:

<validator>

<validator-class>com.mycompany.TagLibValidator</validator-class>

<init-param>

<param-name>parameter</param-name>

<param-value>value</param-value>

</init-param>

</validator>

–>

<!-- A tag library can register Servlet Context event listeners in

case it needs to react to such events. Listener entries look

like this:

<listener>

<listener-class>com.mycompany.TagLibListener</listener-class>

</listener>

–>

<tag>

<name>Duplicar</name>

<tag-class>br.estacio.Duplicar</tag-class>

<body-content>scriptless</body-content>

<attribute>

<name>numero2</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

<type>java.lang.String</type>

</attribute>

<attribute>

<name>numero</name>

<rtexprvalue>true</rtexprvalue>

<type>int</type>

</attribute>

</tag>

</taglib>

xml:

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app xmlns=“http://java.sun.com/xml/ns/j2ee
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
version=“2.4”>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>

BrunoBastosPJ

Opa… Vou te dar uma dica… Colar a classe toda sem utilizar BBCode é meio complicado para os outros entenderem…

Dê uma olhada nestas linhas da classe Duplicar.java

Você provávelmente está tentando converter a String pass para um int.

Se você der uma olhada na API
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

Vai ver que o método parseInt lança essa exceção :wink:

Espero ter ajudado.

Criado 19 de outubro de 2007
Ultima resposta 20 de out. de 2007
Respostas 5
Participantes 2