【问题标题】:Java servlet works on its own but not when requested from web page [duplicate]Java servlet 独立工作,但在从网页请求时不工作 [重复]
【发布时间】:2026-01-06 07:05:02
【问题描述】:

服务器是 Glassfish

找不到错误 404 页面或您未连接到网络

网页代码

<html>
<head>
    <title>Wow</title>        
</head>
<body>
    <form  name ="wow" method="post" action="ThatWas">
        Name : <input type="text" name="name">                      
        <input type="submit" value="That">                        
    </form>        
</body>
</html>

Servlet 的代码是

public class ThatWas extends HttpServlet {    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
    String name = request.getParameter("name");         
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ThatWas</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println(" That was good "+ name + request.getContextPath());
        out.println("</body>");
        out.println("</html>");
    }
}

servlet 运行时

结果是

这很好 + 文件路径

点击网页按钮时

结果是

您没有连接到网络

结果应该是 这很好 + 名称 + 文件路径

【问题讨论】:

  • servlet中还有其他方法吗?
  • 提到的那些

标签: java servlets


【解决方案1】:

您在表单中提到该方法为“post”,但您在servlet 中的方法不是doPost。我认为这就是错误。方法名称应为doPost。此外,您必须使用 web.xml 中的 servlet 类映射操作。

【讨论】:

  • doPost 没有这样做
  • 向servlet发送参数的网页没有返回结果,而是404显示它已经部署并且web xml有详细信息