从Servlet传递的文本未显示在页面上

我想创建一个简单的应用程序,该应用程序在页面上显示从servlet传递的数据。这是我的项目结构:

从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,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错误

leijian_113 回答:从Servlet传递的文本未显示在页面上

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3149042.html

大家都在问