英文:
Java Servlet Form Post issue : getParameters() return null values
问题
我在这里是你的中文翻译,以下是你提供的内容的翻译部分:
我在这里是一个新人,不是以英语为母语的 :).
所以,我编写了一个表单,通过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;
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>欢迎访问我们的新网站</h1>");
writer.println("<body>");
//表单
writer.println("<form action=\"Authentification\" method=\"post\" class=\"form\">");
//登录
writer.println("<div class=\"\">");
writer.println("<label for=\"login\">登录:");
writer.println("<input type=\"text\" name=\"login\"/>");
writer.println("</label>");
writer.println("</div>");
//密码
writer.println("<div class=\"\">");
writer.println("<label for=\"pwd\">密码:");
writer.println("<input type=\"text\" name=\"pwd\"/>");
writer.println("</label>");
writer.println("</div>");
//表单按钮
writer.println("<div class=\"button\">");
writer.println("<button type=\"submit\" name=\"button_connexion\" value=\"Se_connecter\">连接</button>");
writer.println("</div>");
writer.println("</form>");
//前往注册链接
writer.println("<a href=\"/ProjetWeb2020/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>不好意思!</h1>");
}
//JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
writer.println("<h2>登录名为:" + login + "</h2>");
writer.println("<h2>密码为:" + pwd + "</h2>");
}
}
至此为止,不再提供结果。我将非常乐意为你提供任何回答 :)。
英文:
i am new here and not native english speaker :).
So, i programmed a form that sent simple values via a post method within a java servlet.
At first, it worked with request.getParameter(), i don't know what i did, but it doesn't work anymore.
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>");
}
}
And the result i don't want no more.
I will aprecciate any answer with great pleasure
答案1
得分: 1
好的,以下是您提供的代码的中文翻译部分:
所以,非常感谢大家,尤其是 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>欢迎访问我们的新网站</h1>");
writer.println("<body>");
//表单
writer.println("<form action=\"Authentification\" method=\"post\" class=\"form\">");
//登录
writer.println("<div class=\"\">");
writer.println("<label for=\"login\">登录:");
writer.println("<input type=\"text\" name=\"login\"/>");
writer.println("</label>");
writer.println("</div>");
//密码
writer.println("<div class=\"\">");
writer.println("<label for=\"pwd\">密码:");
writer.println("<input type=\"text\" name=\"pwd\"/>");
writer.println("</label>");
writer.println("</div>");
//按钮表单
writer.println("<div class=\"button\">");
writer.println("<button type=\"submit\" name=\"button_connexion\" value=\"Se_connecter\">连接</button>");
writer.println("</div>");
writer.println("</form>");
//跳转到注册链接
writer.println("<a href=\"/ProjetWeb2020/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>不好意思!</h1>");
}
//JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
writer.println("<h2>登录名是:" + login + "</h2>");
writer.println("<h2>密码是:" + pwd + "</h2>");
}
}
以及HTML页面的语法(您可以在我之前的评论中查看旧的HTML代码):
欢迎访问我们的新网站
新来的?这边进行注册
```
故事寓意:注意使用“"”,在HTML代码中使用反斜杠来处理字符串!
<details>
<summary>英文:</summary>
So, thank you everybody, and especially Swati for the solution. Yes, the issue as a typo thing about "backslash".
I give you the correct code:
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>");
}
}
And the syntax of the html page:(you can check the ancient html code in my previous comments)
<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>
Moral of the story:be careful with " ", use Backslash in strings for html code!
</details>
# 答案2
**得分**: 0
更改访问时的参数名称。
login=req.getParameter("login");
pwd=req.getParameter("pwd");
它将起作用。
<details>
<summary>英文:</summary>
change the param names while accessing.
login=req.getParameter("login");
pwd=req.getParameter("pwd");
it will work.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论