实训第五天

前日回顾:

servlet + JSP 实现前台页面的增删查改;
request.getRquestDispatcher("").forward(request,response); 请求转发和 response.sendRedirect("") 的区别;
session ;

今日内容:

关于异步 Ajax

登陆验证:

ServletConfig 对象:

	<servlet>
		<servlet-name>ConfigServlet</servlet-name>
		<servlet-class>com.beiruan.web.servlet.ConfigServlet</servlet-class>
		<init-param>
			<param-name>sheng</param-name>
			<param-value>sichuan</param-value>
		</init-param>
		<init-param>
			<param-name>shi</param-name>
			<param-value>dazhou</param-value>
		</init-param>
		<init-param>
			<param-name>qu</param-name>
			<param-value>nanwai</param-value>
		</init-param>
	</servlet>
要访问 servletConfig 对象,只要从现有的servletConfig 对象 getServletConfig() 就可以,然后调用它的 getInitParameter() 方法就可以获取它的参数。
ServletConfig config = this.getServletConfig();
String sheng= config.getInitParameter("sheng");
...
ServletContext 对象:

可以用来活得数据库连接信息:

<context-param>
	<param-name>jdbcDriver</param-name>
	<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
	<param-name>jdbcUrl</param-name>
	<param-value>********</param-value>
</context-param>
<context-param>
	<param-name>jdbcUser</param-name>
	<param-value>root</param-value>
</context-param>
<context-param>
	<param-name>jdbcPassword</param-name>
	<param-value>123456r</param-value>
</context-param>
获得 ServletContext 对象的方法:

ServletContext application = this.getServletContext();
向 ServletContext 域对象存一个 data 属性:

application.setAttribute("imgs", imgs);
使用 ServletContext 的 getResourcePaths() 方法遍历图片集:

ContextServlet

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext application = this.getServletContext();
		Set<String> imgs = application.getResourcePaths("/images");
		application.setAttribute("imgs", imgs);
		request.getRequestDispatcher("/ShowPhoto.jsp").forward(request, response);
        }
ShowPhoto.jsp

  <body>
  	<c:forEach var="imgs" items="${imgs }" varStatus="i">
  	<c:if test="${i.index%5==0 }"><br /></c:if>
  	<c:if test="${i.index==0 }"><a href="http://www.baidu.com"><img src="<%=path %>/${imgs }" width="96dpi" height="96dpi" ></a></c:if>
	<c:if test="${i.index==9 }"><a href="http://www.sina.com"><img src="<%=path %>/${imgs }" width="96dpi" height="96dpi" ></a></c:if>
	<c:if test="${i.index==23 }"><a href="http://www.sina.com"><img src="<%=path %>/${imgs }" width="96dpi" height="96dpi" ></a></c:if>
	<c:if test="${i.index!=0&&i.index!=9&&i.index!=23 }"><img src="<%=path %>/${imgs }" width="96dpi" height="96dpi" ></c:if>
    </c:forEach>
  </body>

图片的第1 ,10 ,24 张设有链接。可以考虑做一个空间相册。

过滤器 Filter

实现一个接口,(包名.类名)

<filter>
	<filter-name>firstFilterServlet</filter-name>
	<filter-class>com.beiruan.web.servlet.FirstFilterServlet</filter-class>
	<init-param>
		<param-name>encode</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
</filter>
Filter 在服务器一启动就 init() ; 关闭时 destroy();

实现登陆拦截:

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request2 = (HttpServletRequest) request;
		HttpServletResponse response2 = (HttpServletResponse) response;
		request2.setCharacterEncoding(encode);
		//获取 session
		HttpSession session = request2.getSession();
		User loginuser= (User) session.getAttribute("user2");
		if (loginuser!=null) {
			chain.doFilter(request2, response2);//过滤器执行完了 该干嘛干嘛,不写的话它不会跳转到其他servlet
		}else {
			request2.getRequestDispatcher("/long.jsp").forward(request2, response2);
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值