URL localhost:8080/<project_name>/Servlet 不工作
URL localhost:8080/<project_name>/Servlet don't works
我正在尝试 运行 我的简单 servlet "Hello"。
我已经安装了 tomcat 9.0.6
比我在 Idea(称为测试)中创建一个新的 JavaEE Web 项目...在我在 src(称为 servlet)中创建新包之后,在新的 servlet 文件 "myServlet".
之后
Project Structure
这是代码:
public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Hello my first Servlet</h2>");
out.println("<br/>");
out.println("Time on the server is: " + new java.util.Date());
out.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
然后我打开 web.xml 并为 servlet 和 servlet 映射添加下一行:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>servlet.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
在成功 运行ning tomcat ("Artifact is deployed successfully") 之后我尝试访问 url: http://localhost:8080/test/myServlet
Result was page 404...
But if i change url to: http://localhost:8080/myServlet
结果是正确的。
Result
url 怎么了:http://localhost:8080/test/myServlet ???
需要一些图书馆还是什么???
我的错误在哪里???
我还尝试在 web.xml 中使用 @WebServlet("/myServlet") 注释而不使用 servlet 和 servlet 映射行 - 结果相同。
您需要在 context.xml 中定义上下文路径 - 请参阅此 link
http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_context
我正在尝试 运行 我的简单 servlet "Hello"。
我已经安装了 tomcat 9.0.6
比我在 Idea(称为测试)中创建一个新的 JavaEE Web 项目...在我在 src(称为 servlet)中创建新包之后,在新的 servlet 文件 "myServlet".
之后
Project Structure
这是代码:
public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Hello my first Servlet</h2>");
out.println("<br/>");
out.println("Time on the server is: " + new java.util.Date());
out.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
然后我打开 web.xml 并为 servlet 和 servlet 映射添加下一行:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>servlet.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
在成功 运行ning tomcat ("Artifact is deployed successfully") 之后我尝试访问 url: http://localhost:8080/test/myServlet
Result was page 404...
But if i change url to: http://localhost:8080/myServlet
结果是正确的。
Result
url 怎么了:http://localhost:8080/test/myServlet ???
需要一些图书馆还是什么???
我的错误在哪里???
我还尝试在 web.xml 中使用 @WebServlet("/myServlet") 注释而不使用 servlet 和 servlet 映射行 - 结果相同。
您需要在 context.xml 中定义上下文路径 - 请参阅此 link http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_context