我想创建一个简单的应用程序,该应用程序在页面上显示从servlet传递的数据。这是我的项目结构:
这是Servlet类的代码:
@WebServlet("/")
public class MainServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
System.out.println("in get");
PrintWriter writer = resp.getWriter();
req.setAttribute("title", "My title");
req.setAttribute("body", "My body");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
这是我的jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${title}</title>
</head>
<body>
<h1>${body}</h1>
</body>
</html>
当我在Tomcat上运行该应用程序时,它的网址为http://localhost:8080/Lab6_war_exploded/
,而不是所有示例中的localhost:8080。但这没关系。当我运行该应用程序时,页面上没有任何显示。那怎么了我将不胜感激。预先感谢!
UPD 这是我的web.xml:
<?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>MainServlet</servlet-name>
<servlet-class>servlet.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>-->
</web-app>
如果我从此行中删除注释,则会出现404错误