软件包javax.servlet不存在的解决办法

软件包javax.servlet不存在的解决办法

问题:
外部导入的一个java类文件,引用了tomcat中的servlet-api.jar文件中的javax.servlet.Filter类 找不到,
其原因是java编译器没有找到软件包javax.servlet.Filter。

package com.sapsoft;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class sproutCorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication");
        filterChain.doFilter(servletRequest,httpServletResponse);
    }

    @Override
    public void destroy() {

    }
}


解决办法:
从tomcat lib目录下拷贝一个servlet-api.jar的包到“JDK\jre\lib\ext”目录下
凡是出现找不到包的情况,都可以将找到的包放到JDK\jre\lib\ext下,

Project面板中/External Libraries/<1.8> jdk 右键弹出执行Open Library settting
弹出Project Structure窗体
Platform Settings/SDKs/Classpath 添加一个引用类路径
d:\cwgis\apps\jdk\jre\lib\ext\servlet-api.jar
然后再编译就能够通过。

展开阅读全文

没有更多推荐了,返回首页