Share

OpenXava

Subscribe

Login y mantenimiento de sesión

You are viewing a single message from this topic. View all messages.

  1. 2009-11-04 11:16:43 UTC

    Hola de nuevo. Estoy intentando crear el login yo mismo pero no obtengo resultados. Mis conocimientos son bastante bajos pero por más que busco no encuentro solución. He cogido el código de un java servlet de una web. Es el siguiente:

    package org.openxava.pruebas.servlets;
    import java.io.IOException;  import
    java.io.PrintWriter;  
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import javax.servlet.http.HttpSession;  
    public class LoginHandler extends HttpServlet {   
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {  
        res.setContentType("text/html"); 
        PrintWriter out = res.getWriter(); 
        String account = req.getParameter("account"); 
        String password = req.getParameter("password"); 
        String pin = req.getParameter("pin"); 
        if (!allowUser(account, password, pin)) { 
          out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>"); 
          out.println("<BODY>Your login and password are invalid."); 
          out.println("You may want to <A HREF=\"/login.html\">try again</A>"); 
          out.println("</BODY></HTML>"); 
        }  else { 
          // Valid login. Make a note in the session object. 
          HttpSession session = req.getSession(); 
          session.setAttribute("logon.isDone", account);  
          // Try redirecting the client to the page he first tried to access
          try { 
            String target = (String) session.getAttribute("login.target"); 
            if (target != null) { 
              res.sendRedirect(target); 
              return; 
            } 
          } catch (Exception ignored) { 
          } 
          // Couldn't redirect to the target. Redirect to the site's home page. 
          res.sendRedirect("/"); 
        }
       }
       protected boolean allowUser(String account, String password, String pin) { 
            if(account.equals("admin")==true && password.equals("admin")==true) 
                return true; 
            else 
                return false;   
         } 
    }
    

    Tal y como pone en el wiki, he creado un archivo servlets.xml en la carpeta WEB-INF. Y he añadido este código:

        <servlet>
        <servlet-name>LoginHandler</servlet-name>
        <display-name>Login Handler</display-name>
        <description></description>
        <servlet-class>org.openxava.pruebas.servlets.LoginHandler</servlet-classr>
        </servlet>
    
    
    <servlet-mapping>
       <servlet-name>LoginHandler</servlet-name>
        <url-pattern>/pruebas</url-pattern>
    </servlet-mapping> 
    

    No se si me falta algo por hacer, pero cuando ejecuto el módulo de prueba no me sale nada de Login. Disculpen las molestias, Un saludo.

< Previous | 1 | Next >

Add a Reply

This forum does not allow anonymous participation.

Log in to add a reply. Not registered? Create an account to participate and receive email updates when replies are posted to this topic.