javax.servlet.ServletException:过滤器执行引发异常 - java.lang.ExceptionInInitializerError
我正在为学校做一个用户管理 Web 应用程序项目,当我部署到 Tomcat 时,出现此错误。
其他:在web.xml
上,当我尝试添加
时,Intellij无法识别UserFilter
类。我有一个名为 UserFilter 的类,具有正确的路径。
HTTP Status 500 - Filter execution threw an exception
type Exception report
message Filter execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Filter execution threw an exception
root cause
java.lang.ExceptionInInitializerError
teste.web.SecurityFilter.openSession(SecurityFilter.java:26)
teste.web.SecurityFilter.doFilter(SecurityFilter.java:19)
root cause
java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:434)
java.util.Properties.load0(Properties.java:353)
java.util.Properties.load(Properties.java:341)
teste.pluginSession.PluginFactory.<clinit>(PluginFactory.java:12)
teste.web.SecurityFilter.openSession(SecurityFilter.java:26)
teste.web.SecurityFilter.doFilter(SecurityFilter.java:19)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M6 logs.
Apache Tomcat/9.0.0.M6
SecurityFilter.java
package teste.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import teste.pluginSession.*;
public class SecurityFilter implements Filter {
@Override
public void init (FilterConfig filterConfig) throws ServletException{
}
@Override
public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
HttpServletResponse response = (HttpServletResponse) servletResponse;
openSession (servletRequest,filterChain, response);
}
private void openSession(ServletRequest servletRequest, FilterChain filterChain, HttpServletResponse response) throws IOException, ServletException{
RegisterSession plugin = null;
try {
plugin = (RegisterSession) PluginFactory.getPlugin(RegisterSession.class);
plugin.openSession(servletRequest,filterChain,response);
} catch (ClassNotFoundException e){
e.printStackTrace();
}catch (IllegalAccessException e){
e.printStackTrace();
}catch (InstantiationException e){
e.printStackTrace();
}
}
@Override
public void destroy() {
}
}
PluginFactory
package teste.pluginSession;
import java.io.IOException;
import java.util.Properties;
public class PluginFactory
{
static Properties properties = new Properties();
static
{
try {
properties.load(PluginFactory.class.getResourceAsStream("/plugins.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object getPlugin(Class iface) throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
String classNameVerdadeiro = properties.getProperty(iface.getName());
return Class.forName(classNameVerdadeiro).newInstance();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>teste.web.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>teste.web.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>teste.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>teste.web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>teste.web.RestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/soa</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/errors/erro404.jsp</location>
</error-page>
<!--START FILES-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="teste.web.LoginServlet" %>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Login</title>
<style>
.login-form {
border: hidden;
padding: 20px;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.form-group{
padding-left:40px;
padding-right:40px;
}
h3{
text-align: center;
}
body{
background-color: darkslateblue;
}
</style>
</head>
<body>
<div class="container" style="text-align:center;color:white;transform: translate(0%, 400%);">
<h2><strong>Soft</strong>Ware</h2>
</div>
<div class="container login-form">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal" action="<%=request.getContextPath()%>/login" method="POST">
<div class="input-group" style="padding-bottom: 10px;">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" class="form-control" placeholder="Username" type="text" name="username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="password" class="form-control" placeholder="Password" type="password" name="password">
</div>
<div class="form-group" style="text-align: center;padding-top: 10px;">
<button type="submit" class="btn btn-success" onclick=""><span class="glyphicon glyphicon-ok"></span> Submeter</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
非常感谢!
I'm doing a project of user management web application for school and when I deploy to Tomcat I got this error.
Additional: On web.xml
Intellij cannot recognize class UserFilter
when I try to add <filter>
. I have a class called UserFilter with the right path.
HTTP Status 500 - Filter execution threw an exception
type Exception report
message Filter execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Filter execution threw an exception
root cause
java.lang.ExceptionInInitializerError
teste.web.SecurityFilter.openSession(SecurityFilter.java:26)
teste.web.SecurityFilter.doFilter(SecurityFilter.java:19)
root cause
java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:434)
java.util.Properties.load0(Properties.java:353)
java.util.Properties.load(Properties.java:341)
teste.pluginSession.PluginFactory.<clinit>(PluginFactory.java:12)
teste.web.SecurityFilter.openSession(SecurityFilter.java:26)
teste.web.SecurityFilter.doFilter(SecurityFilter.java:19)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M6 logs.
Apache Tomcat/9.0.0.M6
SecurityFilter.java
package teste.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import teste.pluginSession.*;
public class SecurityFilter implements Filter {
@Override
public void init (FilterConfig filterConfig) throws ServletException{
}
@Override
public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
HttpServletResponse response = (HttpServletResponse) servletResponse;
openSession (servletRequest,filterChain, response);
}
private void openSession(ServletRequest servletRequest, FilterChain filterChain, HttpServletResponse response) throws IOException, ServletException{
RegisterSession plugin = null;
try {
plugin = (RegisterSession) PluginFactory.getPlugin(RegisterSession.class);
plugin.openSession(servletRequest,filterChain,response);
} catch (ClassNotFoundException e){
e.printStackTrace();
}catch (IllegalAccessException e){
e.printStackTrace();
}catch (InstantiationException e){
e.printStackTrace();
}
}
@Override
public void destroy() {
}
}
PluginFactory
package teste.pluginSession;
import java.io.IOException;
import java.util.Properties;
public class PluginFactory
{
static Properties properties = new Properties();
static
{
try {
properties.load(PluginFactory.class.getResourceAsStream("/plugins.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object getPlugin(Class iface) throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
String classNameVerdadeiro = properties.getProperty(iface.getName());
return Class.forName(classNameVerdadeiro).newInstance();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>teste.web.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>teste.web.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>teste.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>teste.web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>teste.web.RestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/soa</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/errors/erro404.jsp</location>
</error-page>
<!--START FILES-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="teste.web.LoginServlet" %>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Login</title>
<style>
.login-form {
border: hidden;
padding: 20px;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.form-group{
padding-left:40px;
padding-right:40px;
}
h3{
text-align: center;
}
body{
background-color: darkslateblue;
}
</style>
</head>
<body>
<div class="container" style="text-align:center;color:white;transform: translate(0%, 400%);">
<h2><strong>Soft</strong>Ware</h2>
</div>
<div class="container login-form">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal" action="<%=request.getContextPath()%>/login" method="POST">
<div class="input-group" style="padding-bottom: 10px;">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" class="form-control" placeholder="Username" type="text" name="username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="password" class="form-control" placeholder="Password" type="password" name="password">
</div>
<div class="form-group" style="text-align: center;padding-top: 10px;">
<button type="submit" class="btn btn-success" onclick=""><span class="glyphicon glyphicon-ok"></span> Submeter</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论