Java Servlet表单提交问题:getParameters()方法返回空值。

huangapple 未分类评论53阅读模式
英文:

Java Servlet Form Post issue : getParameters() return null values

问题

我在这里是你的中文翻译,以下是你提供的内容的翻译部分:

我在这里是一个新人,不是以英语为母语的 :).
所以,我编写了一个表单,通过Java Servlet使用POST方法发送简单的值。
起初,它可以使用request.getParameter()正常工作,我不知道我做了什么,但现在不再起作用了。

  1. package servlets;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class Authentification extends HttpServlet{
  9. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  10. res.setContentType("text/html");
  11. PrintWriter writer = res.getWriter();
  12. writer.println("<h1>欢迎访问我们的新网站</h1>");
  13. writer.println("<body>");
  14. //表单
  15. writer.println("<form action=\"Authentification\" method=\"post\" class=\"form\">");
  16. //登录
  17. writer.println("<div class=\"\">");
  18. writer.println("<label for=\"login\">登录:");
  19. writer.println("<input type=\"text\" name=\"login\"/>");
  20. writer.println("</label>");
  21. writer.println("</div>");
  22. //密码
  23. writer.println("<div class=\"\">");
  24. writer.println("<label for=\"pwd\">密码:");
  25. writer.println("<input type=\"text\" name=\"pwd\"/>");
  26. writer.println("</label>");
  27. writer.println("</div>");
  28. //表单按钮
  29. writer.println("<div class=\"button\">");
  30. writer.println("<button type=\"submit\" name=\"button_connexion\" value=\"Se_connecter\">连接</button>");
  31. writer.println("</div>");
  32. writer.println("</form>");
  33. //前往注册链接
  34. writer.println("<a href=\"/ProjetWeb2020/Inscription\">是新用户吗?前往注册</a>");
  35. writer.println("</body>");
  36. }
  37. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  38. res.setContentType("text/html");
  39. PrintWriter writer = res.getWriter();
  40. String login, pwd;
  41. login=req.getParameter("Login");
  42. pwd=req.getParameter("Pwd");
  43. if(login==null && pwd==null){
  44. writer.println("<h1>不好意思!</h1>");
  45. }
  46. //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
  47. writer.println("<h2>登录名为:" + login + "</h2>");
  48. writer.println("<h2>密码为:" + pwd + "</h2>");
  49. }
  50. }

至此为止,不再提供结果。我将非常乐意为你提供任何回答 :)。

英文:

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.

  1. package servlets;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. //import services.*;
  9. //import org.json.JSONException;
  10. //import org.json.JSONObject;
  11. public class Authentification extends HttpServlet{
  12. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  13. res.setContentType(&quot;text/html&quot;);
  14. PrintWriter writer = res.getWriter();
  15. writer.println(&quot;&lt;h1&gt;Bienvenue sur notre nouveau site WEB&lt;/h1&gt;&quot;);
  16. writer.println(&quot;&lt;body&gt;&quot;);
  17. //Form
  18. writer.println(&quot;&lt;form action=&quot;+&quot;Authentification&quot;+&quot; method=&quot;+&quot;post&quot;+&quot; class=&quot;+&quot;form&quot;+&quot;&gt;&quot;);
  19. //login
  20. writer.println(&quot;&lt;div class=&quot;+&quot;&gt;&quot;);
  21. writer.println(&quot;&lt;label for=&quot;+&quot;login&quot;+&quot;&gt;Login:&quot;);
  22. writer.println(&quot;&lt;input type=&quot;+&quot;text&quot;+&quot; name=&quot;+&quot;login&quot;+&quot;/&gt;&quot;);
  23. writer.println(&quot;&lt;/label&gt;&quot;);
  24. writer.println(&quot;&lt;/div&gt;&quot;);
  25. //password
  26. writer.println(&quot;&lt;div class=&quot;+&quot;&gt;&quot;);
  27. writer.println(&quot;&lt;label for=&quot;+&quot;pwd&quot;+&quot;&gt;Password:&quot;);
  28. writer.println(&quot;&lt;input type=&quot;+&quot;text&quot;+&quot; name=&quot;+&quot;pwd&quot;+&quot;/&gt;&quot;);
  29. writer.println(&quot;&lt;/label&gt;&quot;);
  30. writer.println(&quot;&lt;/div&gt;&quot;);
  31. //button form
  32. writer.println(&quot;&lt;div class=&quot;+&quot;button&quot;+&quot;&gt;&quot;);
  33. writer.println(&quot;&lt;button type=&quot;+&quot;submit&quot;+&quot; name=&quot;+&quot;button_connexion&quot;+&quot; value=&quot;+&quot;Se_connecter&quot;+&quot;&gt;Connexion&lt;/button&gt;&quot;);
  34. writer.println(&quot;&lt;/div&gt;&quot;);
  35. writer.println(&quot;&lt;/form&gt;&quot;);
  36. //aller sur le lien de l&#39;inscription
  37. writer.println(&quot;&lt;a href=&quot;+&quot;/ProjetWeb2020/Inscription&quot;+&quot;&gt;T&#39;es nouveau?Par ici l&#39;inscription&lt;/a&gt;&quot;);
  38. writer.println(&quot;&lt;/body&gt;&quot;);
  39. }
  40. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  41. res.setContentType(&quot;text/html&quot;);
  42. PrintWriter writer = res.getWriter();
  43. String login, pwd;
  44. login=req.getParameter(&quot;Login&quot;);
  45. pwd=req.getParameter(&quot;Pwd&quot;);
  46. if(login==null &amp;&amp; pwd==null){
  47. writer.println(&quot;&lt;h1&gt;Not good!&lt;/h1&gt;&quot;);
  48. }
  49. //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
  50. writer.println(&quot;&lt;h2&gt;login is:&quot;+login+&quot;&lt;/h2&gt;&quot;);
  51. writer.println(&quot;&lt;h2&gt;pwd is:&quot;+pwd+&quot;&lt;/h2&gt;&quot;);
  52. }
  53. }

