Java EE:从 POST 获取登录表单的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11036651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java EE: Getting parameters from POST for a login form
提问by JF Beaulieu
I am trying to implement a simple login servlet but it's not working properly.
我正在尝试实现一个简单的登录 servlet,但它无法正常工作。
What I wanted to knowis how to pass the parameters using a HTTP POST. It already works with HTTP GET but the username and password are visible from the URL. It would be better to hide them in a POST.
我想知道的是如何使用 HTTP POST 传递参数。它已经适用于 HTTP GET,但用户名和密码可以从 URL 中看到。最好将它们隐藏在 POST 中。
<form method="post" action="home" >
<input name="username" class="form-login" title="Username" value="" size="30" maxlength="2048" />
<input name="password" type="password" class="form-login" title="Password" value="" size="30" maxlength="2048" />
<input type="submit" value="Connect">
</form>
web.xml
网页.xml
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>controller.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
Servlet:
服务端:
public class HomeController extends HttpServlet {
private HttpSession session;
private UserBean userBean;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserBean user = new UserBean();
String userName = request.getParameter("username");
String password = request.getParameter("password");
user.setUsername(userName);
user.setPassword(password);
user = UserDAO.login(user);
dispatch(request, response, ApplicationRessource.getInstance().getHomePage());
}
protected void dispatch(HttpServletRequest request,
HttpServletResponse response, String page)
throws javax.servlet.ServletException, java.io.IOException {
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(page);
dispatcher.forward(request, response);
}
}
The problem is that the userName
and password
strings are always empty, meaning that the parameters are never fetched from the POST. What am I doing wrong?
问题是userName
和password
字符串始终为空,这意味着永远不会从 POST 中获取参数。我究竟做错了什么?
采纳答案by Subin Sebastian
it should work, can you check by changing form method to get and trying, you should see parameters in url.
它应该可以工作,您可以通过更改表单方法来检查并尝试,您应该在 url 中看到参数。
回答by Ankit
Please try this
请试试这个
In your doPost(..) method code only doGet(..) and put all your logic in doGet(..) and check if still it is giving blank values.
在您的 doPost(..) 方法中,仅代码 doGet(..) 并将所有逻辑放入 doGet(..) 并检查它是否仍然给出空白值。
Let me know what is the output.
让我知道输出是什么。
Example:-
例子:-
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserBean user = new UserBean();
String userName = request.getParameter("username");
String password = request.getParameter("password");
user.setUsername(userName);
user.setPassword(password);
user = UserDAO.login(user);
dispatch(request, response, ApplicationRessource.getInstance().getHomePage());
}
回答by Kshitij
this simple implementation should have worked ..but since its not, there maybe some code which is manipulating the request. The code you have posted is not sufficient to determine that.
这个简单的实现应该有效..但由于它没有,可能有一些代码正在操纵请求。您发布的代码不足以确定这一点。
Some pointers I can give are -
我可以给出的一些指示是——
Check your
web.xml
to see if there is any filter/interceptor which is manipulating the request.Which web/app server are you using? Have you checked the
service(Http...)
method implementation of HttpServlet. You cantry placing a debug point in service(..) method
to see if the request object here has the required request parameters. If it doesn't, then the problem exists either in some filter or your jsp itself.What does
dispatch(request, response, ApplicationRessource.getInstance().getHomePage());
do? I know the problem is before this line, but this is not a standard HttpServlet method, so I assume there's lot more custom code then whats been posted in the question above.
检查您
web.xml
是否有任何过滤器/拦截器正在处理请求。您使用的是哪个网络/应用程序服务器?你检查过
service(Http...)
HttpServlet的方法实现了吗?可以try placing a debug point in service(..) method
在这里查看请求对象是否有需要的请求参数。如果没有,那么问题要么存在于某个过滤器中,要么存在于您的 jsp 本身。有什么作用
dispatch(request, response, ApplicationRessource.getInstance().getHomePage());
?我知道问题在这一行之前,但这不是标准的 HttpServlet 方法,所以我假设有更多的自定义代码然后是上面问题中发布的内容。