ajax接收action的数据返回一大串的html代码的问题
2013-06-08 20:36
459 查看
需要在out.println("xxxx")后加入如下的代码 out.close()
myjs2.js
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.onload=function(){ //<th width="100px"><input type="text" id="name" name="name" onblur="demo()"/></th> document.getElementById("pwd").onblur=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); } }
相关文章推荐
- SSH Ajax Action 不能接收返回数据的问题
- jQuery解析AJAX返回的html数据时碰到的问题与解决
- jQuery解析AJAX返回的html数据时碰到的问题与解决
- jQuery解析AJAX返回的html数据时碰到的问题与解决
- ajax实现返回数据是html类型的跨域问题
- Ajax例子,views返回,html接收数据
- AJAX第一步:AJAX接收返回类型为text/html的字符串数据
- 对ajax的二次封装,返回接收到的数据
- ajax接收java后台传过来的json数据乱码及获取字段undefind问题
- AJAX第三步:AJAX接收返回为JSON类型的数据
- AJAX ,在jsp处理 action返回的不同数据类型
- ajax代码点滴:Post数据问题
- socket tcp使用recv接收数据时,返回errno错误代码88
- asp.net mvc 使用Ajax调用Action 返回数据。alert /modal VB
- ajax提交数据到后台处理返回json问题
- Ajax servlet 返回xml数据乱码问题
- ajax请求后台并接收返回数据
- X5 报表的ACTION 数据返回TABLE 代码
- asp.net mvc 使用Ajax调用Action 返回数据。
- 关于在servlet和action中返回json数据的一些问题