我想在jsp中显示布尔方法以进行电子邮件验证

时间:2019-04-22 05:57:07

标签: java validation email jsp

我获得了包含用户名,密码和电子邮件的登录表单

当我没有包括所有相关的电子邮件部分时,程序将成功运行

但是当我插入电子邮件表格时 我无法显示有效的电子邮件

我有验证方法(Validation.java)

    public class Validation {
private String username;
private String password;
private String email;

public String getUsername() {
    return username;
}
public String getPassword() {
    return password;
}
public String getEmail() {
    return email;
}
public void setUsername(String username){
    this.username = username;
}
public void setPassword(String password) {
    this.password = password;
}
public void setEmai(String email) {
    this.email = email;
}

public String validate (String s1, String s2) {
    if (s1.contains(username) && s2.equals(password))
        return "VALID";
    else
        return "INVALID";

}
public boolean validateEmail() {
    String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + 
            "[a-zA-Z0-9_+&*-]+)*@" +
            "(?:[a-zA-Z0-9]+\\.)+[a-z]" +
            "A-Z]{2,7}$";

    Pattern pat = Pattern.compile(emailRegex);
    return pat.matcher(email).matches();
}

对于response.jsp,这是我的代码

    <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>task 2: login Feedback</title>
</head>
<body style="font-family: Futura; max-width: 600px">
    <jsp:useBean id="validation" class="com.lab5.task2.Validation" scope="request">
        <jsp:setProperty name="validation" property="username" value='<%=request.getParameter("txtUsername")%>'/>
        <jsp:setProperty name="validation" property="password" value='<%=request.getParameter("txtPassword")%>'/>
        <jsp:setProperty name="validation" property="email" value='<%=request.getParameter("txtEmail")%>'/>
    </jsp:useBean>
<fielset>
    <legend>Validation</legend>
    <p>You entered username as <jsp:getProperty name="validation" property="username"/></p>
    <p>You entered password as <jsp:getProperty name="validation" property="password"/></p>
    </br>
    <p>You are a <%=validation.validate("Amirul","12345")%></p>
    <p> Your email: <jsp:getProperty name="validation" property="email"/>
    <%=validation.validateEmail()%>

</fielset>

</body>

我在显示有效电子邮件时遇到问题 我不能使用

    <%=validation.validateEmail("amirul@gmail.com")%>

代码将产生错误 错误说在第18行处理JSP页面/Task2/respond.jsp时发生异常

    <jsp:setProperty name="validation" property="email" value='<%=request.getParameter("txtEmail")%>'/>

1 个答案:

答案 0 :(得分:0)

您可以在此类中添加重载方法

public boolean validateEmail(inputemail) {
    String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + 
            "[a-zA-Z0-9_+&*-]+)*@" +
            "(?:[a-zA-Z0-9]+\\.)+[a-z]" +
            "A-Z]{2,7}$";

    Pattern pat = Pattern.compile(emailRegex);
    return pat.matcher(inputemail).matches();
}

将不带参数的方法更改为

public boolean validateEmail() {
    return validateEmail(this.email);
}

您可以在JSP文件中使用<%=validation.validateEmail("amirul@gmail.com")%>