JSP

#9.1 JSP 로깅

코딩클로스 2020. 5. 10. 22:10

웹 애플리케이션은 일반적으로 다수의 사용자가 접근하여 오랜 시간동안 사용하기 때문에 개인적으로 사용하는 애플리케이션보다 사용 도중 문제가 발생할 가능성이 높습니다. 따라서 이러한 문제 발생시 웹애플리케이션의 어느 부분에서 문제가 발생했는지 빨리 파악하는 것이 중요하게 됩니다.

 

index.jsp를 작성해보겠습니다.

 

LoggingServlet.java를 작성하겠습니다.

package ch09;

 

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

 

 

public class LoggingServlet extends HttpServlet {

 

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code>

* methods.

*

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

 

// 한글 인코딩 처리를 위하여 반드시 지정해야 함.

request.setCharacterEncoding("UTF-8");

String name = request.getParameter("name");

int age = Integer.parseInt(request.getParameter("age"));

 

Properties prop = System.getProperties();

prop.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");

prop.setProperty("org.apache.commons.logging.simplelog.defaultlog","info");

prop.setProperty("org.apache.commons.logging.simplelog.showlogname","true");

prop.setProperty("org.apache.commons.logging.simplelog.showdatename","true");

 

try{

Log log = LogFactory.getLog(LoggingServlet.class);

log.info(new Date() + ": User input : name = " + name + ", age = " + age);

log.fatal("fatal logging is possible");

log.error("error logging is possible");

log.trace("trace logging is possible");

log.warn("warn logging is possible");

log.debug("debug() can be used");

System.out.println("[System.out] System.out.println()을 사용하여 로깅 정보를 출력합니다.");

 

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet LoggingServlet</title>");

out.println("</head>");

out.println("<body>");

out.println("이름 = " + request.getParameter("name") + "<br/>");

out.println("나이 = " + request.getParameter("age") + "<br/>");

out.println("입력한 내용이 제대로 보이나요? 그렇다면 로깅 정보를 찾아보세요.");

out.println("</body>");

out.println("</html>");

} finally{

out.close();

}

 

}

 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

/**

* Handles the HTTP <code>GET</code> method.

*

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

 

/**

* Handles the HTTP <code>POST</code> method.

*

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

 

/**

* Returns a short description of the servlet.

*

* @return a String containing servlet description

*/

@Override

public String getServletInfo() {

return "Short description";

}// </editor-fold>

 

}

결과

'JSP' 카테고리의 다른 글

#10.1 JavaMail API (SimpleSMTP, POP 프로젝트) 메세지 전송  (0) 2020.05.27
#8.3 필터링 (대문자를 소문자로 변경)  (0) 2020.05.14
#8.2 필터링  (0) 2020.05.09
#8.1 JSP 웹 초기화  (0) 2020.05.08
#7.2 JSP Download 프로젝트  (0) 2020.05.07