Versão atual:

Chamar Método pelo Servlet

Olá,

Estou tentando acessar a minha classe Java através de um Servlet.

Classe :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author antun_000
 */
public class UsuarioDAOImp implements UsuarioDAO{
    private Connection conn;


     public UsuarioDAOImp( ) throws Exception{
        try {
            this.conn = ConnectionFactory.getConnection( );

        } catch( Exception e ) {
            throw new Exception( "Erro: " +
                    "\n" + e.getMessage( ) );
        }  }

    /**
     *
     * @return
     */
    @Override
    public Boolean validaLogin(String login,String senha)
    {

        String SQL = "Select login,senha from usuario where login=? and senha = ?";
             try {
                  PreparedStatement stmt = conn.prepareStatement(SQL,   
                                                          ResultSet.TYPE_SCROLL_INSENSITIVE,   
                                                          ResultSet.CONCUR_READ_ONLY);  
                  stmt.setString(1, login);
                  stmt.setString(2, senha);
                  ResultSet rs = stmt.executeQuery(SQL);
                  ArrayList list = new ArrayList();
                  while(rs.next()){
                   list.add(rs.getString("NOME_DA_COLUNA"));                   
                  }
                  if(!list.isEmpty())
                      return false;
                  else
                      return true;

             } catch (SQLException ex) {
                 ex.printStackTrace();
             } 


        return null;
    }
}

Servlet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author antun_000
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LoginServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        Boolean validaLogin;
        validaLogin = UsuarioDAOImp.validaLogin(request.getParameter("Login"),request.getParameter("Senha"));
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

Na linha " validaLogin = UsuarioDAOImp.validaLogin(request.getParameter("Login"),request.getParameter("Senha"));" Está dando erro "non-static method validaLogin(String,String) cannot be referenced from a static context". Alguém pode ajudar?

Versão(1):

Ver a versão formatada

Chamar Método pelo Servlet

Comentário

new question