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

huangapple go评论79阅读模式
英文:

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(&quot;text/html&quot;);
PrintWriter writer = res.getWriter();
writer.println(&quot;&lt;h1&gt;Bienvenue sur notre nouveau site WEB&lt;/h1&gt;&quot;);
writer.println(&quot;&lt;body&gt;&quot;);
//Form
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;);
//login
writer.println(&quot;&lt;div class=&quot;+&quot;&gt;&quot;);
writer.println(&quot;&lt;label for=&quot;+&quot;login&quot;+&quot;&gt;Login:&quot;);
writer.println(&quot;&lt;input type=&quot;+&quot;text&quot;+&quot; name=&quot;+&quot;login&quot;+&quot;/&gt;&quot;);
writer.println(&quot;&lt;/label&gt;&quot;);
writer.println(&quot;&lt;/div&gt;&quot;);
//password
writer.println(&quot;&lt;div class=&quot;+&quot;&gt;&quot;);
writer.println(&quot;&lt;label for=&quot;+&quot;pwd&quot;+&quot;&gt;Password:&quot;);
writer.println(&quot;&lt;input type=&quot;+&quot;text&quot;+&quot; name=&quot;+&quot;pwd&quot;+&quot;/&gt;&quot;);
writer.println(&quot;&lt;/label&gt;&quot;);
writer.println(&quot;&lt;/div&gt;&quot;);
//button form
writer.println(&quot;&lt;div class=&quot;+&quot;button&quot;+&quot;&gt;&quot;);
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;);
writer.println(&quot;&lt;/div&gt;&quot;);
writer.println(&quot;&lt;/form&gt;&quot;);
//aller sur le lien de l&#39;inscription
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;);
writer.println(&quot;&lt;/body&gt;&quot;);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
res.setContentType(&quot;text/html&quot;);
PrintWriter writer = res.getWriter();
String login, pwd;
login=req.getParameter(&quot;Login&quot;);
pwd=req.getParameter(&quot;Pwd&quot;);
if(login==null &amp;&amp; pwd==null){
writer.println(&quot;&lt;h1&gt;Not good!&lt;/h1&gt;&quot;);
}
//JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
writer.println(&quot;&lt;h2&gt;login is:&quot;+login+&quot;&lt;/h2&gt;&quot;);
writer.println(&quot;&lt;h2&gt;pwd is:&quot;+pwd+&quot;&lt;/h2&gt;&quot;);
}
}

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

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

所以,非常感谢大家,尤其是 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 &quot;backslash&quot;.
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(&quot;text/html&quot;);
PrintWriter writer = res.getWriter();
writer.println(&quot;&lt;h1&gt;Bienvenue sur notre nouveau site WEB&lt;/h1&gt;&quot;);
writer.println(&quot;&lt;body&gt;&quot;);
//Form
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;);
//login
writer.println(&quot;&lt;div class=\&quot; \&quot;&gt;&quot;);
writer.println(&quot;&lt;label for=&quot;+&quot;\&quot;login\&quot;&quot;+&quot;&gt;Login:&quot;);
writer.println(&quot;&lt;input type=&quot;+&quot;\&quot;text\&quot;&quot;+&quot; name=&quot;+&quot;\&quot;login\&quot;&quot;+&quot;/&gt;&quot;);
writer.println(&quot;&lt;/label&gt;&quot;);
writer.println(&quot;&lt;/div&gt;&quot;);
//password
writer.println(&quot;&lt;div class=\&quot; \&quot;&gt;&quot;);
writer.println(&quot;&lt;label for=&quot;+&quot;\&quot;pwd\&quot;&quot;+&quot;&gt;Password:&quot;);
writer.println(&quot;&lt;input type=&quot;+&quot;\&quot;text\&quot;&quot;+&quot; name=&quot;+&quot;\&quot;pwd\&quot;&quot;+&quot;/&gt;&quot;);
writer.println(&quot;&lt;/label&gt;&quot;);
writer.println(&quot;&lt;/div&gt;&quot;);
//button form
writer.println(&quot;&lt;div class=&quot;+&quot;\&quot;button\&quot;&quot;+&quot;&gt;&quot;);
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;);
writer.println(&quot;&lt;/div&gt;&quot;);
writer.println(&quot;&lt;/form&gt;&quot;);
//aller sur le lien de l&#39;inscription
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;);
writer.println(&quot;&lt;/body&gt;&quot;);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
res.setContentType(&quot;text/html&quot;);
PrintWriter writer = res.getWriter();
String login, pwd;
login=req.getParameter(&quot;login&quot;);
pwd=req.getParameter(&quot;pwd&quot;);
if(login==null &amp;&amp; pwd==null){
writer.println(&quot;&lt;h1&gt;Not good!&lt;/h1&gt;&quot;);
}
//JSONObject obj=services.Authentification.loginUtilisateur(login, pwd);
writer.println(&quot;&lt;h2&gt;login is:&quot;+login+&quot;&lt;/h2&gt;&quot;);
writer.println(&quot;&lt;h2&gt;pwd is:&quot;+pwd+&quot;&lt;/h2&gt;&quot;);
}

}


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 &quot; &quot;, 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(&quot;login&quot;);
pwd=req.getParameter(&quot;pwd&quot;); 
it will work.
</details>

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

发表评论

匿名网友

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

确定