【问题标题】:Call JSP from servlet [duplicate]从 servlet 调用 JSP [重复]
【发布时间】:2021-06-29 06:25:52
【问题描述】:

错误->

HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET 输入状态报告

此 URL 不支持消息 HTTP 方法 GET

说明 请求的资源不允许使用指定的 HTTP 方法。

web.xml

<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   version="3.0">

   <display-name>To do List</display-name>

   <welcome-file-list>
       <welcome-file>login.do</welcome-file>
   </welcome-file-list>

</web-app>

LoginServlet.java

package com.todo;

//imports ->
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// url ->
@WebServlet(urlPatterns = "/login.do")

// response genrator ->
public class LoginServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {{
        // JSP code response ->
       
        RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/WEB-INF/veiws/login.jsp");
        RequetsDispatcherObj.forward(request, response);   }
}
}

login.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 from jsp</title>
    </head>
    <body>
        <p>
            Hello world from jsp! 
        </p>
    </body>
</html>

【问题讨论】:

  • 代码块很好。但请尝试添加您尝试过的和无效的。

标签: java xml jsp servlets


【解决方案1】:

我认为您应该为 GET 请求创建一个方法 doGet(...)。 如果 GET 和 POST 都做同样的事情,您可以创建另一个方法并从 doPost 和 doGet 调用它。

祝你有美好的一天

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 2021-06-24
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多