Erro no servlet de uma Calculadora

5 respostas
W

É um servlet de uma calculadora, estou sem ideia do que fazer para funcionar
Eu acho que o problema e no arquivo web.xml mas não tenho certeza absoluta
Não exibe erro no código, esse erro ocorrer quando vai executar
Adicionei o projeto todo no anexo

<html>
<head>
<title>Calculadora</title>
</head>
<body>
<form method= get action="AulaCalc/Calculadora">
        Digite N1
        <input type= text name ="n1"> <P>
        
        Digite N2
        <input type= text name = "n2"> <br>   
            <input type= Radio name ="Radio" value ="+"> + <br>
            <input type= Radio name ="Radio" value ="-"> - <br>
            <input type= Radio name ="Radio" value ="*"> * <br>
            <input type= Radio name ="Radio" value ="/"> / <br>
            <input type= Submit>
        </form>
</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Calculadora</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>au061010</display-name>
    <servlet-name>Calculadora</servlet-name>
    <servlet-class>aulaCalc.Calculadora</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Calculadora</servlet-name>
    <url-pattern>/Calculadora</url-pattern>
  </servlet-mapping>
</web-app>

package aulaCalc;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class Calculadora extends HttpServlet
{
	
		public void doGet(HttpServletRequest req,
				HttpServletResponse res)
	    	throws ServletException, IOException {
	
	    res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        int n1 = Integer.parseInt(req.getParameter("n1"));
        int n2 = Integer.parseInt(req.getParameter("n2"));
        String operador = req.getParameter("radio");
        int result = 0;
        
        if (operador.equals("+"))
            result = n1 + n2;
        
        if (operador.equals("-"))
            result = n1 - n2;
         
        if (operador.equals("*"))
            result = n1 * n2;
        
        if (operador.equals("/"))
            result = n1 / n2;
        
        out.println("<HTML>\n" +
                "<HEAD><TITLE>Resultado</TITLE></HEAD>\n" +
                "<BODY>\n" + n1 + " " +
                operador + " " + n2 + " = " + result +
                "</BODY></HTML>");
		}
}

Exibe a seguinte pagina com essa mensagem:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/8.0.12

No console do Eclipse isto:

Set 16, 2014 11:14:46 PM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 1254 ms
Set 16, 2014 11:14:46 PM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service Catalina
Set 16, 2014 11:14:46 PM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/8.0.12
Set 16, 2014 11:14:47 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFORMAÇÕES: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [260] milliseconds.
Set 16, 2014 11:14:47 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["http-nio-7080"]
Set 16, 2014 11:14:47 PM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["ajp-nio-7009"]
Set 16, 2014 11:14:47 PM org.apache.catalina.startup.Catalina start
INFORMAÇÕES: Server startup in 1394 ms

5 Respostas

alexfe
O mapeamento no seu WEB.XML está assim
<url-pattern>/Calculadora</url-pattern>
e o action e alguns detalhes do seu form estão errados. O action fica dessa forma ai em baixo.
<html>
<head>
<title>Calculadora</title>
</head>
<body>
<form method="get" action="Calculadora">
        Digite N1
        <input type= "text" name ="n1"> <P>
        
        Digite N2
        <input type= "text" name = "n2"> <br>   
            <input type= Radio name ="Radio" value ="+"> + <br>
            <input type= Radio name ="Radio" value ="-"> - <br>
            <input type= Radio name ="Radio" value ="*"> * <br>
            <input type= Radio name ="Radio" value ="/"> / <br>
            <input type= Submit>
        </form>
</body>
</html>
W

Sera que tenha que adicionar alguma coisa a biblioteca do projeto?
Se for quais?
Estou usando Eclipse EE Luna Release e Apache Tomcat 8.0

Mudou um pouco a mensagem de erro agora:

HTTP Status 404 - /AulaCalc/

type Status report

message /AulaCalc/

description The requested resource is not available.

Apache Tomcat/8.0.12

No console exibe isto:

Set 18, 2014 10:58:57 AM org.apache.catalina.core.AprLifecycleListener init
INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\ ;C:\Program Files\Java\jre7\bin\bin;.
Set 18, 2014 10:58:57 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:AulaCalc’ did not find a matching property.
Set 18, 2014 10:58:57 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler [“http-nio-9080”]
Set 18, 2014 10:58:58 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
Set 18, 2014 10:58:58 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler [“ajp-nio-9009”]
Set 18, 2014 10:58:58 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
Set 18, 2014 10:58:58 AM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 1243 ms
Set 18, 2014 10:58:58 AM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service Catalina
Set 18, 2014 10:58:58 AM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/8.0.12
Set 18, 2014 10:58:59 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFORMAÇÕES: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [265] milliseconds.
Set 18, 2014 10:58:59 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler [“http-nio-9080”]
Set 18, 2014 10:58:59 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler [“ajp-nio-9009”]
Set 18, 2014 10:58:59 AM org.apache.catalina.startup.Catalina start
INFORMAÇÕES: Server startup in 1436 ms

alexfe

onde ta esse mapeamento – > /AulaCalc/ troca por /AulaCalc

W

O código, fiz o que você falou e não apresenta erro quando não executado
Creio que o erro não seja mais de código e sim de biblioteca faltando mas não sei quais?
Vi esse artigo sobre biblioteca necessária a acrescentar a projeto web:
http://www.devmedia.com.br/criando-um-projeto-web-no-eclipse/27375

alexfe

Não tem nehum erro mesmo, porque quando falta biblioteca tb da erro :slight_smile:

Criado 16 de setembro de 2014
Ultima resposta 19 de set. de 2014
Respostas 5
Participantes 2