为什么servlet不显示它的html页面?

chy5wohz  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(487)

我创建了一个servlet,它接受两个数字并返回总和。该项目是一个名为“webadd”的动态web项目。servlet名称是addservlet。url模式是/add。包名是org.koushik.javabrains。类名是addservlet。html页面是add.html。
这是我的web.xml页面:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>AddWeb</display-name>

  <servlet>
    <servlet-name>addServlet</servlet-name>
    <servlet-class>org.koushik.javabrains.AddServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>addServlet</servlet-name>
    <url-pattern>/add</url-pattern>
  </servlet-mapping>

</web-app>

这是addservlet.java:

public class AddServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String num1 = request.getParameter("num1");
        String num2 = request.getParameter("num2");

        int result = (Integer.parseInt(num1) + Integer.parseInt(num2));

        response.getWriter().println("Result is " + result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String num1 = request.getParameter("num1");
        String num2 = request.getParameter("num2");

        int result = (Integer.parseInt(num1) + Integer.parseInt(num2));

        response.getWriter().println("Result is " + result);
    }
}

同样,这是add.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

</head>
<body>

<form method="post" action="addServlet">
  First number: <input name="num1" />
  Second number: <input name="num2" />
  <input type="submit" />
</form>

</body>
</html>

我的问题是当我打字的时候 localhost:8080/WebAdd/add 我得到一个空值错误。
我打字的时候 localhost:8080/WebAdd/add?num1=10&num2=15 我得到“结果是25”。
问题是它从不直接请求用户输入,因为它从不显示add.html。
而且,我打字的时候 localhost:8080/WebAdd/add.html 我犯了个错误 404 .
如何显示add.html页面?

flmtquvp

flmtquvp1#

在web.xml中,有这样的Map并使用localhost:8080/webadd/addpage 作为url

<servlet>
    <servlet-name>addpage</servlet-name>
    <jsp-file>/add.html</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>addpage</servlet-name>
    <url-pattern>/addpage</url-pattern>
</servlet-mapping>

另外,确保add.html直接位于您的上下文根目录或创建的war文件中。同样,在add.html中,have action有“add”而不是“addservlet”

nhaq1z21

nhaq1z212#

除非将web服务器配置为提供静态文件,否则无法获取html文件。它没有代码。
你可以试着把这个加到你的 web.xml 能过得去吗 localhost:8080/WebAdd :

<welcome-file-list>  
   <welcome-file>add.html</welcome-file>  
</welcome-file-list>

或者你可以改变主意 doGet 例如:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

     // "/" is relative to the context root (your web-app name)
     // don't add your web-app name to the path
     RequestDispatcher view = req.getRequestDispatcher("/add.html"); // here you need to provide correct path to your file, I don't know it so leaving just /add.html, maybe that will work
     view.forward(req, resp);    
}

在这种情况下 GET localhost:8080/WebAdd/add 应该返回页面。

相关问题