简体   繁体   English

JSP,Java和数据库。 如果尝试登录时提交了错误的数据,如何再次调用同一index.jsp?

[英]JSP, Java and Database. How to invoke the same index.jsp one more time if incorrect data were submitted while trying to login?

I can connect to database but I am not able to reinvoke the same index.jsp file if incorrect data are passed while trying to login. 我可以连接到数据库,但是如果尝试登录时传递了错误的数据,则无法重新调用相同的index.jsp文件。

After I connect to database the index.jsp file is displayed and I put email and password with this data: a.smith@gmail.com 1234. These data are in my database so it redirect me to the menu.jsp page but also if I put incorrect data: a.sm@gmail.com 12 it also redirects me to menu.jsp.Now I get this error: 连接到数据库后,将显示index.jsp文件,并在电子邮件和密码中输入以下数据:a.smith@gmail.com1234。这些数据在我的数据库中,因此它将我重定向到menu.jsp页面,但如果我输入了不正确的数据:a.sm@gmail.com 12它也将我重定向到menu.jsp。现在我收到此错误:


Mar 30, 2014 12:14:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Cap] threw exception [/index.jsp     
(line: 33, column: 41) quote symbol expected] with root cause
org.apache.jasper.JasperException: /index.jsp (line: 33, column: 41) quote symbol expected
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
    ..........

Line 33 in index.jsp is index.jsp中的第33行是

 <jsp:getProperty name="login" property="value" />

getEmail and getPasword not return null only good value as expected. getEmail和getPasword不返回null,仅返回期望值。


    <jsp:useBean id="member" class="StaffMember" scope="session"/>
    <jsp:setProperty name="member" property="*"/>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Welcome</title>
    </head>
    <body bgcolor="lightblue">
    <font size=4>

    <center>
    </center>
    <form method=post> <hr><br>
    <center>
    <b>LOGIN</b><p>
    <input type=text name=email value= email value = member.setEmail(email)> <p>
    <input type=text name=password value=password value = member.setPassword(password)> <p>
    <input type=submit value="Submit">
    <jsp:getProperty name="login" property="value" />
 <c:if value = "false" action="index.jsp"></c:if>
 <c:if value = "true" action="menu.jsp"></c:if>
    </center>
    <img src="<%=request.getContextPath()%>/images/ndnuLogo.jpg"/>
    <br><hr>
    </form>
    </font>  
    </body>
    </html>


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.FormParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.view.Viewable;

    public class LoginServiceImplem implements LoginService{
    private Connection connection;
    private Statement statement;
    private boolean value; //used to check if user that login is on the list of user available to 
                          //test

    public LoginServiceImplem() throws SQLException{
            connection = null;
            statement = null;
        value = false;
        StaffMember sm = new StaffMember();
        value = login(sm.getEmail(), sm.getPassword());
    }

    public boolean getValue() {
    return value;
    }

public void setValue(boolean value) {
        this.value = value;
    }

public boolean login(String email, String password) throws SQLException{
        System.out.println(email + " " + password);
        String query = "SELECT * FROM cap.member WHERE Email= '" + email + "'"
                + " AND Password = '" + password + "'";
        ResultSet rs = null;
        try {
            try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
            //builds a connection and statement
            connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/" + 
            "cap", "root", "");
            System.out.println("After connection");
            statement = connection.createStatement(); 
            System.out.println("After statement");
            rs = statement.executeQuery(query);
            System.out.println("After rs");
            //if the statement returns something
            if(rs.next()){
                //and email and password match the database information, return true
                return(email.equals(rs.getString("Email")) 
                        && password.equals(rs.getString("Password")));
            }
        }finally {
            //close all connections
            DbUtil.close(rs);
            DbUtil.close(statement);
            DbUtil.close(connection);
        }return false;
    }
}


public class StaffMember {

    private String emailAddress;
    private String password;

    public StaffMember() {  
        emailAddress = "a.smith@gmail.com";
        password = "1234";  
    }

    /* The setEmail method sets the employee's email */
    public void setEmail(String email) {
    emailAddress = email;
    }

    public void setPassword(String pword){
        password = pword;
    }

    /* The getEmail method gets the employee's email. */
    public String getEmail() {
        return emailAddress;
    }    

    public String getPassword(){
        return password;
    }
}

在此处输入图片说明

If I have not missed anything I think the error "No suitable driver found" comes because of not having the below line in Java code. 如果我什么都没错过,我认为会出现错误“找不到合适的驱动程序”,因为Java代码中没有以下行。 Let me know if I am missing anything. 让我知道我是否想念任何东西。

 Class.forName("com.mysql.jdbc.Driver");

you can add this in "LoginServiceImplem" class. 您可以在“ LoginServiceImplem”类中添加它。 in a static block something like 在静态块中

static {
    Class.forName("com.mysql.jdbc.Driver");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM