프로그래밍/Java_JSP 게시판 만들기

Java_JSP 게시판 로그 남기기

개발계발게발 2021. 7. 13. 16:47
반응형

JSP 게시판 로그 남기기

 

log 테이블생성

 

 

 

Util (ip가져오기)

public class Util {
	//ip가져오기
	public static String getIP(HttpServletRequest request) {
		String ip = request.getHeader("X-FORWARDED");
		if(ip == null) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null) {
			ip = request.getHeader("WL-Proxy-Client_IP");
		}
		if(ip == null) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if(ip == null) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if(ip == null) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}

 

 

 

LogDAO

package com.knowhoon.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.knowhoon.db.DBConnection;
import com.knowhoon.dto.LogDTO;

public class LogDAO {
	public static void insertLog(LogDTO dto) {

		// conn
		Connection conn = DBConnection.dbConnection();
		//
		PreparedStatement pstmt = null;

		String sql = "INSERT INTO log (log_ip, log_taget, log_id, log_etc) VALUES (?, ?, ?, ?)";

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, dto.getLog_ip());
			pstmt.setString(2, dto.getLog_taget());
			pstmt.setString(3, dto.getLog_id());
			pstmt.setString(4, dto.getLog_etc());

			pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

 

LogDTO

package com.knowhoon.dto;

public class LogDTO {
	private int log_no;
	private String log_ip, log_date, log_taget, log_id, log_etc;
	public int getLog_no() {
		return log_no;
	}
	public void setLog_no(int log_no) {
		this.log_no = log_no;
	}
	public String getLog_ip() {
		return log_ip;
	}
	public void setLog_ip(String log_ip) {
		this.log_ip = log_ip;
	}
	public String getLog_date() {
		return log_date;
	}
	public void setLog_date(String log_date) {
		this.log_date = log_date;
	}
	public String getLog_taget() {
		return log_taget;
	}
	public void setLog_taget(String log_tager) {
		this.log_taget = log_tager;
	}
	public String getLog_id() {
		return log_id;
	}
	public void setLog_id(String log_id) {
		this.log_id = log_id;
	}
	public String getLog_etc() {
		return log_etc;
	}
	public void setLog_etc(String log_etc) {
		this.log_etc = log_etc;
	}	
}

 

jsp파일 에서 로그 남기기

ex) index.jsp

<%
String result = (String) request.getAttribute("result");
String ip = Util.getIP(request);
LogDTO dto = new LogDTO();
dto.setLog_ip(ip);
dto.setLog_taget("index");
if(session.getAttribute("id") != null){
	dto.setLog_id((String)session.getAttribute("id"));
}
LogDAO.insertLog(dto);
%>
<%@page import="com.knowhoon.dto.LogDTO"%>
<%@page import="com.knowhoon.dao.LogDAO"%>
<%@page import="com.knowhoon.util.Util"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
String result = (String) request.getAttribute("result");
String ip = Util.getIP(request);
LogDTO dto = new LogDTO();
dto.setLog_ip(ip);
dto.setLog_taget("index");
if(session.getAttribute("id") != null){
	dto.setLog_id((String)session.getAttribute("id"));
}
LogDAO.insertLog(dto);
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index.jsp</title>

</head>	
<body>
<%@ include file="./menu.jsp"%>

<%if(result == "3"){ %>
비밀번호 변경완료.<br>
다시 로그인해주세요.<br>
<%} %>

<%if(session.getAttribute("id") == null){ %>
<div id="loginbox">
	<div id="loginimg">
		<img alt="" src="./img/login.png">
	</div>
	<div id="logininput">

		<form action="./loginAction" method="post">
			<input type="text" name="id" maxlength="15" placeholder="아이디를 입력하세요"
				required="required"> <input type="password" name="pw"
				maxlength="20" placeholder="비밀번호를 입력하세요" required="required"><br>
			<button type="submit">LOGIN</button>
			<a href ="./join.jsp">가입하기</a>
			<a href ="./find_idpw.jsp">아이디/비밀번호 찾기</a>
			
		</form>
	</div>

</div>
<%} %>


</body>
</html>

 

servlet에서 로그 남기기

ex) LoginAction

 

BoardDTO dto 변수 중복으로 LogDTO dto1 변수 생성

 

String ip = Util.getIP(request);
LogDTO dto1 = new LogDTO();
dto1.setLog_ip(ip);
dto1.setLog_taget("login");
if(session.getAttribute("id") != null){
	dto1.setLog_id((String)session.getAttribute("id"));
}
dto1.setLog_etc(result.getId() + "로그인");
LogDAO.insertLog(dto1);
package com.knowhoon.web;

import java.io.IOException;

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 javax.servlet.http.HttpSession;

import com.knowhoon.dao.LogDAO;
import com.knowhoon.dao.LoginDAO;
import com.knowhoon.dto.LogDTO;
import com.knowhoon.dto.LoginDTO;
import com.knowhoon.util.Util;

@WebServlet("/loginAction")
public class LoginAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
      						//UUID
    public LoginAction() {
        super();
    }
    				//get방식 요청
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		LoginDAO dao = new LoginDAO();
		LoginDTO dto = new LoginDTO();
		dto.setId(id);
		dto.setPw(pw);
		LoginDTO result = dao.loginAction(dto);
	
		if(result != null && result.getName()!=null){
			HttpSession session = request.getSession();
			session.setAttribute("id", result.getId());
			session.setAttribute("name", result.getName());
			session.setAttribute("no", result.getNo());
			System.out.println(session.getAttribute("id"));
			System.out.println(session.getAttribute("name"));
			System.out.println(session.getAttribute("no"));
			response.sendRedirect("./board.jsp");

			String ip = Util.getIP(request);
			LogDTO dto1 = new LogDTO();
			dto1.setLog_ip(ip);
			dto1.setLog_taget("login");
			if(session.getAttribute("id") != null){
				dto1.setLog_id((String)session.getAttribute("id"));
			}
			dto1.setLog_etc(result.getId() + "로그인");
			LogDAO.insertLog(dto1);
			
		}else{
			System.out.println("로그인 해주세요");		
			response.sendRedirect("./index.jsp");
		}
	}
}
반응형