如何在servlet中获取表单参数? request.getAttribute 不起作用
是否可以让同一个 servlet 执行验证?似乎在这里可能需要利用某种递归,但是当我在电子邮件框中输入内容并单击“提交”时,电子邮件参数仍然为空。 单击提交后,URL 更改为: http://localhost/servlet/EmailServlet?Email=test
该页面显示 Email: null
和文本框,但我期望它通过验证函数(即不为空)。是否有可能实现这种类型的递归行为?
public class EmailServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String theForm =
"<FORM METHOD=\"GET\" ACTION=\"EmailServlet\">\n<INPUT TYPE=\"TEXT\" NAME=\"Email\"><P>\n"
+ "<INPUT TYPE=\"SUBMIT\">\n</FORM>";
String email = (String) request.getAttribute("Email");
// Bogus email validation...
if( email == null )
{
out.println("Email: " + email + "\n" + theForm);
}
else if(emailAddressNotBogous(email))
{
out.println("Thank you!");
}
else
{
out.println("“Invalid input. Please try again:\n" + theForm);
}
out.flush();
}
}
更新:正如已接受的答案所指出的那样,代码中有错误。将 getAttribute 更改为 getParameter 修复了“问题”:)。
String email = (String) request.getAttributegetParameter("Email");
Is it possible to have the same servlet perform validation? It seems that one might have to utilize some sort of recursion here, but when I type in something in the e-mail box and click submit the e-mail parameter is still blank.
After I click submit, the URL changes to: http://localhost/servlet/EmailServlet?Email=test
The page shows Email: null
and the text box, but I was expecting it to go through the validation function (i.e. not be null). Is it possible to achieve this type of recursive behavior?
public class EmailServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String theForm =
"<FORM METHOD=\"GET\" ACTION=\"EmailServlet\">\n<INPUT TYPE=\"TEXT\" NAME=\"Email\"><P>\n"
+ "<INPUT TYPE=\"SUBMIT\">\n</FORM>";
String email = (String) request.getAttribute("Email");
// Bogus email validation...
if( email == null )
{
out.println("Email: " + email + "\n" + theForm);
}
else if(emailAddressNotBogous(email))
{
out.println("Thank you!");
}
else
{
out.println("“Invalid input. Please try again:\n" + theForm);
}
out.flush();
}
}
Update: as the accepted answer pointed out, there was an error in the code. Changing the getAttribute to getParameter fixes the "problem" :).
String email = (String) request.getAttributegetParameter("Email");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在 servlet 中获取表单参数,您可以使用:
是的,您可以使用相同的 servlet,但使用两个不同的 servlet 来执行此操作会更容易。
To get a form parameter in a servlet you use:
And yes you can use the same servlet but it would be way easier to use two different servlets to do this.
您可以将表单的方法设置为 POST,然后在 servlet 中实现 doPost() 方法。 doGet() 将被调用来显示表单,并且 doPost() 将被调用来处理表单提交。
或者,您可以使用 doGet() 方法测试是否存在任何参数。如果没有,则仅显示表单。如果有则处理提交...
you could have the form's method set to POST and then implement a doPost() method in your servlet. the doGet() will get called to display the form and the doPost() will get called to process the form submission.
alternatively you could have the doGet() method test for the presence of any parameters. if there aren't any then just display the form. if there are then process the submission...