Servlet 页面跳转 并弹出提示对话框
虾米姐
阅读:1116
2021-03-31 22:40:21
评论:0
<pre name="code" class="html">简单的登录验证模块
点击登录进入相应的Servlet,Servlet调用bean验证用户名是否存在
如果存在则登录成功,跳转
如果不存在则弹出错误对话框,并回到登录页面
开始用的下面两句话
out.print("<script language='javascript'>alert('the name doesnot exit')</script>");
response.sendRedirect("Login.jsp");
但不会显示对话框,而是直接回到Login.jsp
后来改用
JOptionPane.showMessageDialog(null, "name doesnot exits");
response.sendRedirect("Login.jsp");
则可以实现。
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String code=request.getParameter("code");
//状态码(0:默认值(用户未登入成功),1:用户登入成功,2:验证码通过)
String message="0";
boolean target=false;
if(username.equals("")){
username="0";
}
if(password.equals("")){
password="0";
}
HttpSession sessions = request.getSession();
if(sessions.getAttribute("safecode")!=null ){
target=true;
message="2";
}
if(target){
String sql="select * from userinfo info where info.`username`='"+username.trim()+"' and info.`password`='"+password.trim()+"'";
try {
ResultSet rusultset=DataBase.select(sql);
while(rusultset.next()){
message="1";
HttpSession session = request.getSession();
session.setAttribute("username",username);
}
DataBase.closeAll(rusultset);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<span style="color:#ff0000;">
response.setContentType("text/;html");
PrintWriter out=response.getWriter();
//登入信息返回
if(message.equals("0")){//默认
JOptionPane.showMessageDialog(null, "用户名或密码不正确");
response.sendRedirect("./login.html");
}else if(message.equals("1")){//成功
response.sendRedirect("./message.html");
}else if(message.equals("2")){//用户名已经存在
JOptionPane.showMessageDialog(null, "注册码不正确");
response.sendRedirect("./login.html");
}else{//特殊情况
JOptionPane.showMessageDialog(null, "用户名或密码不正确");
response.sendRedirect("./login.html");
}
out.flush();
out.close();</span>
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。