Servlet 页面跳转 并弹出提示对话框

<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);
		
	}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值