登陆框

 登陆代码框!!!使用bootstrap框架,较为标准的登陆框

<div class="col-md-5">
				<div style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;">
					<font>会员登录</font>USER LOGIN
					<div>&nbsp;</div>
					<form class="form-horizontal" method="post" action="${pageContext.request.contextPath }/user">
						
						<input type="hidden" name="method" value="login">
						
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">用户名</label>
							<div class="col-sm-6">
								<input type="text" class="form-control" id="username" name="username"
									placeholder="请输入用户名">
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
							<div class="col-sm-6"> 
								<input type="password" class="form-control" id="inputPassword3" name="password"
									placeholder="请输入密码">
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-2 control-label">验证码</label>
							<div class="col-sm-3">
								<input type="text" class="form-control" id="inputPassword3"
									placeholder="请输入验证码">
							</div>
							<div class="col-sm-3">
								<img src="./image/captcha.jhtml" />
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<div class="checkbox">
									<label> <input type="checkbox" name="autoLogin" value="autoLogin"> 自动登录
									</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <label> <input
										type="checkbox"> 记住用户名
									</label>
								</div>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<input type="submit" width="100" value="登录" name="submit"
									style="background: url('./images/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
							</div>
						</div>
					</form>
				</div>
			</div>

常用代码:

request.getContextPath()

${pageContext.request.contextPath }

开发笔记:

https://blog.csdn.net/u010452388/article/details/80723550

这篇blog详细描述了baseservlet的使用,BaseServlet优点如下:因为BaseServlet可以帮我们解决代码冗余,

解决创建过多的Servlet,可以让我们开发的过程只需要注重业务实现,提高工作效率

runner.query 未查询到的返回值为null,并不会报错

在开发登陆的dao层,直接返回搜寻的结果,如果找不到则返回空,不报错。

QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where username=? and password=?";
        return runner.query(sql, new BeanHandler<User>(User.class), username,password);

 登陆服务,设置cookie:

if(user!=null){
			//鐧诲綍鎴愬姛
			//***************鍒ゆ柇鐢ㄦ埛鏄惁鍕鹃�変簡鑷姩鐧诲綍*****************
			String autoLogin = request.getParameter("autoLogin");
			if("autoLogin".equals(autoLogin)){
				//瑕佽嚜鍔ㄧ櫥褰�
				//鍒涘缓瀛樺偍鐢ㄦ埛鍚嶇殑cookie
				Cookie cookie_username = new Cookie("cookie_username",user.getUsername());
				cookie_username.setMaxAge(10*60);
				//鍒涘缓瀛樺偍瀵嗙爜鐨刢ookie
				Cookie cookie_password = new Cookie("cookie_password",user.getPassword());
				cookie_password.setMaxAge(10*60);

				response.addCookie(cookie_username);
				response.addCookie(cookie_password);

			}

			//***************************************************
			//灏唘ser瀵硅薄瀛樺埌session涓�
			session.setAttribute("user", user);

			//閲嶅畾鍚戝埌棣栭〉
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		}

通过session获取user属性来建立它的session域内容:

HttpSession session = request.getSession();

		User user = (User) session.getAttribute("user");
		
		ProductService service = new ProductService();
		//鏌ヨ璇ョ敤鎴风殑鎵�鏈夌殑璁㈠崟淇℃伅(鍗曡〃鏌ヨorders琛�)
		//闆嗗悎涓殑姣忎竴涓狾rder瀵硅薄鐨勬暟鎹槸涓嶅畬鏁寸殑 缂哄皯List<OrderItem> orderItems鏁版嵁
		List<Order> orderList = service.findAllOrders(user.getUid());
		//寰幆鎵�鏈夌殑璁㈠崟 涓烘瘡涓鍗曞~鍏呰鍗曢」闆嗗悎淇℃伅
		if(orderList!=null){
			for(Order order : orderList){
				//鑾峰緱姣忎竴涓鍗曠殑oid
				String oid = order.getOid();
				//鏌ヨ璇ヨ鍗曠殑鎵�鏈夌殑璁㈠崟椤�---mapList灏佽鐨勬槸澶氫釜璁㈠崟椤瑰拰璇ヨ鍗曢」涓殑鍟嗗搧鐨勪俊鎭�
				List<Map<String, Object>> mapList = service.findAllOrderItemByOid(oid);
				//灏唌apList杞崲鎴怢ist<OrderItem> orderItems 
				for(Map<String,Object> map : mapList){
					
					try {
						//浠巑ap涓彇鍑篶ount subtotal 灏佽鍒癘rderItem涓�
						OrderItem item = new OrderItem();
						//item.setCount(Integer.parseInt(map.get("count").toString()));
						BeanUtils.populate(item, map);
						//浠巑ap涓彇鍑簆image pname shop_price 灏佽鍒癙roduct涓�
						Product product = new Product();
						BeanUtils.populate(product, map);
						//灏唒roduct灏佽鍒癘rderItem
						item.setProduct(product);
						//灏唎rderitem灏佽鍒皁rder涓殑orderItemList涓�
						order.getOrderItems().add(item);
					} catch (IllegalAccessException | InvocationTargetException e) {
						e.printStackTrace();
					}
					
					
				}

			}

Session和Cookie的主要区别

  • Cookie是把用户的数据写给用户的浏览器。
  • Session技术把用户的数据写到用户独占的session中。 
  • Session对象由服务器创建,开发人员可以调用request对象的getSession方法得到session对象
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select i.count,i.subtotal,p.pimage,p.pname,p.shop_price from orderitem i,product p where i.pid=p.pid and i.oid=?";
List<Map<String, Object>> mapList = runner.query(sql, new MapListHandler(), oid);

 BeanUtils.populate应用理解:

Servlet层代码:

List<Map<String, Object>> mapList = service.findAllOrderItemByOid(oid);
				
for(Map<String,Object> map : mapList){
					
	try {
		OrderItem item = new OrderItem();
						//item.setCount(Integer.parseInt(map.get("count").toString()));
		BeanUtils.populate(item, map);
						
		Product product = new Product();
		BeanUtils.populate(product, map);
						
		item.setProduct(product);
						
		order.getOrderItems().add(item);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}



DAO层代码:

QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select i.count,i.subtotal,p.pimage,p.pname,p.shop_price from orderitem i,product p where i.pid=p.pid and i.oid=?";
List<Map<String, Object>> mapList = runner.query(sql, new MapListHandler(), oid);
return mapList;

可以得出,BeanUtils.populate的应用特别灵活,不要map映射一一对应,只要存在部分对应就能够
将部分封装到相应的对像中去

pageContext,request,session,application作用域范围从小到大:

pageContext                         

属性的作用范围仅限于当前jsp页面,在servlet中无法获取该对象

request

属性的作用范围仅限于同一个请求,主要用于请求转发

session

属性的作用范围仅限于一次会话,从浏览器打开直到关闭称为一次会话(在此期间会话没失效),在servlet中通过request对象获取

application

属性的作用范围限于当前WEB应用,是范围最大的属性作用范围,只要在一处设置属性,在其他各处的jsp或servlet中都可以获取到,在servlet中对应于ServletContext对象

发布了40 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41466575/article/details/93602613