无法从表单中获取我的电子邮件值

时间:2016-06-14 15:40:08

标签: forms tomcat servlets java-ee httprequest

我正在尝试从我的表单中收到电子邮件,帖子请求很好是firebug窗口,但是即使我的变量设置为名称"电子邮件&#,我也无法正确获取它34;,我的代码正在寻找"用户名"请求中的参数(为btw为null)并将其值放在我想发送给DB的变量中。

如果因为这些非更新变量而导致我的eclipse没有被破坏,我正在掩饰自己....我每次尝试代码时都清除了我的tomcat(7.0.59)并且我已经尝试过了卸载tomcat并重新安装它。 你有任何线索吗?

这是我的总代码

package com.isep.connexion.model;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.isep.eleve.model.Eleve;
import com.isep.metier.Users;

public final class ConnexionForm {
    private static final String CHAMP_EMAIL = "email";
    private static final String CHAMP_PASS   = "password";  
    private String              resultat;
    private Map<String, String> erreurs      = new HashMap<String, String>();

    public String getResultat() {
        return resultat;
    }

    public Map<String, String> getErreurs() {
        return erreurs;
    }

    public Users connecterUser( HttpServletRequest request ) {
        /* getting data from form */
        String email = getValeurChamp( request, CHAMP_EMAIL );
        String password = getValeurChamp( request, CHAMP_PASS );

        Users eleve = new Users();

        /* email validation. */
        try {
            validationEmail(email);
        } catch ( Exception e ) {
            setErreur( CHAMP_EMAIL, e.getMessage() );
        }
        eleve.setEmail(email);

        /* PW validation */
        try {
            validationMotDePasse(password);
        } catch ( Exception e ) {
            setErreur( CHAMP_PASS, e.getMessage() );
        }
        eleve.setPassword(password);

        /* setting connexion result */
        if ( erreurs.isEmpty() ) {
            resultat = "Succès de la connexion.";
        } else {
            resultat = "Échec de la connexion.";
        }

        return eleve;
    }

    /**
     * Valide l'adresse email saisie.
     */
    private void validationEmail( String email ) throws Exception {
         if ( email != null ) {
                if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) {
                    throw new Exception( "Merci de saisir une adresse mail valide." );
                }
            } else {
                throw new Exception( "Merci de saisir une adresse mail." );
            }
    }

    /**
     * check email syntax
     */
    private void validationMotDePasse( String password ) throws Exception {
        if ( password != null ) {
            if ( password.length() < 3 ) {
                throw new Exception( "Le mot de passe doit contenir au moins 3 caractères." );
            }
        } else {
            throw new Exception( "Merci de saisir votre mot de passe." );
        }
    }

    /*
     * getting error with the name of the input
     */
    private void setErreur( String champ, String message ) {
        erreurs.put( champ, message );
    }

    /*
     * getting value from the input
     * 
     */
    private static String getValeurChamp( HttpServletRequest request, String nomChamp ) {
        String valeur = request.getParameter( nomChamp );
        if ( valeur == null || valeur.trim().length() == 0 ) {
            return null;
        } else {
            return valeur;
        }
    }
}

这是控制器部分

package com.isep.connexion.controlleur;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.isep.connexion.model.ConnexionForm;
import com.isep.eleve.model.Eleve;
import com.isep.metier.Demo;
import com.isep.metier.Users;


 // Servlet implementation class Connexion

@WebServlet("/Connexion")
public class ConnexionController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public static final String ATT_USER = "eleve";
    public static final String ATT_FORM = "form";
    public static final String ATT_SESSION_USER = "sessionUser";
    public static final String LOGIN = "/view/connex.jsp";
    public static final String ETUDIANT = "/view/AccueilEtudiant.jsp";
    public static final String TUTEUR = "/view/ProfilTuteur.jsp";



     // @see HttpServlet#HttpServlet()

    public ConnexionController() {
        super();
        // TODO Auto-generated constructor stub
    }


     // @see HttpServlet#doGet(HttpServletRequest request, //HttpServletResponse response)

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());

//      heading the jsp
        this.getServletContext().getRequestDispatcher(LOGIN).forward( request, response );
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ConnexionForm form = new ConnexionForm();
        Users eleve = form.connecterUser( request );
        Demo demo = new Demo();
        HttpSession session = request.getSession();
        request.setAttribute( ATT_FORM, form );
        request.setAttribute( ATT_USER, eleve );

        if ( form.getErreurs().isEmpty() && eleve.getPassword().equals(demo.chargerMDP(eleve.getEmail())) ) {
            session.setAttribute( ATT_SESSION_USER, eleve );
            this.getServletContext().getRequestDispatcher( ETUDIANT ).forward( request, response );
        } else {

            this.getServletContext().getRequestDispatcher( LOGIN ).forward( request, response );
            session.setAttribute( ATT_SESSION_USER, null );
        }



        if ( form.getErreurs().isEmpty() ) {

        } else {

        }
    }
}    

这是我的JSP代码:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/GAPP/ressources/css/lastcss.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<title>Gestion APP</title>
</head>
<body class="connexion">

    <div class="modal-dialog">
        <div class="modal-content modal-md">
            <div class="modal-header">
                <h1 class="text-center ">Connexion</h1>
            </div>
            <div class="modal-body">
                <form class="col-md-12 center-block" method="post"
                    action="connexion">
                    <div class="form-group">
                        <input type="email" name="email" id="email"
                            value="<c:out value="${param.email}"/>"
                            class="form-control input-lg" placeholder="email" required
                            autofocus /><br> <span class="erreur">${form.erreurs['email']}</span>
                    </div>
                    <div class="form-group">
                        <input type="password" name="password"
                            class="form-control input-lg" placeholder="password" required /><br>
                        <span class="erreur">${form.erreurs['password']}</span>
                    </div>
                    <div class="form-group">
                        <input type="submit" class="btn btn-block btn-lg btn-success"
                            value="login"><br>
                        <p class="${empty form.erreurs ? 'succes' : 'erreur'}">${form.resultat}</p>
                        <p>${sessionScope.sessionUser.email}</p> <!-- pour voir que la session est détruite -->
                        <span class="pull-right"><a href="#">Register</a></span> <span><a
                            href="#">Mot de passe oublié</a></span>
                    </div>
                </form>
                <div class="modal-footer">
                    <div class="col-md-12">
                        <button class="btn btn-success">cancel</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

0 个答案:

没有答案