web学习03

获取表单提供的数据

html form表单

用户名:

密码:

篮球 唱歌 画画

                response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String[] hobbys = request.getParameterValues("hobby");
		String city = request.getParameter("city");
		System.out.println(name);
		System.out.println(password);
		System.out.println(sex);
		for (String string : hobbys) {
			System.out.println(string);
		}
		
		System.out.println(city);

把请求中的参数放到map中

            Map map = request.getParameterMap();
		for (String string  : map.keySet()) {
			System.out.println(string);
			for (String string2 :map.get(string)) {
				System.out.println(string2);
			}
		}
             Enumeration names = request.getParameterNames();
		while (names.hasMoreElements()) {
			System.out.println(names.nextElement());
		}

将表单提交的数据封装到user对象中

                String name = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String[] hobbys = request.getParameterValues("hobby");
		String city = request.getParameter("city");
		User user=new User(name,password,sex,hobbys,city);

 请求转发

一次请求 并且响应到网页的是转发后的响应结果

 请求重定向

两次请求 超出了request域的作用范围  不能使用request传值

 请求包含

几次请求? 一次请求(说明可以用request域传值)

 最终响应到网页的内容是什么? 

  两个servlet的响应结果会一起返回给浏览器


Request域对象

内部维护了一个map集合 添加获取删除值

作用范围:只对一次请求有效

请求转发 通过请求对象获取请求转发器

                RequestDispatcher dispatcher = request.getRequestDispatcher("/demo03");
		dispatcher.forward(request, response);

请求重定向 参数是重定向的地址

response.sendRedirect("/sh-web-03/demo03");

请求包含  获取请求转发器

                RequestDispatcher dispatcher = request.getRequestDispatcher("/demo03");
		dispatcher.include(request, response);

会话技术 客户端与服务端之间的交互

 什么是会话?相当于打电话

 访问网页时只要浏览器不关闭 点击多次连接也叫一次会话

 关闭浏览器 结束会话

 1.客户端技术 Cookie 保存客户端(浏览器中) 是一个文件

 每个站点(网站)可以保存20个cookie

 每个浏览器可以保存300个cookie

 每个cookie可以保存4kb的数据

 cookie默认的存活时间 :一次会话

 关闭浏览器 cookie被清除 默认保存在浏览器的内存中

 可以设置cookie的存活时间 1年 

 浏览器会将cookie保存到硬盘中(保存到文件中)

 注意:cookie不能保存中文

  

 设置cookie存活时间

 setMaxAge(时间 以秒为单位)

 正值 值是多少 浏览器保存多少秒

 负值 浏览器不会存储cookie到硬盘当中

     当浏览器关闭 会话结束 cookie被删除

 0时 cookie被删除

 

2.服务端技术 HttpSession

 session保存在服务端

需求: 显示上一次(最后一次)的登录时间

 关闭浏览器之后再访问还能显示 

 1.第一下访问servlet时 将时间保存到cookie中

 在响应会浏览器 让浏览器存储起来

 下一次访问时通过request请求获取之前保存的cookie 

 显示之前储存的额时间

public class Demo04 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		Cookie[] cookies = request.getCookies();
		if (cookies!=null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("lastTime")) {
					String value = cookie.getValue();
					long time = Long .parseLong(value);
					Date date=new Date(time);
					SimpleDateFormat dateFormat=
							new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss");
					String lastTime = dateFormat.format(date);
					System.out.println("上次的登录时间"+lastTime);
					response.getWriter().write("上次的登录时间"+lastTime);
				}
			}
		}
		
		Cookie cookie=new Cookie("lastTime", System.currentTimeMillis()+"");
		cookie.setPath("/");
		cookie.setMaxAge(60*5);
		response.addCookie(cookie);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
public class Demo05 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
				Cookie[] cookies = request.getCookies();
				if (cookies!=null) {
					for (Cookie cookie : cookies) {
						if (cookie.getName().equals("lastTime")) {
							String value = cookie.getValue();
							long time = Long .parseLong(value);
							Date date=new Date(time);
							SimpleDateFormat dateFormat=
									new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss");
							String lastTime = dateFormat.format(date);
							System.out.println("上次的登录时间"+lastTime);
							response.getWriter().write("上次的登录时间"+lastTime);
						}
					}
				}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

登录后下次打开浏览器  还能记录用户名

public class Demo06 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String username="";
		String remember="";
		Cookie[] cookies = request.getCookies();
		if (cookies!=null) {
			System.out.println("123");
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("username")) {
				username=cookie.getValue();
				remember="checked='checked'";
				}
			}
		}
		System.out.println(username);
		
		response.getWriter().write("

"); response.getWriter().write("用户名:
"); response.getWriter().write("密码:
"); response.getWriter().write("记住:"); response.getWriter().write(""); response.getWriter().write("
"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class Demo07 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String remember = request.getParameter("remember");
		Cookie cookie=new Cookie("username", username);
		cookie.setPath("/sh-web-03/servlet");
		if (username.equals("wanglong")&&password.equals("123")) {
			if (remember!=null) {
				cookie.setMaxAge(Integer.MAX_VALUE);
				
			}else {
				cookie.setMaxAge(0);
			}
			response.getWriter().write("登录成功");
			response.addCookie(cookie);
		}else {
		response.getWriter().write("登录失败");
		response.setHeader("refresh", "3;url=/sh-web-03/servlet/demo06");
		    
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}












你可能感兴趣的:(web学习03)