public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String UserName = request.getParameter("username");
String UserPass = request.getParameter("userpass");
if(UserName =="123" && UserPass=="123"){
response.sendRedirect("MyJsp.jsp");
}else {
JOptionPane.showConfirmDialog(null, "请检查输入的内容是否出错");
}
}我这样写了之后 UserName 和 UserPass 都对的情况下还会弹出提示框
各位帮我看下应该怎么改成 在用户名和密码正确的情况下跳到相应的页面。输入错误才弹出提示框
throws ServletException, IOException { doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String UserName = request.getParameter("username");
String UserPass = request.getParameter("userpass");
if(UserName =="123" && UserPass=="123"){
response.sendRedirect("MyJsp.jsp");
}else {
JOptionPane.showConfirmDialog(null, "请检查输入的内容是否出错");
}
}我这样写了之后 UserName 和 UserPass 都对的情况下还会弹出提示框
各位帮我看下应该怎么改成 在用户名和密码正确的情况下跳到相应的页面。输入错误才弹出提示框
if(UserName =="123" && UserPass=="123")改为if(UserName.equals("123") && UserPass.equals("123"))
换成
UserName.equals("123") && UserPass.equals("123")
应该是这里的原因,改成equals()方法就可以了
if(UserName.equals("123") && UserPass.equals("123"))的都是正解,因为equals()方法比较的是两个字符串中的值是否相等,而==判断的不仅仅是值是否相等,还要判断两个字符串是否占用的同一内存,必须两者都成立==的值才为true。而你定义的UserName和UserPass字符串和""字符串的内存是不相同的。
散分吧
String a="123";
System.out.println(q.equals("123"));
System.out.println(a=="123");
System.out.println("123"=="123");
亲身体验过,相信楼主后更深刻的。