1) java web 表现层Action基类模版:
package org.springside.examples.miniweb.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* Action公共处理类
*
*
* @version $Revision: 1.9
* @Date: 2008/04/02 05:48:40
*/
public abstract class BasicActionSupport extends ActionSupport {
public static Logger logger = Logger.getLogger(BasicActionSupport.class);
/** 进行增删改操作后,以redirect方式重新打开action默认页的result名.*/
public static final String RELOAD = "reload";
/**
* 得到request
* @return
*/
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
* 得到response
* @return
*/
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* 取得web发布路径
* @return 容器路径
*/
public String getContextPath(){
return ServletActionContext.getServletContext().getRealPath("/");
}
/**
* 运用j2ee api将数据发给客户端(页面)
* @param content
* @throws IOException
*/
protected void sendClient(String content)
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.addHeader("pragma", "no-cache");
response.addHeader("cache-control", "no-cache,must-revalidate");
response.addHeader("expires", "0");
PrintWriter out;
try {
out = response.getWriter();
out.print(content);
out.flush();
out.close();
}
catch (IOException e) {
}
}
}
2) 系统登陆过滤器
package com.magnus.core.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;
public class SysUserFilter implements Filter {
private FilterConfig config;
/**
* 一个字符串是否包含字符数组中的字符串
*
* @param container
* @param regx
* @return
*/
public static boolean isContains(String container, String[] regx) {
boolean result = false;
for (int i = 0; i < regx.length; i++) {
if (container.indexOf(regx[i]) != -1) {
result = true;
}
}
return result;
}
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
// 当管理员登录后让所有请求全部通过
if (request.getSession().getAttribute("user_name") != null&&!"".equals(request.getSession().getAttribute("user_name"))){
chain.doFilter(req, res);
}
// 当管理员没有登录
else {
//但是属于例外则通过
String noincludes = (String) config.getInitParameter("noincludes");
String[] str = noincludes.split(";");
if ((isContains(request.getRequestURL().toString(), str))) {
chain.doFilter(req, res);
}
else{
if("".equals(request.getSession().getAttribute("user_name"))){
response.getWriter().write("<script>top.parent.location.href='"+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/index.html?other';</script>");
request.getSession().invalidate();
}
else{
response.getWriter().write("<script>top.parent.location.href='"+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"';</script>");
}
return;
}
}
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
}