Código:
Este es el servlet:<jsp:include page="includes/header.jsp"/> <jsp:include page="includes/menu.jsp"/> <jsp:include page="includes/content.jsp"/> <form method="post" action="login"> Usuario: <input type="text" name="username"><%=request.getAttribute("logMes")==null?"":request.getAttribute("logMes") %><p> Password: <input type="password" name="password"> <p> <input type="submit" name="enviar" value="Enviar"> </form> <%=request.getAttribute("Logeatuta")==null?"":request.getAttribute("Logeatuta")%> <jsp:include page="includes/sidebar.jsp"/> <jsp:include page="includes/footer.jsp"/>
Código:
package Servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Dao.*; public class ServletLogin extends ServletBase{ HttpSession session; String returnCode; void execute (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int id = -1; String username=req.getParameter("username"); String password=req.getParameter("password"); session=req.getSession(true); session.setAttribute("berria","true"); returnCode="/login.jsp"; User user; UsersFacade facade=new UsersFacade(); Users users=facade.users(); int pos=exist(username,password,users); if(pos!=-1){ user=users.getUser(pos); session.setAttribute("isLogin", "true"); session.setAttribute("username", username); if(user.getIdUserType()==1){ session.setAttribute("userType", "Admin"); returnCode="/inicio.jsp"; } else{ if(user.getIdUserType()==2){ session.setAttribute("userType", "Trabajo"); returnCode="/inicio.jsp"; } } } session.setAttribute("iduser", pos); facade.closeFacade(); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(returnCode); dispatcher.forward(req, resp); } private int exist(String username, String password, Users users) { int i; for(i=0;i<users.getLenght();i++){ if(users.equals(users.getUser(i).getUsername())&&(users.equals(users.getUser(i).getPassword()))){ return i; } } return -1; } @Override Error[] validate(HttpServletRequest req, HttpServletResponse resp) { String user = req.getParameter("user")==null?"":req.getParameter("user"); String password = req.getParameter("password")==null?"":req.getParameter("password"); UsersFacade facade = new UsersFacade(); Users users=facade.users(); Error[] errors = new Error[10]; int i=0; if(exist(user,password,users)==-1){ errors[i++]= new Error("loginMessage","Erabiltzaile edo pasahitza ezegokiak!!!"); } if(password.length()<4){ errors[i++]= new Error("passwordLength","Pasahitzak hiru karaktere baino gehiago izan behar ditu!!!!"); } if(i==0)return null; else return errors; } @Override void forwardErrors(HttpServletRequest req, HttpServletResponse resp, Error[] errors) throws ServletException, IOException { String returnCode="/login.jsp"; for(int i=0;i<errors.length;i++){ if(errors[i]!=null){ req.setAttribute(errors[i].name,errors[i].value); } } RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(returnCode); dispatcher.forward(req, resp); } @Override public boolean securityRedirect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub return false; } @Override void forwardSecurity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } }
Y este es el web.xml:
Código:
No sé si se necesita algún otro dato, la cosa es que si cojo. La cosa es que hago el login y cuando le doy a enviar datos me va a esta dirección: "http://localhost:8080/Proiektua/login" y la página está en blanco, me falta algún otro servlet o algo por el estilo???<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Proiektua</display-name> <welcome-file-list> <welcome-file>login.html</welcome-file> <welcome-file>login.htm</welcome-file> <welcome-file>login.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> <servlet-name>ServletLogin</servlet-name> <servlet-class>Servlet.ServletLogin </servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletLogin</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <!-- --> <servlet> <servlet-name>ServletRedirect</servlet-name> <servlet-class>Servlet.ServletRedirect </servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletRedirect</servlet-name> <url-pattern>/RedirectServlet</url-pattern> </servlet-mapping> <!-- --> <servlet> <servlet-name>ServletLogout</servlet-name> <servlet-class>Servlet.ServletLogout </servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletLogout</servlet-name> <url-pattern>/logout</url-pattern> </servlet-mapping> <!-- --> </web-app>
Gracias de antemano