简体   繁体   English

尝试读取 JSP 时出现错误(500 内部服务器错误)

[英]Error (500 Internal Server Error) while attempting to read JSP

I am adding the ability for users to log on to my website.我正在添加用户登录我的网站的功能。 I am experiencing some difficulties developing the user interface for the log on page.我在开发登录页面的用户界面时遇到了一些困难。 I received the error我收到了错误

Error (500 Internal Server Error) while attempting to read 'webAPIs/logonAPI.jsp?email=user1@gmail.com&password=123'

while I entered in the correct credentials for a user in my database.当我为数据库中的用户输入正确的凭据时。 When I ran logonAPI.jsp locally on my browser and added the credentials into the URL, it displayed the correct information.当我在浏览器上本地运行 logonAPI.jsp 并将凭据添加到 URL 中时,它显示了正确的信息。 However, it is not working for my UI.但是,它不适用于我的 UI。

Here is my code.这是我的代码。

The UI page:用户界面页面:

var logon = {};

logon.UI = function (id) {
    var content = `
        <div class='logon'>
            <br/>
            Email Address: <input type="text" id="email"/>
            &nbsp;
            Password: <input type="password" id="password"/>
            &nbsp;
            <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.";
        }
    }
}

The Log On API:登录 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());
%>

The DbMods class DbMods class

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

And here is the error message that is displayed in the console when I click "Submit" after inputting the correct credentials这是输入正确凭据后单击“提交”时控制台中显示的错误消息

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)

You need to import class using static:您需要使用 static 导入 class:

<%@page import="static model.webUser.DbMods"%>

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

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