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>