Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法是POST,但作为GET传递_Java_Http_Servlets - Fatal编程技术网

Java 方法是POST,但作为GET传递

Java 方法是POST,但作为GET传递,java,http,servlets,Java,Http,Servlets,我第一次使用JSP创建了一个非常简单的登录页面,但出现了一个错误。我已经指定表单方法为POST,这意味着数据不会在URL中传递。但是,它会这样做,并且不会调用POST函数。更重要的是,相同的页面会被重新加载,如果我再次输入数据,POST函数就会被调用,它的预期功能也会被执行。我不明白 HTML页面: <html> <head> <meta charset="ISO-8859-1"> <title>Logi

我第一次使用JSP创建了一个非常简单的登录页面,但出现了一个错误。我已经指定表单方法为POST,这意味着数据不会在URL中传递。但是,它会这样做,并且不会调用POST函数。更重要的是,相同的页面会被重新加载,如果我再次输入数据,POST函数就会被调用,它的预期功能也会被执行。我不明白

HTML页面:

<html>
  <head>
    <meta charset="ISO-8859-1">
    <title>Login page</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
  </head>
  <body id="body">

    <div id="main">
      <h1 id="bruh">LOGIN</h1>

      <form action="login" method="POST">
          <input class = "textFields" id="tfUsername" type="text" name="username" placeholder="Username"><br>
          <input class = "textFields" id="tfPassword" type="password" name ="password" placeholder="Password"><br>
          <input type="submit" value="LOGIN">
      </form>

      <div id="bottom">
        <p class="text">Not a member? Sign up <a href="gsdf" class="text">now</a></p>
      </div>
    </div>
  </body>
</html>
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_4_0.xsd" id="WebApp_ID" version="4.0">
   <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>com.jame.loginServlet</servlet-class>
  </servlet>
  <servlet-mapping> 
    <servlet-name>abc</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

abc
com.jame.loginServlet
abc
/登录

修复重定向到JSP页面而不是HTML文件的问题。希望当Servlet接收到您的POST请求时,它会将您重定向到一个JSP页面,以使用用户名欢迎用户

使用以下内容创建JSP页面(home.JSP):

<html>
   <head>
      <title>Your App Name</title>
   </head>
   <body>
      <center>
      <h1>Welcome!</h1>
      
      <ul>
         <li><p><b>Username: </b>
            <%= request.getParameter("username")%>
         </p></li>
      </ul>
   </body>
</html>
致:

<html>
   <head>
      <title>Your App Name</title>
   </head>
   <body>
      <center>
      <h1>Welcome!</h1>
      
      <ul>
         <li><p><b>Username: </b>
            <%= request.getParameter("username")%>
         </p></li>
      </ul>
   </body>
</html>
        if(username.contentEquals("test") && password.contentEquals("test")) {
            res.sendRedirect("login.html");
            System.out.println("login successful");
        }
        if(username.contentEquals("test") && password.contentEquals("test")) {
            res.sendRedirect("home.jsp");
            System.out.println("login successful");
        }