Logging with Java Servlets and IntelliJ

Servlet and Logging with IntelliJ Example


Adding a Servlet

Starting with a brand new project from the previous tutorial. We can add a servlet by creating a Class within the src folder.

Let’s call it mainServlet. This will create the mainServlet.java file and the initial

    /**
     * Created by ...
     */
    public class mainServlet {

    }

First of all we have to extend the class with HttpServlet and define a doGet method. After just typing the initial character the autocomplete kicks in and pressing enter fills out the method and imports the required modules.

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class mainServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,         IOException {

            resp.getWriter().println("Hello World");

    }
}

Next we specify the servlet class in the web.xml file that can be found under web->WEB-INF
by adding into web-app. We also specify the url-pattern it should be executed for.

 <servlet>
     <servlet-name>mainServlet</servlet-name>
     <servlet-class>mainServlet</servlet-class>
 </servlet>
 <servlet-mapping>
     <servlet-name>mainServlet</servlet-name>
     <url-pattern>/main</url-pattern>
 </servlet-mapping>


new_class

We are able to execute now and see the “Hello World” written on the http://localhost:8080/main page.

Adding Logging Package

Because most installations won’t come with the org.apache.commons.logging library, we have to import it. Which is actually pretty straight forward with IntelliJ, after the first try.

  • Open up “Project Structure” , File -> Project Structure
  • Select Libraries
    • Click on the Add + button
      • From Maven
      • Just type in logging and click the search icon
      • Once it’s done find and select commons-logging:commons-logging:1.1.2
      • Check Download to and keep the default folder.
      • Check Sources and JavaDocs
    • Now there will be a red error bar saying Artifact ’Tuts: … Library … required for… missing from the artifact…”
      • Click on the Light bulb and a menu will open up, now click add ‘commons…’ to the artifact.
      • Now we added the library to the build process and are done adding the Logger

addingthelibrary

Testing the Logger

To begin using the logger we just need to initiate it using
Log log = LogFactory.getLog(“Name”);
and we can begin logging using
log. [severe,fatal,error,warn,info,debug,trace]

import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;

public class mainServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.getWriter().println("Hello World");
    Log log = LogFactory.getLog("CS378.testing.Log");
    log.fatal("Testing out a fatal message");

    }
}

More information can be found http://commons.apache.org/proper/commons-logging/guide.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s