JSP文件中的doGet有效,但doPost不起作用

问题描述 投票:0回答:2

我是Java的新手,因此我尝试构建一个简单的Web服务。基本上,用户应该将数据发送到服务器。然后服务器发送另一组要显示给用户。

如果我想使用POST方法发送数据,则会收到错误404。但是我可以运行GET请求,但POST无法正常工作。我尝试了很多版本。我感到困惑的是,在互联网上有不止一种可能的解决方案。

[如果有人能给我提示,我做错了,我会很高兴。

我的servlet文件.java

@WebServlet("/HomeServlet") 
public class HomeServlet extends HttpServlet {  
private static final long serialVersionUID = 1L;    
RequestDao req = new RequestDao();  
ResultDao res = new ResultDao();    
int month = 0;  
int year = 0;   
double hours = 0;   
private ResultDao execute;  
private CalcMonthFlow calcMonthFlow;
public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

@Override
public void init() throws ServletException {
    System.out.println("Servlet " + this.getServletName() + " has started");
}
@Override
public void destroy() {
    System.out.println("Servlet " + this.getServletName() + " has stopped");
}

/**
 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
 */
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    super.service(request, response); 
}


/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            response.getWriter().println("AHOJ");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String monthInput = request.getParameter("month");
    String yearInput = request.getParameter("year");
    String hoursInput = request.getParameter("hours");

    response.getWriter().println(monthInput);
    response.getWriter().println(yearInput);
    response.getWriter().println(hoursInput);

}

}

我的JSP文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.time.LocalDateTime" %>
<!DOCTYPE html>
<html>
    <head>
        <title>Mandays</title>

        <!-- responzivní web -->
        <meta name="viewport" content="width=device-width,initial-scale=1">

        <!-- CDN Bootstrap -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

        <!-- JS -->
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>

        <!-- jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

        <script>
            $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();   
            });
            </script>
    </head>
    <body class="text-monospace">
        <!-- Navigační lišta -->
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="#">Mandays</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                  <li class="nav-item">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="#">API</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="#">Wiki</a>
                  </li>
                </ul>
            </div>
        </nav>

    <div class="jumbotron"> 
                <p>Kalkulačka spočítá počet dnů a hodin v zadaném měsíci a roce včetně a bez svátků.</p>
            </div>

    <!-- Začátek kontejneru sloupců -->
    <div class="container">
        <!-- první řádka -->
        <div class="row">
            <!-- Level prázdnej panel -->
            <div class="col"></div>

            <!-- prostření obsahovej panel -->
            <div class="col-6">
            <form action="/submit" method="POST">
                <div class="input-group mb-3 input-group-sm">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">Měsíc</span>
                    </div>
                    <input type="text" class="form-control" name="month" value="${data.month}" placeholder="05" aria-label="Month" aria-describedby="basic-addon1">
                </div>

                <div class="input-group mb-3 input-group-sm">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">Rok</span>
                    </div>
                    <input type="text" class="form-control" name="year" value="${data.year}" placeholder="2019" aria-label="Year" aria-describedby="basic-addon1">
                </div>

                <div class="input-group mb-3 input-group-sm">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1" >Hodin</span>
                    </div>
                    <input type="text" class="form-control" name="hours" value="${data.hours}" placeholder="7.5" aria-label="Hours" aria-describedby="basic-addon1" data-toggle="tooltip" data-placement="right" title="Použijte desetinnou tečku.">
                </div>
                    <input type="submit" class="btn btn-primary btn-sm" value="Spočítej">   

                </form>
                </div>
                <!-- Level prázdnej panel -->
                <div class="col"></div>
            </div>
        </div>
    </body>

还有我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MandaysFigu</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>HomeServlet</servlet-name>
    <servlet-class>present.HomeServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>HomeServlet</servlet-name>
    <url-pattern>/submit</url-pattern>
</servlet-mapping>

谢谢!

java http jsp post
2个回答
1
投票

<form>的操作属性应包括Web应用程序的上下文路径,或使用相对路径(如果JSP位于Web应用程序的根目录中,则可以使用action =“ submit”)。


0
投票

[我认为您在JSP文件中的<form action="/submit" method="POST">中,由于Servlet文件中的action="/HomeServlet",您需要替换@WebServlet("/HomeServlet")

© www.soinside.com 2019 - 2024. All rights reserved.