⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 framework.java

📁 招标投标网上系统
💻 JAVA
字号:
/*
 *  Source File Information
 * ----------------------------------------------------------------------------
 *  Source File Name : Framework.java
 *  Porject Name     : 
 *
 *  Last Change      : $Date: 2003-12-11 14:12 $
 *                     $Author:  $
 *                     $Revision: 10 $
 * ----------------------------------------------------------------------------
 */
package cn.com.syntc.webapp.framework;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import java.net.URL;
import java.net.URLEncoder;

import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.activation.FileTypeMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUtils;

import cn.com.syntc.webapp.session.UserSession;

/**
 * 这个servlet负责转向用户的请求。
 */
public class Framework
    extends HttpServlet
{
    //~ 静态变量/初始化 -----------------------------------------------------------------------------------
    private static String charset = System.getProperty("file.encoding", "GBK");

    /** 登录页面 */
    public static final String LOGINPAGE = "/login.jsp";

    /** 首页 */
    public static final String HOMEPAGE = "index.jsp";

    /** 模板文件名 */
    public static final String TEMPLATE = "template.jsp";

    /** 存放模板的根路径 */
    public static final String WEB_ROOT = "/WEB-INF/web/framework/";

    /** 当前系统是否允许匿名访问 */
    private static boolean anonymous = true;
    private static String absoluteRoot = null;

    //~ 方法 -----------------------------------------------------------------------------------------

    /**
     * servlet 初始化时执行的方法。
     *
     * @param config <!-- 请在这里添加参数描述 -->
     *
     * @throws ServletException <!-- 请在这里添加异常描述 -->
     */
    public void init(ServletConfig config)
              throws ServletException
    {
        super.init(config);
        absoluteRoot = getServletContext().getRealPath(WEB_ROOT);
    }

    /**
     * 处理用户端的请求。
     *
     * @param req <!-- 请在这里添加参数描述 -->
     * @param resp <!-- 请在这里添加参数描述 -->
     *
     * @throws ServletException <!-- 请在这里添加异常描述 -->
     * @throws IOException <!-- 请在这里添加异常描述 -->
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException
    {
        // 取得session
        HttpSession session = req.getSession(true);

        // 用户信息
        UserSession USession = (UserSession)session.getAttribute("UserSession");

        // 用户验证
        if (USession == null || USession.isGuest())
        {
          // 不允许游客登陆
          System.out.println( "未登录或停滞时间过久,请重新登陆!" );
          resp.sendRedirect(LOGINPAGE);
          return;
        } 

        // 分离出请求的目标页面。
        String pathinfo = req.getPathInfo();

        System.out.println("pathinfo="+pathinfo);

        // 只有网站参数,则转向修正URL
        if (pathinfo.indexOf(".jsp")==-1 && pathinfo.indexOf(".html")==-1 && pathinfo.indexOf(".htm")==-1 && !pathinfo.endsWith("/"))
        {
            resp.sendRedirect(pathinfo + "/" + HOMEPAGE);
            return;
        }

        req.setAttribute("target", pathinfo);

        String url = WEB_ROOT + TEMPLATE;
        req.getRequestDispatcher(url)
           .forward(req, resp);
    }

    private static String getContentType(String filename)
    {
        return FileTypeMap.getDefaultFileTypeMap()
                          .getContentType(filename);
    }

    private static String getFullRequestURL(HttpServletRequest request)
    {
        StringBuffer url = HttpUtils.getRequestURL(request);

        if (request.getQueryString() != null)
        {
            url.append("?")
               .append(request.getQueryString());
        }

        return getURLEncodeString(url.toString());
    }

    private static String getURLEncodeString(String str)
    {
        String encoded = str;

        try
        {
             str = URLEncoder.encode(str, "ISO-8859-1");
        }
        catch (UnsupportedEncodingException e) {}

        return str;
    }

    private static String[] split(String pathinfo)
    {
        if (pathinfo == null)
        {
            return new String[0];
        }

        if (pathinfo.startsWith("/"))
        {
            pathinfo = pathinfo.substring(1);
        }

        if (pathinfo.endsWith("/"))
        {
            pathinfo = pathinfo.substring(0, pathinfo.length() - 1);
        }

        if (pathinfo.length() == 0)
        {
            return new String[0];
        }

        int index = pathinfo.indexOf("/");

        if (index != -1)
        {
            String[] result = new String[2];
            result[0] = pathinfo.substring(0, index);
            result[1] = pathinfo.substring(index + 1);

            return result;
        }
        else
        {
            return new String[] { pathinfo };
        }
    }

    /**
     * 将一个指定的文件以HTTP协议送到客户端
     * 
     * @param file 指定的文件
     * @param response HttpServletResponse对象
     * @throws IOException <!-- 请在这里添加异常描述 -->
     */
    public static void sendfile(File file, HttpServletResponse response)
                         throws IOException
    {
        if (!file.exists())
        {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String filename = file.getName();
        String contentType = FileTypeMap.getDefaultFileTypeMap()
                                        .getContentType(filename);
        try
        {
            filename = new String(filename.getBytes(charset), "ISO-8859-1");
        }
        catch (UnsupportedEncodingException e) {}

        response.setContentType(contentType + "; name=" + filename);
        response.setHeader("Content-Disposition", "inline; filename=" + filename);

        InputStream is = new FileInputStream(file);
        OutputStream os = response.getOutputStream();
        int i;
        while ((i = is.read()) != -1)
        {
            os.write(i);
        }

        is.close();
        os.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -