gpt4 book ai didi

Java Servlet 表单发布问题 : getParameters() return null values

转载 作者:行者123 更新时间:2023-12-02 08:45:36 25 4
gpt4 key购买 nike

我是新来的,母语不是英语:)。因此,我编写了一个表单,通过 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

我将非常高兴地接受任何答案:)

最佳答案

所以,谢谢大家,特别是 Swati 的解决方案。是的,这个问题是关于“反斜杠”的拼写错误。

我给你正确的代码:

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>");
}
}

以及html页面的语法:(你可以在我之前的评论中查看古老的html代码)

<h1>Bienvenue sur notre nouveau site WEB</h1>
<body>
<form action="Authentification" method="post" class="form">
<div class=" ">
<label for="login">Login:
<input type="text" name="login"/>
</label>
</div>
<div class=" ">
<label for="pwd">Password:
<input type="text" name="pwd"/>
</label>
</div>
<div class="button">
<button type="submit" name="button_connexion" value="Se_connecter">Connexion</button>
</div>
</form>
<a href=/ProjetWeb2020/Inscription>T'es nouveau?Par ici l'inscription</a>
</body>

这个故事的寓意:小心“”,在 html 代码的字符串中使用反斜杠!

关于Java Servlet 表单发布问题 : getParameters() return null values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61105775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com