ajax接收action的数据返回一大串的html代码的问题

需要在out.println("xxxx")后加入如下的代码  out.close()

package com.zb.web.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zb.domain.Employee;
import com.zb.service.interfac.EmployeeServiceInter;

public class LoginAction extends ActionSupport {
	
	
	private String id;
	private String password;
	private Map session  = ActionContext.getContext().getSession(); ;  
	private HttpServletResponse response = ServletActionContext.getResponse();
	private HttpServletRequest request = ServletActionContext.getRequest();
	private EmployeeServiceInter employeeService;
	
	//定义计数器,测试是否是一个sessionfactory
	private int a;

	
	
	public EmployeeServiceInter getEmployeeService() {
		return employeeService;
	}
	@Resource
	public void setEmployeeService(EmployeeServiceInter employeeService) {
		this.employeeService = employeeService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
public String checkEmp() throws Exception {
	//ͨ������������spring��ʵ��˷�������
//	 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
           //��spring�л�ȡbean
	//	  EmployeeServiceInter employeeServiceInter= (EmployeeServiceInter) ctx.getBean("employeeService");
	  
	//��a��ֵ�Ƿ�仯��֤ÿ������ʱaction�Ƿ���һ���������һ���Ļ���a��ֵ�������ӣ�����ǣ���aһֱ��0
	    //����һ��employee����
	//System.out.println("a="+(++a)); 
     if(request.getParameter("password")!=null&&request.getParameter("id")!=null) {
    	 id=request.getParameter("id");
    	 password=request.getParameter("password");
    	 
     }
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	System.out.println(id);
	System.out.println(password);
	 Employee e = new Employee();
	 e.setId(Integer.parseInt(id));
	 e.setPassword(password);
	  e = employeeService.checkEmployee(e);
	  if(e!=null){
		  //将登陆的用户信心放到session中
		  
		  session.put("loginUser", e);
		  if(e.getGrade()==5){
		  return "admin";
		}else{
			
			return "putong";
		}
	  }else{
		  
	       out.println("用户名或者密码错误");
	       out.close();
			 return INPUT;
	  }
	
	/*if("1".equals(id)&&"123".equals(password)){
			return SUCCESS;
			
		}else{
			 this.addFieldError("flag", "id���������������������");
			 return INPUT;
		}*/
		
	}

//注销用户
	public String cancellationEmp(){
		session.clear();
	return SUCCESS;
		
	}

}  
登录界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>
  <head>
   <meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  
   <title>用户登录界面</title>
     <style type="text/css">
     	.login{
     		font-family: Arial;
     		font-size: 14px;
     	}
     	
     	.newStyle {
			opacity: 0.5;
			box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5) ;
		    padding:10px; width:300px; height:50px;
		    border: 5px solid #dedede;
		    -moz-border-radius: 15px;     
		    border-radius:15px;          
		    background: -moz-radial-gradient(30px 30px, circle farthest-corner, #58ff00 0%, rgba(222, 255, 0, 0) 30%);   
		    vertical-align: middle;
}
     </style>
  <script type="text/javascript" src="/myFirstSSH03/js/myjs2.js"></script>
  </head>
  <body topmargin="150px">
  <center>
        <p></p>
    	<form action="check_loginAction" method="post">
    	   <fieldset id="inputs" style="width: 400; height: 150" class="newStyle"><br>
    	      <legend>雇员登录</legend>
       id:<input type="text" class="login" id="name" name="id" placeholder="请输入id" > <br><br>
              
         密码:<input type="password" class="login" name="password" id="pwd" placeholder="请输入密码" ><div id="div1"></div><br><br>
    
   </fieldset>
              <input type="submit" value="登录">
              <input type="reset" value="重置">
    	</form>
 </center>
  </body>
</html>

myjs2.js

function ajaxFunction()
 {
   var xmlHttp;
 
 try{
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
 }catch (e){

  // Internet Explorer
   try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e){
    try {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }catch (e){
         alert("您的浏览器不支持AJAX!");
         return false;
      }
    }
  }
	return xmlHttp;
 }
window.οnlοad=function(){
	//<th width="100px"><input type="text" id="name"  name="name" οnblur="demo()"/></th>
	
	document.getElementById("pwd").οnblur=function(){
		var xht= ajaxFunction();
		
		//接收从服务器传回来的信息
		xht.onreadystatechange=function(){
			if(xht.readyState==4&&xht.status==200){
				var div1Element=document.getElementById("div1");
				  var data=xht.responseText;
				 // alert(flag.equals("wrong"));
				  alert(data);
				   	div1Element.innerHTML = "<font color='red'>"+data+"</font>";
				  
			
			}
			
		}
		 var id=document.getElementById("name").value;
		 var password=document.getElementById("pwd").value;
		//连接服务器
		xht.open("get","./check_loginAction?timestatus="+new Date().getTime()+"&id="+id+"&password="+password,true);
		//发送数据
		xht.send(null);
		
	}
	 
	
	
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值