目录
1.mainPage的处理;
这里我们要把主页面的“日志列表”这一块要做处理;
由于每个日志列表的主页面显示的内容不一样,我们可以将日志列表这一块单独拿出来;
我们再写一个MainServlet来接受请求,将所得到的mainPage请求与diaryList相关联起来;
package com.java.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MainServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符集编码
request.setCharacterEncoding("utf-8");
//将得到的mainPage和diaryList连接起来
request.setAttribute("mainPage", "diary/diaryList.jsp");
//内部转发
request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
}
}
还有diaryList.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/images/list_icon.png"/>
日记列表</div>
</div>
</body>
</html>
最后我们还要配置一下web.xml;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Diary</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.java.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>com.java.web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
</web-app>
总之,整个工作流程是:
1.我们访问login,跳到LoginServlet,登录成功后,服务器内部跳转到main;
2.接受main请求后,跳到MainServlet,然后显示mainPage对应的diaryList.jsp页面,最后服务器内部转发到mainTemp.jsp主页面;
2.用户请求过滤功能;
在这里我们实现一下Servlet的过滤器功能:
我们重新建个包,里面写个LoginFilter类:
package com.java.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
//获取resquest,response
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
//获取session
HttpSession session=request.getSession();
//获取当前的用户信息
Object object=session.getAttribute("currentUser");
//获取正确的用户登录途径
String path=request.getServletPath();
//进行判断,若是object为null,则进行拦截,也即返回登录界面
if(object==null&&path.indexOf("login")<0&&path.indexOf("bootstrap")<0&&path.indexOf("images")<0){
response.sendRedirect("login.jsp");
}else{
//反之则重复进行验证,利用递归的思想
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
再进行一下web.xml配置:
最后测试,如果输入非法请求,系统就会自动跳转到登录页面login.jsp上面去了。