【问题标题】:404 when servlet is invoked but works directly when url to servlet is used from browser调用 servlet 时为 404,但当从浏览器使用 servlet 的 url 时直接工作
【发布时间】:2019-07-20 16:55:55
【问题描述】:

问题:当我直接在浏览器中调用 url(url-mapping)时效果很好,但是当我使用 post 方法从 jsp 文件中调用 servlet 时,它不起作用但给出一个错误:

类型状态报告

消息 /HelloWorld/myservlet

描述 源服务器没有找到当前的表示 为目标资源或不愿意透露存在。

Jsp页面

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Hello World</title>
    </head>
    <body>
        <form method="post"  action="myservlet">
            <input type="submit" value ="send">
        </form>
    </body>
    </html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 id="WebApp_ID" version="3.0">
    <display-name>helloworld2</display-name>
    <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>apress.helloworld.HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Servlet 代码:

    package apress.helloworld;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class HelloWorld extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        //System.out.println("Get Method Called");

          try { response.setContentType("text/html");
        `enter code here`  PrintWriter printwriter = response.getWriter(); 
          printwriter.println("<h2>");
          printwriter.println("Hello World");
          printwriter.println("</h2>"); 
          } 
          catch
          (IOException ex) 
          { ex.printStackTrace(); }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub

          try { resp.setContentType("text/html");
          PrintWriter printwriter = resp.getWriter(); 
          printwriter.println("<h2>");
          printwriter.println("Hello World");
          printwriter.println("</h2>"); 
          } 
          catch
          (IOException ex) 
          { ex.printStackTrace(); }


        }



   }

【问题讨论】:

  • 你需要传递 'hello' 而不是 'mysevlet' 因为你已经给出了 /hello
  • 您从浏览器传递的网址是什么?
  • Sanjay,我正在使用localhost:8080/HelloWorld/hello,但是当我使用表单上的按钮调用servelet 时,它导航到localhost:8080/HelloWorld/myservlet,这不起作用。 web.xml 将 myservlet 映射到 Servlet 类,该类是包 'apress.helloworld.HelloWorld' 类下的 servlet,其 doGet 方法被执行,因为 action 指向该 servlet 但它不起作用。
  • 将您的 action="myservlet" 更改为 action="/hello"
  • 在表单中传递 action="hello" 而不是 action="myservlet"。

标签: java jsp servlets


【解决方案1】:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Hello World</title>
    </head>
    <body>
        <form method="post"  action="hello">
            <input type="submit" value ="send">
        </form>
    </body>
    </html>

使用上面的代码 在 HTML 表单中 action="url-mapping" 你必须提到 servlet 的 url 模式而不是 servlet 名称参考你的 web.xml

【讨论】:

    猜你喜欢
    • 2013-12-20
    • 2016-12-28
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2013-12-25
    • 2014-12-10
    相关资源
    最近更新 更多