java 在 IntelliJ 中添加一个 servlet,说包 javax.servlet.http 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7939167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a servlet in IntelliJ, says package javax.servlet.http does not exist
提问by Blankman
Just creating my first IntelliJ web application that runs on tomcat.
刚刚创建了我的第一个在 tomcat 上运行的 IntelliJ Web 应用程序。
The project ran fine, and it rendered the index.jsp just fine.
该项目运行良好,它呈现的 index.jsp 也很好。
How come the index.jsp rendered even though the web.xml doesn't have a reference to it btw? Does it first look for psychical files (.jsp's), if present, it executes them? Or is web.xml just for servlets?
顺便说一句,即使 web.xml 没有对 index.jsp 的引用,它如何呈现?它是否首先寻找心理文件(.jsp),如果存在,它会执行它们吗?还是 web.xml 仅用于 servlet?
The real issue was I created a TestServlet in my /src folder, and it can't seem to find the javax.servlet jar:
真正的问题是我在 /src 文件夹中创建了一个 TestServlet,但它似乎找不到 javax.servlet jar:
package javax.servlet.http does not exist
Reference:
参考:
web.xml:
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<display-name>Test1</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
</web-app>
TestServlet.java:
测试Servlet.java:
import java.io.IOException;
package hello_world.Servlets
/**
* Created by IntelliJ IDEA.
* User: snad
* Date: Oct 29, 2011
* Time: 9:19:27 AM
* To change this template use File | Settings | File Templates.
*/
public class TestServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
}
采纳答案by Bozho
- You are missing the servlet-api.jar on your build path.
- jsp's are picked automatically without the need to register them. web.xml is for servlets, filters, listeners and other settings.
- Don't use the default package. Always give a package to your classes. So it better be
test.TestServlet
- 您的构建路径中缺少 servlet-api.jar。
- jsp 是自动选择的,无需注册。web.xml 用于 servlet、过滤器、侦听器和其他设置。
- 不要使用默认包。总是给你的课程一个包。所以最好是
test.TestServlet