And the result i don't want no more.

enter image description here

I will aprecciate any answer with great pleasure Java Servlet表单提交问题:getParameters()方法返回空值。

答案1

得分: 1

好的,以下是您提供的代码的中文翻译部分:

  1. 所以,非常感谢大家,尤其是 Swati 提供的解决方案。是的,这个问题是关于“反斜杠”的一个拼写错误。
  2. 我给您正确的代码:

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{

  1. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  2. res.setContentType("text/html");
  3. PrintWriter writer = res.getWriter();
  4. writer.println("<h1>欢迎访问我们的新网站</h1>");
  5. writer.println("<body>");
  6. //表单
  7. writer.println("<form action=\"Authentification\" method=\"post\" class=\"form\">");
  8. //登录
  9. writer.println("<div class=\"\">");
  10. writer.println("<label for=\"login\">登录:");
  11. writer.println("<input type=\"text\" name=\"login\"/>");
  12. writer.println("</label>");
  13. writer.println("</div>");
  14. //密码
  15. writer.println("<div class=\"\">");
  16. writer.println("<label for=\"pwd\">密码:");
  17. writer.println("<input type=\"text\" name=\"pwd\"/>");
  18. writer.println("</label>");
  19. writer.println("</div>");
  20. //按钮表单
  21. writer.println("<div class=\"button\">");
  22. writer.println("<button type=\"submit\" name=\"button_connexion\" value=\"Se_connecter\">连接</button>");
  23. writer.println("</div>");
  24. writer.println("</form>");
  25. //跳转到注册链接
  26. writer.println("<a href=\"/ProjetWeb2020/Inscription\">新来的?这边进行注册</a>");
  27. writer.println("</body>");
  28. }
  29. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  30. res.setContentType("text/html");
  31. PrintWriter writer = res.getWriter();
  32. String login, pwd;
  33. login=req.getParameter("login");
  34. pwd=req.getParameter("pwd");
  35. if(login==null && pwd==null){
  36. writer.println("<h1>不好意思!</h1>");
  37. }
  38. //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
  39. writer.println("<h2>登录名是:" + login + "</h2>");
  40. writer.println("<h2>密码是:" + pwd + "</h2>");
  41. }

}

  1. 以及HTML页面的语法(您可以在我之前的评论中查看旧的HTML代码):

欢迎访问我们的新网站

新来的?这边进行注册

