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

📄 skinutils.java

📁 products program INSTRACTION item
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *  <code>store(...)</code> methods. If <code>remove</code> is true, the
     *  value is also removed from persistence.
     *
     *  @param request The HttpServletRequest object, known as "request" on
     *      a JSP page.
     *  @param response The HttpServletRequest object, known as "response" on
     *      a JSP page.
     *  @param id The id or name of the stored value.
     */
    public static String retrieve(HttpServletRequest request,
            HttpServletResponse response, String id)
    {
        // First, check the session.
        HttpSession session = request.getSession();
        String value = (String)session.getAttribute(id);

        // if it's not found, check the cookies
        if (value == null) {
            Cookie cookie = getCookie(request, id);
            if (cookie != null) {
                value = cookie.getValue();
            }
            if (id != null && value != null) {
                session.setAttribute(id, value);
            }
        }
        return value;
    }

    /**
     * Deletes a user stored value. Values are set using the
     * <code>store(...)</code>
     * methods.
     *
     * @param request the HttpServletRequest object, known as "request" on
     *      a JSP page.
     * @param response the HttpServletRequest object, known as "response" on
     *      a JSP page.
     * @param id the id or name of the stored value you wish to remove from persistence.
     */
    public static void delete(HttpServletRequest request,
            HttpServletResponse response, String id)
    {
        // First, remove it from the session:
        HttpSession session = request.getSession();
        session.removeAttribute(id);

        // Invalidate the cookie by setting a null expired cookie in its place
        deleteCookie(request, response, id);
    }
    public static void remove(HttpServletRequest request,
            HttpServletResponse response, String id)
    {
        delete(request,response,id);
    }

    /**
     * Returns the specified cookie, or <code>null</code> if the cookie
     * does not exist.
     *
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the cookie.
     * @return the Cookie object if it exists, otherwise <code>null</code>.
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        // Return null if there are no cookies or the name is invalid.
        if(cookies == null || name == null || name.length() == 0) {
            return null;
        }
        // Otherwise, we  do a linear scan for the cookie.
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals(name) ) {
                return cookies[i];
            }
        }
        return null;
    }

    /**
     * Stores a value in a cookie. This cookie will persist for 30 days.
     *
     * @param request The HttpServletResponse object, known as "response" in a
     *      JSP page.
     * @param name a name to identify the cookie
     * @param value the value to store in the cookie
     */
    public static void saveCookie(HttpServletResponse response, String name,
            String value)
    {
        // Save the cookie value for 1 month
        saveCookie(response, name, value, 60*60*24*30);
    }

    /**
     * Stores a value in a cookie. This cookie will persist for the amount
     * specified in the <tt>saveTime</tt> parameter.
     *
     * @param request The HttpServletResponse object, known as "response" in a
     *      JSP page.
     * @param name a name to identify the cookie
     * @param value the value to store in the cookie
     * @param saveTime the time (in seconds) this cookie should live
     */
    public static void saveCookie(HttpServletResponse response, String name,
            String value, int saveTime)
    {
        // Check to make sure the new value is not null (appservers like Tomcat
        // 4 blow up if the value is null).
        if (value == null) {
            value = "";
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(saveTime);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * Builds a cookie string containing a loginname and password.<p>
     *
     * Note: with open source this is not really secure, but it prevents users
     * from snooping the cookie file of others and by changing the XOR mask and
     * character offsets, you can easily tweak results.
     *
     * @param loginname The loginname.
     * @param password The password.
     * @return String encoding the input parameters, an empty string if one of
     *      the arguments equals <code>null</code>.
     */
    private static String encodePasswordCookie(String loginname, String password)
    {
        StringBuffer buf = new StringBuffer();
        if (loginname != null && password != null) {
            byte[] bytes = (loginname + ENCODE_DELIMETER + password).getBytes();
            int b;

            for (int n = 0; n < bytes.length; n++) {
                b = bytes[n] ^ (ENCODE_XORMASK + n);
                buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
                buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
            }
        }
        return buf.toString();
    }

    /**
     * Unrafels a cookie string containing a loginname and password.
     * @param value The cookie value.
     * @return String[] containing the loginname at index 0 and the password at
     *      index 1, or <code>{ null, null }</code> if cookieVal equals
     *      <code>null</code> or the empty string.
     */
    private static String[] decodePasswordCookie( String cookieVal ) {

        // check that the cookie value isn't null or zero-length
        if( cookieVal == null || cookieVal.length() <= 0 ) {
            return null;
        }

        // unrafel the cookie value
        char[] chars = cookieVal.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        int b;
        for (int n = 0, m = 0; n < bytes.length; n++) {
            b = chars[m++] - ENCODE_CHAR_OFFSET1;
            b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
            bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n));
        }
        cookieVal = new String(bytes);
        int	pos = cookieVal.indexOf(ENCODE_DELIMETER);
        String loginname = (pos < 0) ? "" : cookieVal.substring(0, pos);
        String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);

        return new String[] {loginname, password};
    }
    
	public static Timestamp convertToTimestamp(String aDate, String aHour, String aMin){
		String[] date = new String[3];
		String temp_str = "-";
		
		date = StringUtils.split(aDate, temp_str);
		int year = Integer.parseInt(date[0]) - 1900;
		int month = Integer.parseInt(date[1])-1;
		int day = Integer.parseInt(date[2]);
		int hour = Integer.parseInt(aHour);
		int min = Integer.parseInt(aMin);
		//java.util.Calendar calendar = java.util.Calendar.getInstance();
		//calendar.set(temp_year,temp_month,temp_day,temp_hour,temp_min,0); 
		//calendar.getTime();
		//java.sql.Date myDate = new java.sql.Date(d.getTime());
		
		//return myDate ;

		Timestamp ts = new Timestamp(year, month, day, hour, min,0,0); 
		
		return ts ;
	}  
    public static java.sql.Date convertToDate(String aDate){
            String[] date = new String[3];
            String temp_str = "-";
        
            date = StringUtils.split(aDate, temp_str);
            int year = Integer.parseInt(date[0]) - 1900;
            int month = Integer.parseInt(date[1])-1;
            int day = Integer.parseInt(date[2]);
           
            //java.util.Calendar calendar = java.util.Calendar.getInstance();
            //calendar.set(temp_year,temp_month,temp_day,temp_hour,temp_min,0); 
            //calendar.getTime();
            //java.sql.Date myDate = new java.sql.Date(d.getTime());
        
            //return myDate ;

            java.sql.Date ts = new java.sql.Date(year, month, day); 
        
            return ts ;
        }      
	
	public static String timestampToString(Timestamp aTimestamp){
		String simpleString="";
		int year=aTimestamp.getYear()+1900;
		int month=aTimestamp.getMonth()+1;
		int day=aTimestamp.getDate();
		
		simpleString=year+"-"+month+"-"+day;
		return simpleString;
	}
    public static String timestampToLongString(Timestamp aTimestamp){
        String simpleString="";
        String min = "";
        String hours = "";
        int year=aTimestamp.getYear()+1900;
        int month=aTimestamp.getMonth()+1;
        int day=aTimestamp.getDate();
        int hour = aTimestamp.getHours();
        int mins = aTimestamp.getMinutes();
        
        if(mins<9){
            min = "0"+Integer.toString(mins);
        }else{
            min = Integer.toString(mins);
        }
        if(hour<9){
            hours = "0"+Integer.toString(hour);
        }else{
            hours = Integer.toString(hour);
        }
        simpleString=year+"-"+month+"-"+day+" "+hours+":"+min;
        return simpleString;
    }
	
	public static Timestamp dateToTimestamp(Date aDate){
		long ms=aDate.getTime();
		Timestamp ts=new Timestamp(ms);
		
		return ts;
	}
}

⌨️ 快捷键说明

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