HTTP 状态 500 - 实例化 servlet 时出错 class LoginServlet
HTTP Status 500 - Error instantiating servlet class LoginServlet
我收到以下错误。
type Exception report
message Error instantiating servlet class LoginServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class LoginServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
root cause
java.lang.ClassNotFoundException: LoginServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
我正在使用注释而不是 web.xml 文件。由于所有答案都是为带有 web.xml 文件的项目提供的。我发现很难找到我的问题的答案。下面给出了我的 login.jsp 页面和 LoginServlet class 代码。
login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>APIS</title>
<link rel="stylesheet" href="public/css/bootstrap.min.css">
<link rel="stylesheet" href="public/css/style.css">
<%@page import= "com.servlet.LoginServlet" %>
</head>
<body>
<ul class="nav navbar-nav">
<li><a href="index.jsp"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li class="active"><a href="login.jsp"><span class="glyphicon glyphicon-lock"></span> Login</a></li>
<%
if (session.getAttribute("agencyname") != null) {
if (session.getAttribute("agencyname").toString().equals("slc")) {
%>
<li><a href="register.jsp"><span class="glyphicon glyphicon-user"></span> New Register</a></li>
<%
}
%>
<li><a href="resetPassword.jsp"><span class="glyphicon glyphicon-refresh"></span> Reset Password</a></li>
<%
}
%>
</ul>
<form class="login-form" action="LoginServlet" method="post">
<h2 class="form-signin-heading" style="margin-left:20px;margin-bottom:30px;">Sign In</h2>
<div class="form-group">
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="UserName" name="UserName">
</div>
<div class="form-group">
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="Password">
</div>
<input type="submit" class="btn btn-success btn-block" value="Submit">
</form>
</body>
<script src="public/js/jquery.min.js"></script>
<script src="public/js/bootstrap.min.js"></script>
</html>
LoginServlet class
package com.servlet;
import com.controller.UserDAO;
import com.model.OgaUser;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LoginServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet LoginServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
System.out.println("******************************");
System.out.println("In doPost method");
String username = request.getParameter("UserName");
String password = request.getParameter("Password");
//OgaUser ou = new OgaUser();
String loginResult = UserDAO.login(username, password);
if (loginResult != null) {
HttpSession session = request.getSession();
if (loginResult.equalsIgnoreCase("slc")) {
session.setAttribute("agency", "slc");
session.setAttribute("username","Harsha");
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("index.jsp");
}
} else {
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
这是一个 ClassNotFoundException 问题。 "java.lang.ClassNotFoundException: LoginServlet"。确保您已将包含 LoginServlet 的 jar 文件放在 class-路径中,该路径通常为 WEB-INF/classes and/or WEB-INF/lib
我收到以下错误。
type Exception report
message Error instantiating servlet class LoginServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class LoginServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
root cause
java.lang.ClassNotFoundException: LoginServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
我正在使用注释而不是 web.xml 文件。由于所有答案都是为带有 web.xml 文件的项目提供的。我发现很难找到我的问题的答案。下面给出了我的 login.jsp 页面和 LoginServlet class 代码。
login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>APIS</title>
<link rel="stylesheet" href="public/css/bootstrap.min.css">
<link rel="stylesheet" href="public/css/style.css">
<%@page import= "com.servlet.LoginServlet" %>
</head>
<body>
<ul class="nav navbar-nav">
<li><a href="index.jsp"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li class="active"><a href="login.jsp"><span class="glyphicon glyphicon-lock"></span> Login</a></li>
<%
if (session.getAttribute("agencyname") != null) {
if (session.getAttribute("agencyname").toString().equals("slc")) {
%>
<li><a href="register.jsp"><span class="glyphicon glyphicon-user"></span> New Register</a></li>
<%
}
%>
<li><a href="resetPassword.jsp"><span class="glyphicon glyphicon-refresh"></span> Reset Password</a></li>
<%
}
%>
</ul>
<form class="login-form" action="LoginServlet" method="post">
<h2 class="form-signin-heading" style="margin-left:20px;margin-bottom:30px;">Sign In</h2>
<div class="form-group">
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="UserName" name="UserName">
</div>
<div class="form-group">
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="Password">
</div>
<input type="submit" class="btn btn-success btn-block" value="Submit">
</form>
</body>
<script src="public/js/jquery.min.js"></script>
<script src="public/js/bootstrap.min.js"></script>
</html>
LoginServlet class
package com.servlet;
import com.controller.UserDAO;
import com.model.OgaUser;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LoginServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet LoginServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
System.out.println("******************************");
System.out.println("In doPost method");
String username = request.getParameter("UserName");
String password = request.getParameter("Password");
//OgaUser ou = new OgaUser();
String loginResult = UserDAO.login(username, password);
if (loginResult != null) {
HttpSession session = request.getSession();
if (loginResult.equalsIgnoreCase("slc")) {
session.setAttribute("agency", "slc");
session.setAttribute("username","Harsha");
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("index.jsp");
}
} else {
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
这是一个 ClassNotFoundException 问题。 "java.lang.ClassNotFoundException: LoginServlet"。确保您已将包含 LoginServlet 的 jar 文件放在 class-路径中,该路径通常为 WEB-INF/classes and/or WEB-INF/lib