```

故事寓意:注意使用“"”,在HTML代码中使用反斜杠来处理字符串!

  1. <details>
  2. <summary>英文:</summary>
  3. So, thank you everybody, and especially Swati for the solution. Yes, the issue as a typo thing about &quot;backslash&quot;.
  4. 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{

  1. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  2. res.setContentType(&quot;text/html&quot;);
  3. PrintWriter writer = res.getWriter();
  4. writer.println(&quot;&lt;h1&gt;Bienvenue sur notre nouveau site WEB&lt;/h1&gt;&quot;);
  5. writer.println(&quot;&lt;body&gt;&quot;);
  6. //Form
  7. writer.println(&quot;&lt;form action=&quot;+&quot;\&quot;Authentification\&quot;&quot;+&quot; method=&quot;+&quot;\&quot;post\&quot;&quot;+&quot; class=&quot;+&quot;\&quot;form\&quot;&quot;+&quot;&gt;&quot;);
  8. //login
  9. writer.println(&quot;&lt;div class=\&quot; \&quot;&gt;&quot;);
  10. writer.println(&quot;&lt;label for=&quot;+&quot;\&quot;login\&quot;&quot;+&quot;&gt;Login:&quot;);
  11. writer.println(&quot;&lt;input type=&quot;+&quot;\&quot;text\&quot;&quot;+&quot; name=&quot;+&quot;\&quot;login\&quot;&quot;+&quot;/&gt;&quot;);
  12. writer.println(&quot;&lt;/label&gt;&quot;);
  13. writer.println(&quot;&lt;/div&gt;&quot;);
  14. //password
  15. writer.println(&quot;&lt;div class=\&quot; \&quot;&gt;&quot;);
  16. writer.println(&quot;&lt;label for=&quot;+&quot;\&quot;pwd\&quot;&quot;+&quot;&gt;Password:&quot;);
  17. writer.println(&quot;&lt;input type=&quot;+&quot;\&quot;text\&quot;&quot;+&quot; name=&quot;+&quot;\&quot;pwd\&quot;&quot;+&quot;/&gt;&quot;);
  18. writer.println(&quot;&lt;/label&gt;&quot;);
  19. writer.println(&quot;&lt;/div&gt;&quot;);
  20. //button form
  21. writer.println(&quot;&lt;div class=&quot;+&quot;\&quot;button\&quot;&quot;+&quot;&gt;&quot;);
  22. writer.println(&quot;&lt;button type=&quot;+&quot;\&quot;submit\&quot;&quot;+&quot; name=&quot;+&quot;\&quot;button_connexion\&quot;&quot;+&quot; value=&quot;+&quot;\&quot;Se_connecter\&quot;&quot;+&quot;&gt;Connexion&lt;/button&gt;&quot;);
  23. writer.println(&quot;&lt;/div&gt;&quot;);
  24. writer.println(&quot;&lt;/form&gt;&quot;);
  25. //aller sur le lien de l&#39;inscription
  26. writer.println(&quot;&lt;a href=&quot;+&quot;/ProjetWeb2020/Inscription&quot;+&quot;&gt;T&#39;es nouveau?Par ici l&#39;inscription&lt;/a&gt;&quot;);
  27. writer.println(&quot;&lt;/body&gt;&quot;);
  28. }
  29. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  30. res.setContentType(&quot;text/html&quot;);
  31. PrintWriter writer = res.getWriter();
  32. String login, pwd;
  33. login=req.getParameter(&quot;login&quot;);
  34. pwd=req.getParameter(&quot;pwd&quot;);
  35. if(login==null &amp;&amp; pwd==null){
  36. writer.println(&quot;&lt;h1&gt;Not good!&lt;/h1&gt;&quot;);
  37. }
  38. //JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
  39. writer.println(&quot;&lt;h2&gt;login is:&quot;+login+&quot;&lt;/h2&gt;&quot;);
  40. writer.println(&quot;&lt;h2&gt;pwd is:&quot;+pwd+&quot;&lt;/h2&gt;&quot;);
  41. }

}

  1. 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>

  1. Moral of the story:be careful with &quot; &quot;, use Backslash in strings for html code!
  2. </details>
  3. # 答案2
  4. **得分**: 0
  5. 更改访问时的参数名称。
  6. login=req.getParameter("login");
  7. pwd=req.getParameter("pwd");
  8. 它将起作用。
  9. <details>
  10. <summary>英文:</summary>
  11. change the param names while accessing.
  12. login=req.getParameter(&quot;login&quot;);
  13. pwd=req.getParameter(&quot;pwd&quot;);
  14. it will work.
  15. </details>

huangapple
  • 本文由 发表于 2020年4月9日 00:46:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61105775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定