package zall; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import zutil.db.DBConnection; import zutil.log.LogUtil; public class Zallery extends HttpServlet{ private static Logger logger = LogUtil.getLogger(); public static final String VERSION = "2.0.0"; public static String WEBSITE_NAME = ""; public static String WEBSITE_URL = ""; public static String ADMIN_EMAIL = ""; public static String ADMIN_EMAIL_NICE = ""; public static String SMTP_HOST = ""; public static String ROOT_PATH = ""; public static String DATA_PATH = ""; /** * Config Options: *
- WEBSITE_NAME *
- WEBSITE_URL *
- SMTP_HOST *
- DATA_PATH */ public void init(ServletConfig config) throws ServletException { super.init(config); // java:comp/env ROOT_PATH = config.getServletContext().getRealPath("/"); try { Context context = new InitialContext(); // Check if Zallery has been properly configured if ("PATH TO DATA FOLDER".equals(context.lookup("java:comp/env/DATA_PATH"))) throw new ServletException("Zallery has not been properly configured, set proper configuration in Zallery.xml context file."); WEBSITE_NAME = (String)context.lookup("java:comp/env/WEBSITE_NAME"); WEBSITE_URL = (String)context.lookup("java:comp/env/WEBSITE_URL"); if( WEBSITE_URL.charAt(WEBSITE_URL.length()-1) != '/') WEBSITE_URL += "/"; ADMIN_EMAIL = (String)context.lookup("java:comp/env/ADMIN_EMAIL"); ADMIN_EMAIL_NICE = (String)context.lookup("java:comp/env/ADMIN_EMAIL_NICE"); SMTP_HOST = (String)context.lookup("java:comp/env/SMTP_HOST"); DATA_PATH = (String)context.lookup("java:comp/env/DATA_PATH"); LogUtil.setLevel("zall", Level.FINEST); //LogUtil.setLevel("zutil", Level.FINEST); } catch (NamingException e) { throw new ServletException(e); } } public void destroy(){ } public static DBConnection getDB() throws ServletException{ try { return new DBConnection("jdbc/mysql"); } catch (Exception e) { throw new ServletException(e); } } public static String getCookieValue(Cookie[] cookies, String name) { if( cookies == null ) return null; for(Cookie cookie : cookies) { if ( name.equals(cookie.getName()) ) return cookie.getValue(); } return null; } // /mywebapp/servlet/MyServlet/a/b;c=123?d=789 public static String getUrl(HttpServletRequest req) { String reqUri = req.getRequestURI().toString(); String queryString = req.getQueryString(); // d=789 if (queryString != null) { reqUri += "?"+queryString; } return reqUri; } }