j2ee:servlet练习题短信项目,包含过滤器的使用、EL表达式和JSTL标准标签库的使用
1.过滤器的使用
package com.comm;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 MyFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("-----destroy()");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
System.out.println("-----doFilter()");
HttpServletRequest request = (HttpServletRequest)req;//封装客户端强求的是HttpServletRequest对象而不是ServletRequest,因此这里向下转型
HttpServletResponse response = (HttpServletResponse)resp;
HttpSession session=request.getSession();
Object obj=session.getAttribute("u");
List<String> path=new ArrayList<String>();
path.add("/index.jsp");
path.add("/userServlet");
String p=request.getServletPath();
//System.out.println("打印路径:"+p);
if(path.contains(p)){
chain.doFilter(request, response);
}else{
if(obj!=null){
chain.doFilter(request, response);
}else{
response.sendRedirect("index.jsp");
}
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("-----init()");
}
}
2.EL表达式和标准标签库的使用
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.dao.*,com.dao.impl.*,com.entity.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'msgList.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="style/style.css">
</head>
<body>
<%
Users u=(Users)session.getAttribute("u");
ArrayList<Message> mlist=(ArrayList<Message>)session.getAttribute("mlist");
%>
<div align="center">
<%-- 用户信息 --%>
<div style="width:500px">
当前用户${u.uname }
<a href="userServlet?op=exit">[登出]</a>
<a href="userServlet?op=tofasong">[给旧号码发短消息]</a>
<a href="post2.jsp">[给新号码发短消息]</a>
</div>
<h2 align="left" style="width:600px">我的短消息</h2>
<%-- 短消息列表 --%>
<div align="left" style="width:600px">
<form action="">
<table>
<tr>
<th><a href="javascript:document.myform.submit()">批量删除</a></th>
<th>内容</th>
<th>发送者</th>
<th>发送时间</th>
<th>状态</th>
<th>操作</th>
</tr>
<c:forEach var="b" items="${mlist}" varStatus="a">
<tr <c:if test="${a.index%2==0}">style="background-color: gray" </c:if>
<c:if test="${a.index%2!=0}"> style="background-color: #ffff00" </c:if>>
<td><input type="checkbox" name="cbox" value="${b.mid}"/></td>
<td>${b.mcontent}</td>
<td>${b.sendUname}</td>
<td>${b.mtime }</td>
<c:if test="${b.state eq 1}">
<td><img src="image/old.jpg"/></td><!-- 已读 -->
</c:if>
<c:if test="${b.state==0}">
<td><img src="image/new.jpg"/></td><!-- 未读 -->
</c:if>
<td>
<a href="messageServlet?op=seaByid&mid=${b.mid}">查看详情</a>
<a href="messageServlet?op=delmessage&mid=${b.mid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</form>
</div>
</div>
</body>
</html>