Java Servlet表单发布问题:getParameters()返回空值

问题描述 投票:1回答:1

我是新来的,不是以英语为母语的人:)。因此,我编写了一种表单,该表单通过java servlet中的post方法发送简单值。最初,它与request.getParameter()一起使用,我不知道自己做了什么,但是它不再起作用了。

package servlets;

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;

//import services.*;

//import org.json.JSONException;
//import org.json.JSONObject;

public class Authentification extends HttpServlet{

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        writer.println("<h1>Bienvenue sur notre nouveau site WEB</h1>");
        writer.println("<body>");

        //Form
        writer.println("<form action="+"Authentification"+" method="+"post"+" class="+"form"+">");

        //login
        writer.println("<div class="+">");
        writer.println("<label for="+"login"+">Login:");
        writer.println("<input type="+"text"+" name="+"login"+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //password
        writer.println("<div class="+">");
        writer.println("<label for="+"pwd"+">Password:");
        writer.println("<input type="+"text"+" name="+"pwd"+"/>");
        writer.println("</label>");
        writer.println("</div>");

        //button form
        writer.println("<div class="+"button"+">");
        writer.println("<button type="+"submit"+" name="+"button_connexion"+" value="+"Se_connecter"+">Connexion</button>");
        writer.println("</div>");
        writer.println("</form>");

        //aller sur le lien de l'inscription
        writer.println("<a href="+"/ProjetWeb2020/Inscription"+">T'es nouveau?Par ici l'inscription</a>");
        writer.println("</body>");


    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();

        String login, pwd;

        login=req.getParameter("Login");
        pwd=req.getParameter("Pwd");

        if(login==null && pwd==null){
            writer.println("<h1>Not good!</h1>");
        }
        //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);

        writer.println("<h2>login is:"+login+"</h2>");
        writer.println("<h2>pwd is:"+pwd+"</h2>");
    }
}

结果我再也不想了。

enter image description here

我将很高兴感激任何答案:)

java html web post servlets
1个回答
0
投票

访问时更改参数名称。login = req.getParameter(“ login”);pwd = req.getParameter(“ pwd”);

它将起作用。

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