zoukankan      html  css  js  c++  java
  • Session保存用户名到Session域对象中

    Session保存用户名

    1.构造登录界面

    用户名:
    密   码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Session保存用户名</title>
    </head>
    <body>
    
    <form action="doS3" method= "post">
    	用户名:<input type="text" name="name"/><br/>
    	密   码:<input type="password" name = "pwd"><br/>
    	<input type="submit">
    </form>
    
    </body>
    </html>
    

      2.获取Session并将用户名保存到Session域对象中

    package com.oaec.session;
    
    import java.io.IOException;
    import java.util.Date;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class servletDemo3 extends HttpServlet {
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		req.setCharacterEncoding("UTF-8");
    		resp.setContentType("text/html;charset=UTF-8");
    		// req.getAttribute(arg0)
    		String name = req.getParameter("name");
    		String pwd = req.getParameter("pwd");
    		if ("高圆圆".equals(name) && "123".equals(pwd)) {
    			// 将用户名保存在session中
    			// 1.获得session
    			HttpSession session = req.getSession();
    			// 2.将用户名保存在session中
    			session.setAttribute("uname", name);
    			resp.sendRedirect("doS4");
    
    		} else {
    			resp.sendRedirect("index.html");
    		}
    	}
    
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		doGet(req, resp);
    	}
    }
    

      

    3.从Session中取出数据  并对页面进行保护  没有登录通过URL访问 直接重定向到登录界面  即主页

    package com.oaec.session;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class servletDemo4 extends HttpServlet{
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		req.setCharacterEncoding("UTF-8");
    		resp.setContentType("text/html;charset=UTF-8");
    		//从session中取出数据
    		HttpSession session = req.getSession(false);
    		Object object = null;
    		if (session != null && (object = session.getAttribute("uname"))!=null) {		
    				PrintWriter writer = resp.getWriter();
    				writer.write("登录成功<br>");
    				writer.write("欢迎你"+object);
    			}else {
    				//没有登录过 直接重定向到主页
    				resp.sendRedirect("index.html");
    			}
    		}
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		doGet(req, resp);
    	}
    }
    

      

  • 相关阅读:
    jQuery Easing 动画效果扩展--使用Easing插件,让你的动画更具美感。
    JavaScript表达式--掌握最全的表达式,一切尽在掌握中,让表达不再是难事
    JavaScript的格式--从格式做起,做最严谨的工程师
    JavaScript 简介--对javascript的初识,最基础的了解
    手机web页面制作时的注意事项
    实现像淘宝一样牛的语音搜索框
    Cufon在渲染网页字体你不知道的事
    .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?
    使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/john568300/p/6478634.html
Copyright © 2011-2022 走看看