在“易居住房10”的基础上进行增加或修改代码
过滤器拦截
“yiju”中添加“filter”包,在“filter”包中添加“LoginFilter.java”
“LoginFilter.java”代码如下
package com.yiju.filter;
import com.yiju.bean.UserInfo;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request= (HttpServletRequest) servletRequest;
HttpServletResponse response= (HttpServletResponse) servletResponse;
HttpSession session=request.getSession();
String path=request.getContextPath();
String url=request.getRequestURI();
UserInfo userInfo= (UserInfo) session.getAttribute("user");
if (userInfo==null && url.indexOf("login2.do")==-1
&& url.indexOf("findFourHouse.do")==-1
&& url.indexOf("main.jap")==-1
&& url.indexOf("login.jsp")==-1
&& url.indexOf("index.jsp")==-1
&& (!"/".equals(url))
){
response.sendRedirect(path+"/pages/login.jsp");
} else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
“web.xml”增加代码
<!-- 拦截 -->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.yiju.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
“UserController.java”增加代码
@RequestMapping("logout.do")
public String logout(HttpSession session){
session.removeAttribute("user");
return "redirect:../index.jsp";
}
修改“basehead.jsp”如下
<!--头部最上方的框-->
<div class="header">
<div class="width1190">
<div class="fl" style="font-size: 14px">您好,欢迎来到<a href="../index.jsp">易居住房信息平台!</a></div>
<div class="fr">
<% UserInfo userInfo= (UserInfo) request.getSession().getAttribute("user");%>
<% if (userInfo==null){%>
<a href="${pageContext.request.contextPath}/pages/login.jsp" style="font-size: 14px" target="_blank"><strong>登录</strong></a> |
<a href="${pageContext.request.contextPath}/pages/signup.jsp" style="font-size: 14px" target="_blank"><strong>注册</strong></a>
<%} else {%>
<a href="${pageContext.request.contextPath}/pages/personal.jsp" style="font-size: 14px" target="_blank"><strong>你好!${user.phone}</strong></a>
<%}%>
<a style="font-size: 14px">欢迎使用</a> |
<a href="${pageContext.request.contextPath}/pages/personal.jsp" style="font-size: 14px" target="_blank"><strong>个人中心</strong></a> |
<a href="${pageContext.request.contextPath}/user/" style="font-size: 14px"><strong>退出</strong></a> |
<a href="javascript:;" onclick="" style="font-size: 14px">加入收藏</a> |
<a href="javascript:;" onclick="" style="font-size: 14px">设为首页</a>
</div>
<div class="clears"></div>
</div><!--width1190/-->
</div>
<!--头部最上方的框-->
页面效果
未登录只显示首页内容,任意点击都会跳转到登录页面,登录成功后“登录|注册”变为“你好!手机号”,例如下图
图片轮播
在“main.jsp”的<head></head>
里的包后增加代码
<script type="text/javascript">
var picsArr=new Array();
picsArr[0]="images/lunbotu/fang1.jpg"
picsArr[1]="images/lunbotu/fang2.jpg"
picsArr[2]="images/lunbotu/fang3.jpg"
var index=0;
var timer=0;
window.onload=showPic;
function showNext() {
clearTimeout(timer);
showPic();
}
function showPic() {
if(index<(picsArr.length-1)){
index++;
}else {
index=0;
}
document.getElementById("pic").src=picsArr[index];
timer=setTimeout("showPic()",2000);
}
function showPre() {
showPrePic()
}
function showPrePic() {
if(index>0){
index--;
}else {
index=picsArr.length-1;
}
document.getElementById("pic").src=picsArr[index];
}
</script>
页面效果
首页图片每隔2秒自动轮播,点击左右键可分别跳到上一张,下一张