需要在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.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) 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); } }原文链接:https://www.f2er.com/ajax/166258.html