我正在添加用户登录我的网站的功能。我在开发登录页面的用户界面时遇到一些困难。我收到错误
Error (500 Internal Server Error) while attempting to read 'webAPIs/logonAPI.jsp?email=user1@gmail.com&password=123'
当我在数据库中输入正确的用户凭据时。当我在浏览器本地运行 logonAPI.jsp 并将凭据添加到 URL 中时,它显示了正确的信息。但是,它不适用于我的用户界面。
这是我的代码。
用户界面页面:
var logon = {};
logon.UI = function (id) {
var content = `
<div class='logon'>
<br/>
Email Address: <input type="text" id="email"/>
Password: <input type="password" id="password"/>
<input type="button" value="Enter" onclick="logon.findById('email', 'password', 'msgArea')"/>
<br/> <br/>
<div id="msgArea"></div>
</div>
`;
document.getElementById(id).innerHTML = content;
}
logon.findById = function (email, password, targetId) {
var contentDOM = document.getElementById(targetId);
contentDOM.innerHTML = "";
var logonEmail = escape(document.getElementById(email).value);
var logonPassword = escape(document.getElementById(password).value);
var logonUrl = "webAPIs/logonAPI.jsp?email=" + logonEmail + "&password=" + logonPassword;
ajax2({
url: logonUrl,
successFn: success,
errorId: targetId
});
function success(obj) {
if (obj.webUserList[0] === null) {
contentDOM.innerHTML = "Email + Password incorrect.";
}
else {
contentDOM.innerHTML = "You have logged on.";
}
}
}
登录 API:
<%@page contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page language="java" import="dbUtils.*" %>
<%@page language="java" import="model.webUser.*" %>
<%@page language="java" import="view.WebUserView" %>
<%@page language="java" import="com.google.gson.*" %>
<%
// default constructor creates nice empty StringDataList with all fields "" (empty string, nothing null).
StringData list = new StringData();
String email = request.getParameter("email");
String password = request.getParameter("password");
if (email == null || password == null) {
list.errorMsg = "Cannot search for user - 'email & password' must be supplied";
}
else {
DbConn dbc = new DbConn();
list.errorMsg = dbc.getErr(); // returns "" if connection is good, else db error msg.
if (list.errorMsg.length() == 0) { // if got good DB connection,
System.out.println("*** Ready to call logonAPI");
list = DbMods.logonFind(email, password, dbc);
if(list == null){
}
else{
session.setAttribute("webUser", list);
}
}
dbc.close(); // EVERY code path that opens a db connection, must also close it - no DB Conn leaks.
}
// This object (from the GSON library) can to convert between JSON <-> POJO (plain old java object)
Gson gson = new Gson();
out.print(gson.toJson(list).trim());
%>
DbMods 类
package model.webUser;
import dbUtils.DbConn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DbMods {
public static StringDataList findById (DbConn dbc, String id) {
StringDataList sdl = new StringDataList();
try {
String sql = "SELECT web_user_id, user_email, user_password, membership_fee, birthday, image, "
+ "web_user.user_role_id, user_role_type "
+ "FROM web_user, user_role WHERE web_user.user_role_id = user_role.user_role_id "
+ "AND web_user_id = ?";
PreparedStatement stmt = dbc.getConn().prepareStatement(sql);
// Encode the id (that the user typed in) into the select statement, into the first
// (and only) ?
stmt.setString(1, id);
ResultSet results = stmt.executeQuery();
if (results.next()) { // id is unique, one or zero records expected in result set
sdl.add(results);
}
results.close();
stmt.close();
} catch (Exception e) {
StringData sd = new StringData();
sd.errorMsg = "Exception thrown in WebUserView.getUserById(): " + e.getMessage();
sdl.add(sd);
}
return sdl;
} // getUserById
public static StringData logonFind(String email, String pw, DbConn dbc) {
StringData foundData = new StringData();
if ((email == null) || (pw == null)) {
foundData.errorMsg = "Programmer error in model.webUser.DbMods.logonFind: email and pw must be both non-null.";
return foundData;
}
try {
String sql = "SELECT web_user_id, user_email, user_password, membership_fee, birthday, image,"
+ "web_user.user_role_id, user_role_type "
+ "FROM web_user, user_role "
+ "WHERE web_user.user_role_id = user_role.user_role_id "
+ "AND user_email = ? and user_password = ? ";
PreparedStatement pStatement = dbc.getConn().prepareStatement(sql); // this compiles the SQL
// Encode user supplied values into the ?s of the prepared statement.
pStatement.setString(1, email); // replace 1st question mark
pStatement.setString(2, pw); // replace 2nd question mark
ResultSet results = pStatement.executeQuery(); // Get the result set - expecting 1 or 0 records,
// because user_email must be unique within the table.
if (results.next()) {
// Record found in database, credentials are good.
return new StringData(results);
} else {
// Returning null means that the username / pw were not found in the database
return null;
}
} catch (Exception e) {
foundData.errorMsg = "Exception in model.webUser.DbMods.logonFind(): " + e.getMessage();
System.out.println("******" + foundData.errorMsg);
return foundData;
}
} // logonFind
} // class
这是输入正确的凭据后单击“提交”时控制台中显示的错误消息
HTTP Status 500 - Unable to compile class for JSP:
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 27 in the jsp file: /test6/webAPIs/logonAPI.jsp
DbMods cannot be resolved
24: if (list.errorMsg.length() == 0) { // if got good DB connection,
25:
26: System.out.println("*** Ready to call logonAPI");
27: list = DbMods.logonFind(email, password, dbc);
28: if(list == null){
29: }
30: else{
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
最佳答案
您需要使用静态导入类:
<%@page import="static model.webUser.DbMods"%>
关于javascript - 尝试读取 JSP 时出错(500 内部服务器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425398/