session和cookie的面试题(通常在网站和管理系统的登陆中会用到)

除了书中说的基本概念之外呢!要是在面试的时候将你做的项目和概念结合起来,一起用的话,基本上这部分就OK了!
session和cookie的概念我给置顶了,可以去看我得博客!
在这里插入图片描述
前台传数据(struts2)到action中,HttpServletRequest和HttpServletResponse是和HTTP有关,前者是获取HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
具体了解:参考HttpServletRequest的参考资料
HttpServletResponse是Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。
request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了。
具体了解:参考HttpServletResponse的参考资料
HttpSession:
具体参考:参考HTTPSession的参考资料
知道了,这三个概念之后呢,我粘贴一段代码:验证码的后台代码,这两个方法是同一个类里面的,都是继承于上面的那个类(验证码的这段代码,可以直接拿去开发用)

public void ajax_emp_getImage() throws Exception{
		 //System.out.println("####################生成字母和数字得验证码########################");  
	        BufferedImage img = new BufferedImage(68, 22,  
	        BufferedImage.TYPE_INT_RGB);  
	        Graphics g = img.getGraphics(); 
	        Random r = new Random();  
	        Color c = new Color(200, 150, 255);  
	        g.setColor(c);  
	        g.fillRect(0, 0, 68, 22); 
	        StringBuffer sb = new StringBuffer();  
	        char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();  
	        int index, len = ch.length;  
	        for (int i = 0; i < 4; i++) {  
	  
	            index = r.nextInt(len);  
	  
	            g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt  
	  
	            (255)));  
	  
	            g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));  
	            g.drawString("" + ch[index], (i * 15) + 3, 18);  
	            sb.append(ch[index]);  
	  
	        }  
	        //注意这里,给session附上了值之后,就有一个名为piccode的属性
	        request.getSession().setAttribute("piccode", sb.toString());  
	        ImageIO.write(img, "JPG", response.getOutputStream());  
	        
	}

public String emp_login() {
		
		ActionContext context = ActionContext.getContext();
		HttpSession session2 = request.getSession();
		//这里可以看到,任何一个地方的代码用session可以获取到piccode
		String rightCap=(String)session2.getAttribute("piccode");
		//如果登陆失败,将会重新进入到登陆页面,并提示错误信息
		if(!StringUtils.endsWithIgnoreCase(rightCap,captcha)){
			context.put("tip","caperror");
			return LOGIN;
		}
		//这里也是,将会提示出错误的信息
		String newPass = MD5Utils.md5(emp.getPassword());
		Emp emp2 = empService.getEmpByUnameAndPWord(emp.getUsername(),emp.getPassword());
		if(emp2 == null){
			context.put("tip","userpasserror");
		}
		//这一步将用户的信息,放入session中,其实,只要上面的信息没错误了,就可以跳转页面了
		Map<String,Object> session = context.getSession();
		session.put("user",emp2);
		return super.MAIN;
	}
粗略的看一下,前台比较不专业的代码,(哈哈哈哈哈)!
<!--设置隐藏域得作用就是取到从后台传入的tip的值"#tip"  -->
	<input type="hidden" id="tip" value="<s:property value="#tip"/>">
	<div id="tipDiv" style="text-align: center;border: 1px solid #FFC2DB;width: 180px;background:#FFE791; color: red;display:none;"> </div>
	 <!--JS代码-->
$(function() {
		$("#login_ok").click(function() {
			$("form:first").submit();
		});
		
		if(top.localtion.href != localtion.href){
			top.localtion.href = localtion.href;
		}
			/* 用于刷新验证码 */
			$("#captchaImg").click(function (){
				var srcPath = "${path}/ajax_emp_getImage?date="+new Date();
				$(this).attr("src",srcPath);
			})
			
			var tip = $("#tip").val();
			if(tip == "caperror"){
				$("#tipDiv").show(300);
				$("#tipDiv").html("验证码错误");
			}if(tip == "userpassword"){
				$("#tipDiv").show(300);
				$("#tipDiv").html("用户名或者密码错误");
			}
		
	});
	function MM_swapImage(srcObj,image_src){
		srcObj.src=image_src;
	}			
<!--前端就可以在隐藏域中显示出错误信息,没有的话,就可以直接跳转到主界面即可-->

面试官看着你的简历问:
session是干嘛的,你就将session和cookie的概念一说,然后,在项目中,主要用于登陆使用,保存用户的信息,剩下的自己发挥一下就好了!(我目前的理解,就是实际用到的session和概念中的session的作用关系感觉不大,近期会更新这篇文章,对session还没有理解到精髓)

猜你喜欢

转载自blog.csdn.net/weixin_39638459/article/details/86516379
今日推荐