2
respostas

URL SERVLET NAO ENCONTRADA

Quando vou conectar com o tomcat da esse erro:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <!-- General description of your web application -->

    <display-name>Julien</display-name>
    <description>Teste</description>

    <!-- #################################################  -->


    <servlet>
      <servlet-name>Incluir</servlet-name>
      <servlet-class>br.com.ppto.julien.atividades.Incluir</servlet-class>
      <load-on-startup>6</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>Incluir</servlet-name>
      <url-pattern>/PaginaInicio</url-pattern>

    </servlet-mapping>

      <servlet>
      <servlet-name>Operacoes</servlet-name>
      <servlet-class>br.com.ppto.julien.dao.Operacoes</servlet-class>
      <load-on-startup>5</load-on-startup>
    </servlet>

     <welcome-file-list>
      <welcome-file>PaginaInicio.html</welcome-file>
    </welcome-file-list>

    <resource-ref>
      <description>db connection</description>
      <res-ref-name>jdbc/serverdb</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>











    </web-app>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Incluir" method="post">
Col1 : <input type ="text" name="col1" >
Col2: <input type ="text" name="col2" >
Col3:<input type ="text" name="col3" >
Col4:<input type ="text" name="col4" >
Col5:<input type ="text" name="col5" >
<input type="submit" value = "Enviar">

</form>

</body>
</html>
package br.com.ppto.julien.atividades;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

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

import br.com.ppto.julien.beans.Tabela1;
import br.com.ppto.julien.dao.Operacoes;

public class Incluir extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Operacoes op = new Operacoes();
        PrintWriter writer = resp.getWriter();
        String col1 = req.getParameter("col1");
        String col2 = req.getParameter("col3");
        int col3 = Integer.parseInt(req.getParameter("col3"));
        double col4 = Double.parseDouble(req.getParameter("col4"));
        double col5 = Double.parseDouble(req.getParameter("col5"));
         Tabela1 tabe = new Tabela1(col1,col2,col3,col4,col5);

         try {
            op.Inserir(req,resp,tabe);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



}
}
2 respostas

ESSE E O DAO

package br.com.ppto.julien.dao;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import br.com.ppto.julien.beans.Tabela1;
import br.com.ppto.julien.webtstdb.WebTstDB1;

public class Operacoes extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private static final boolean DEBUG1 = true;
    private static final int TB_NONE = 0;
    private DataSource ds = null;



    public void Inserir(HttpServletRequest request, HttpServletResponse response, Tabela1 t)
            throws SQLException {
                Connection conn = null;
                PreparedStatement stmt = null;






                conn = ds.getConnection();
                stmt = conn.prepareStatement("INSERT INTO tabtest1 (col1,col2,col3,col4,col5) VALUES (?,?,?,?,?) ");
                stmt.setString(1, t.getCol1());
                stmt.setString(2, t.getCol2());
                stmt.setInt(3, t.getCol3());
                stmt.setDouble(4, t.getCol4());
                stmt.setDouble(5, t.getCol5());

                stmt.execute();
                stmt.close();
                conn.close();






            }


public void Alterar(HttpServletRequest request, HttpServletResponse response, Tabela1 t)
        throws SQLException {
    Connection conn = null;
    PreparedStatement stmt = null;
    conn = ds.getConnection();

         stmt = conn.prepareStatement("UPDATE tabetest1 col3=?,col4=?,col5=? where col1=? and col2=?");
         stmt.setInt(1, t.getCol3());
         stmt.setDouble(2, t.getCol4());
         stmt.setDouble(3, t.getCol4());
         stmt.setString(4, t.getCol1());
         stmt.setString(5, t.getCol2());

         stmt.executeUpdate();
         stmt.close();
            conn.close();



}
public void Apagar(HttpServletRequest request, HttpServletResponse response, Tabela1 t)
        throws SQLException {

    Connection conn = null;
    PreparedStatement stmt = null;
    conn = ds.getConnection();

         stmt=conn.prepareStatement("DELETE FROM tabetest1 where col1=? ");
         stmt.setString(1, t.getCol1());
         stmt.executeUpdate();
         stmt.close();
        conn.close();





}

Olá Julien, a sua servlet Operacoes está sem mapeamento no arquivo web.xml. Você precisa acrescentar algo como:

<servlet-mapping>
    <servlet-name>Operacoes</servlet-name>
    <url-pattern>/Operacoes</url-pattern>
</servlet-mapping>

Nesta servlet estão os métodos do seu DAO. Neste caso, essa classe não precisa ser um servlet . Repare que ela não depende de HttpServletRequest e HttpServletResponse para funcionar e nem é sua responsabilidade tratar requisições. Você pode isolar este código em uma classe simples, sem extender HttpServlet e sem necessidade de mapeamento no web.xml.

Neste curso de JDBC, o padrão DAO é explicado com mais detalhes e pode te interessar: https://cursos.alura.com.br/course/jdbc

Espero ter ajudado e bons estudos! :)