登录功能实现代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011309353/article/details/72770444

简单的登录代码实现

第一步,页面提交登录请求

//form表单提交请求

<form id="loginFrom">
			         <div>
			         	<h2>管理登录</h2>
						<span><label>用户名</label></span>
						<span><input type="text" id="sysloginname" name="sysloginname" onkeypress="if(event.keyCode==13){ doLogin()};" class="validate[require] textbox" maxlength="15"/></span>
					 </div>
					 <div>
						<span><label>密码</label></span>
					    <span><input type="password" id="sysloginpass" name="sysloginpass" onkeypress="if(event.keyCode==13){ doLogin()};" class="validate[require] password " maxlength="15"/></span>
					 </div>
					<div class="sign">
                        <input type="button" value="登录" class="submit" onclick="doLogin();" />
					</div>
					</form>


//登录JS
	function doLogin() {
		if ($("#loginFrom").validationEngine('validate')) {
			var submitOptions = {
				target : '#loginFrom',
				url : "${pageContext.request.contextPath}/dologin.htm",
				type : 'post',
				dataType : 'html',
				cache : false,
				async : false,
				error : function(xhr) {
				},
				success : function(data) {
					var r = eval('(' + data + ')');
					if (r.flag) {
						window.location.href = "${pageContext.request.contextPath}/toappmain.htm";
					} else {
						alert(r.msg);
					}
				}
			}
			$('#loginFrom').ajaxSubmit(submitOptions);
		}
	}
第二步,action中处理请求

	@RequestMapping(value = "dologin.htm")
	public void doLogin(HttpSession httpsession, HttpServletRequest req,HttpServletResponse res, ModelMap model,String sysloginname,String sysloginpass) throws Exception{
		if(checkStr(sysloginname)){
			this.renderText("{'flag':false,'msg':'登录名错误!'}", res);
		}
		else if(checkStr(sysloginpass)){
			this.renderText("{'flag':false,'msg':'登录密码错误!'}", res);
		}else{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sysloginname", sysloginname);
			map.put("sysloginpass", MD5.convert(sysloginpass));
			MgrSysAccount appsysaccount = this.mgrSysAccountService.getLoginInfoByMap(map);
			if(appsysaccount!=null){
				MgrSysAccount updatedate = new MgrSysAccount();//用于修改最后一次登录时间
				updatedate.setId(appsysaccount.getId());
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				updatedate.setLastlogintime(simpleDateFormat.format(new Date()));
				mgrSysAccountService.updateEntity(updatedate);
				httpsession.setAttribute("syslogininfo", appsysaccount);
				this.renderText("{'flag':true}", res);
			}else{
				this.renderText("{'flag':false,'msg':'登录失败!'}", res);
			}
			
		}
		
		
	}
/**
	 * 校验非法字符
	 * @param 校验的字符串
	 * @return
	 */
    public boolean checkStr(String str) {
		if (StringUtils.isBlank(str.trim())) {
		    return true;
		}
		String inj_str = "‘,*,%,;,/,+,&,|,(,),$,!,[,]";
		String[] inj_stra = inj_str.split(",");
		for (int i = 0; i < inj_stra.length; i++) {
		    if (str.indexOf(inj_stra[i]) >= 0) {
		    	return true;
		    }
		}
		return false;
    }
/**
	 * 输出Text类型
	 * 
	 * @param response
	 * @param text
	 */
	protected void renderText(String text, HttpServletResponse res) {
		render(text, "text/plain;charset=UTF-8", res);
	}

/**
	 * 封装response用于json输出
	 * 
	 * @param text
	 * @param contentType
	 */
	protected void render(String text, String contentType,
			HttpServletResponse res) {
		try {
			res.setContentType(contentType);
			res.getWriter().write(text);
			res.getWriter().flush();
			res.getWriter().close();
		} catch (IOException e) {
			System.out.println(e);
		}
	}




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页