gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:05:45 25 4
gpt4 key购买 nike

我可以连接到数据库,但如果尝试登录时传递了不正确的数据,则无法重新调用相同的 index.jsp 文件。

连接到数据库后,将显示 index.jsp 文件,并使用以下数据输入电子邮件和密码:a.smith@gmail.com 1234。这些数据位于我的数据库中,因此它将我重定向到 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)
..........

index.jsp 中的第 33 行是

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

getEmail 和 getPassword 不返回 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;
}
}

enter image description here

最佳答案

如果我没有错过任何内容,我认为错误“找不到合适的驱动程序”是因为 Java 代码中没有以下行。如果我遗漏了什么,请告诉我。

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

您可以将其添加到“LoginServiceImplem”类中。在静态 block 中,例如

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

关于java - JSP、Java 和数据库。如果尝试登录时提交了错误的数据,如何再次调用同一个index.jsp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com