Validar

5 respostas
J

index.jsp

<html>
<head>
    <title>Area Restrita</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
      <p align="center"/>
      <img src="http://4.bp.blogspot.com/-vy4EuaNfQ8A/TftT4wsHfbI/AAAAAAAAARo/7espbSSGm-c/s250/area_restrita.gif"/>
    <form id="form1" name="form1" method="post" action="Valida">
    <table width="200" border="0" align="center">
<tr>
    <td>Login:
    <td>
        <input type="text" name="login" id="login" />
</label>
</tr>
    <tr>
    <td>Senha:
<td>
    <input type="password" name="senha" id="senha" />
    </label>
</tr>
<tr>
<td>
<td>
    <input type="submit" name="submit" id="emviar" value="Logar" />
    <input type="reset" name="submit" id="limpar" value="Limpar"/>
</label>
</tr>
</table>
</form>
</body>
</html>

sucesso.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>Logado</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

erro.jsp

<html>
<head>
    <title>Pagina de Erro</title>
</head>
<body bgcolor="white">
    Usuario ou senha inválidos!!!<a href= "index.jsp">Tente outra vez</a>.
</body>
</html>

Servlet Valida.java

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


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

/**
 *
 * @author JOAO
 */
        public class Valida extends HttpServlet {
        @Override
   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
      String retorno = "Valida.java";
      if ("admin".equals(request.getParameter("login"))) {
         retorno = "sucesso.jsp";
      } else {
         retorno = "erro.jsp";
  }
 }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
    <display-name>Aplicação Web</display-name>
    <servlet>
        <servlet-name>Valida</servlet-name>
        <servlet-class>Valida</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Valida</servlet-name>
        <url-pattern>/Valida</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

5 Respostas

mausexdd

voce tem que receber o valor da JSP em uma variavél … lembra?

tipo login este que você esta comparando é oque , que valor ele tem?

e também esse “admin” é uma string apenas voce tem que setar o valor no admin e fazer
admin.getLogin

continua ai que voce ta no caminho certo da uma olhada naquele exemplo la

J

onde eu defino meu usuario e senha para loga?

Servlet Valida.java

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Valida extends HttpServlet {
    @Override
   protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String login = request.getParameter("login");
        String senha = request.getParameter("senha");

    RequestDispatcher d;
        Object confirmaSeha = null;

       if (senha.equals(confirmaSeha)) {
            d = request.getRequestDispatcher("sucesso.jsp");
        } else {
            d = request.getRequestDispatcher("erro.jsp");
        }
        try {
            d.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
mausexdd
public class Valida extends HttpServlet {  
    @Override  
   protected void doPost(HttpServletRequest request,  
            HttpServletResponse response) throws ServletException, IOException {  
        String login = request.getParameter("login");  //OK PEGOU O VALOR DIGITADO
        String senha = request.getParameter("senha");   //OK PEGOU O VALOR DIGITADO
  
    RequestDispatcher d;  
        Object confirmaSeha = null;   //OQUE SERIA ISTO
?
  //SE VOCE QUER DEFINIR UMA SENHA PADRÃO VOCE FALA AQUI
//SE SENHA(PEGO DO JSP).EQUALS( "DIGITE AQUI SUA SENHA QUE VOCE QUER PADRÃO EX 123"), FAÇA ALGUMA COISA SE NÃO FAÇA TAL COISA

       if (senha.equals("123") && login.equal("admin")) {  
            d = request.getRequestDispatcher("sucesso.jsp");  
        } else {  
            d = request.getRequestDispatcher("erro.jsp");  
        }  
        try {  
            d.forward(request, response);  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
}

Oque voce estava fazendo tava sem logica , voce estava comparando com um objeto nulo, não intendi oque voce estava tentando fazer.. tenta assim e tenta intender o contexto:D

FLw

mausexdd

Oque eu tinha falado para voce fazer antes setando um objeto era o seguinte, se voce tiver uma classe modelo usuario com os atributos login e senha voce poderia fazer isto.

String login = request.getParameter("login");  //OK PEGOU O VALOR DIGITADO  
        String senha = request.getParameter("senha");   //OK PEGOU O VALOR DIGITADO  

Usuario user= new Usuario();
user.setLogin(login);
user.setSenha(senha);//Passando os valores que voce pegou da JSP.

if (llogin.equals(user.getLogin) && senha.equals(user.getSenha)) {    
            d = request.getRequestDispatcher("sucesso.jsp");    
        } else {    
            d = request.getRequestDispatcher("erro.jsp");    
        }    
        try {    
            d.forward(request, response);    
        } catch (Exception ex) {    
            ex.printStackTrace();    
        }    
    }
J

deu certo abrç

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Valida extends HttpServlet {
    @Override
   protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        String login = request.getParameter("login");
        String senha = request.getParameter("senha");
        login.equals("admin");
        senha.equals("admin");

    RequestDispatcher d;
        Object confirmaSeha ="admin";
        Object confirmaLogin ="admin";

       if (senha.equals(confirmaSeha) && login.equals(confirmaLogin)) {
           request.getSession().setAttribute("logado", true);
            d = request.getRequestDispatcher("sucesso.jsp");
        } else {
            d = request.getRequestDispatcher("erro.jsp");
        }
        try {
            d.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Criado 31 de agosto de 2011
Ultima resposta 31 de ago. de 2011
Respostas 5
Participantes 2