Saturday, December 15, 2012

Adding user in MySQL and give grant


CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';

Backup database using PostgreSQL

pg_dump dbname | gzip > filename

Create servlet to serving Download


This tutorial shows how to create a link button on web for "Download". You will need have a servlet on the server side to provide this feature. In the end of this tutorial you will have a download link to download a text file.

Create a servlet

public class DownloadServlet extends HttpServlet {

 

  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    // Set the response content type

    response.setContentType("text/plain"); // for text file

 

    response.addHeader("Content-disposition","attachment; filename=yourfilename.txt");

 

    out.print("HELLO");

    out.close();

 

 }

 

}

Register servlet in web.xml

<web-app>

 

   <servlet>

      <servlet-name>DownloadServlet</servlet-name>

      <servlet-class>com.mycompany.DownloadServlet</servlet-class>

   </servlet>

 

<servlet-mapping>

      <servlet-name>DownloadServlet</servlet-name>

      <url-pattern>/download</url-pattern>

   </servlet-mapping>

</web-app>

Create HTML page

<a href="download">Go to download</a>


Create logging using log4j

Put log4j.properties in your classpath


#Set the level of the root loger to DEBUG and set its appender log4j.rootCategory=DEBUG, CONSOLE, DAILY log4j.logger.com.dappit.Dapper.parser=ERROR log4j.logger.org.w3c.tidy=FATAL
# CONSOLE - Console Appender log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender #set the layout for the appender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n # FILE - File Appender #log4j.appender.FILE=org.apache.log4j.FileAppender #log4j.appender.FILE.File=logs/jlego.log # Define the layout for file appender #log4j.appender.FILE.layout=org.apache.log4j.PatternLayout #log4j.appender.FILE.layout.conversionPattern=%m%n # DAILY - DailyRollingFile Appender log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender log4j.appender.DAILY.File=${catalina.home}/logs/mylog.log log4j.appender.DAILY.DatePattern='.'yyyy-MM-dd # Define the layout for DAILY file appender log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout log4j.appender.DAILY.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n # Rolling file appender in HTML #------------------------------------------------------------------------------ # # The following properties configure the Rolling File appender in HTML. # See http://logging.apache.org/log4j/docs/api/index.html for details. # #------------------------------------------------------------------------------ log4j.appender.HTML=org.apache.log4j.RollingFileAppender log4j.appender.HTML.File=${catalina.home}/logs/mylog.html log4j.appender.HTML.MaxFileSize=100KB log4j.appender.HTML.Append=false log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout log4j.appender.HTML.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n


Set this

log4j.logger.org.w3c.tidy=FATAL

Will cause org.w3c.tidy log only will shown if FATAL occurs