forms - 如何处理登录表单中的 Servlet?
问题描述
我正在构建一个仅使用 JSP 页面和 Servlet 的 Web 应用程序。我正在尝试制作登录表单,但无法访问主页。有谁知道如何处理这个?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
Iterator it = list_etudiant().iterator();
System.out.println("username" + username);
while (it.hasNext()) {
Etudiant et = (Etudiant) it.next();
String user_name = et.getEmailEtudiant();
String pass_word = et.getPasswordEtudiant();
if ((user_name == null ? username == null : user_name.equals(username)) && (pass_word == null ? password == null : pass_word.equals(password))) {
response.sendRedirect("View/Home.jsp");
}
}
} catch (Exception ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
response.sendRedirect("index.html");
}
}
还有一个问题,为什么我在 Netbeans 控制台中看不到输出?例如,当我想打印一些东西时:
System.out.println("username is : "+username);
我找不到输出,我只是得到这个:
BUILD SUCCESSFUL (total time: 1 second)
解决方案
if ((user_name == null ? username == null : user_name.equals(username)) &&
(pass_word == null ? password == null : pass_word.equals(password))) {
response.sendRedirect("View/Home.jsp");
}
我希望你的其余代码工作正常。参考上面的代码片段。我猜你的观点是用户名和密码不应该为空并且需要相等。
if (((user_name != null) && (user_name.equals(username)))
&& ((pass_word != null) && (pass_word.equals(password)))) {
System.out.println("username is : " + username); // If you need to print it on console
response.sendRedirect("View/Home.jsp");
}
您的条件检查不正确。请参考?:运算符
推荐阅读
- tcl - 将组合框放在 ttk::notebook 的框架上
- elasticsearch - ElasticSearch 频繁更新
- wordpress - Wordpress get_posts 自定义类别
- amazon-web-services - 如何将我的 aws ec2 实例从 http 重定向到 https?
- oracle11g - 试图了解 oracle REGEXP_REPLACE 是如何工作的
- angular - 子路由未路由到角度 5 中的内容页面
- php - Zend_translate 类未找到
- maven - maven exec:java 可以与命令 iine 中的任意 java 类一起使用吗
- java - GridLayout - 基于单元格宽度的高度
- jquery - 如何使用数据转到另一个页面