Created
May 29, 2014 05:25
-
-
Save daichan4649/e497bc61acd5544ff095 to your computer and use it in GitHub Desktop.
アクセスログ保存/一覧表示 (jQuery, JSON, JavaEE6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
| <title>Demo</title> | |
| <script src="js/jquery-2.1.0.js"></script> | |
| </head> | |
| <body> | |
| <ul id="contents"></ul> | |
| <script type="text/javascript"> | |
| $(document).ready(function() { | |
| loadIpList(); | |
| }); | |
| function loadIpList() { | |
| var URL = '/Demo/list'; | |
| $.getJSON(URL, | |
| {}, | |
| function(data) { | |
| for(var i in data) { | |
| $("#contents").append("<li>" + data[i].ip + "</li>"); | |
| } | |
| } | |
| ); | |
| }; | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Set; | |
| 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 test.Util.SessionKey; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| @WebServlet("/list") | |
| public class IpListServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| public IpListServlet() { | |
| super(); | |
| } | |
| @SuppressWarnings("unchecked") | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| Set<String> ipList = (Set<String>) getServletContext().getAttribute(SessionKey.IPLIST.getKey()); | |
| // toJSON | |
| try { | |
| List<IpData> dataList = new ArrayList<>(); | |
| for (String ipAddress : ipList) { | |
| IpData data = new IpData(); | |
| data.setIp(ipAddress); | |
| dataList.add(data); | |
| } | |
| Gson gson = new GsonBuilder().setPrettyPrinting().create(); | |
| String jsonText = gson.toJson(dataList); | |
| System.out.println("json=" + jsonText); | |
| response.setContentType("application/json;charset=utf-8"); | |
| response.setCharacterEncoding("UTF-8"); | |
| response.getWriter().write(jsonText); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | |
| IOException { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.io.IOException; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| 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 test.Util.SessionKey; | |
| @WebServlet("/save") | |
| public class IpSaveServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| public IpSaveServlet() { | |
| super(); | |
| } | |
| @Override | |
| public void init() throws ServletException { | |
| super.init(); | |
| // IPアドレスの一覧(重複不可) | |
| Set<String> ipList = new HashSet<>(); | |
| getServletContext().setAttribute(SessionKey.IPLIST.getKey(), ipList); | |
| } | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| HttpSession session = request.getSession(); | |
| final String sessionId = session.getId(); | |
| System.out.println(String.format("%s doGet", sessionId)); | |
| final String ipAddress = request.getRemoteHost(); | |
| // IP保存 | |
| saveIpAddress(ipAddress); | |
| // IP削除 | |
| // removeIpAddress(ipAddress); | |
| // 即切断 | |
| session.invalidate(); | |
| } | |
| private void saveIpAddress(String ipAddress) { | |
| // IP保存(Application単位で排他) | |
| synchronized (getServletContext()) { | |
| Util.saveIpAddress(getServletContext(), ipAddress); | |
| } | |
| } | |
| private void removeIpAddress(String ipAddress) { | |
| // IP削除(Application単位で排他) | |
| synchronized (getServletContext()) { | |
| Util.removeIpAddress(getServletContext(), ipAddress); | |
| } | |
| } | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | |
| IOException { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.Set; | |
| import javax.servlet.ServletContext; | |
| public class Util { | |
| public enum SessionKey { | |
| IPLIST("iplist"); | |
| private String key; | |
| SessionKey(String key) { | |
| this.key = key; | |
| } | |
| public String getKey() { | |
| return key; | |
| } | |
| } | |
| @SuppressWarnings("unchecked") | |
| public static void saveIpAddress(ServletContext context, String ipAddress) { | |
| System.out.println(String.format("%s SAVE", ipAddress)); | |
| Set<String> ipList = (Set<String>) context.getAttribute(SessionKey.IPLIST.getKey()); | |
| ipList.add(ipAddress); | |
| context.setAttribute(SessionKey.IPLIST.getKey(), ipList); | |
| } | |
| @SuppressWarnings({ "unchecked" }) | |
| public static void removeIpAddress(ServletContext context, String ipAddress) { | |
| System.out.println(String.format("%s REMOVE", ipAddress)); | |
| Set<String> ipList = (Set<String>) context.getAttribute(SessionKey.IPLIST.getKey()); | |
| ipList.remove(ipAddress); | |
| context.setAttribute(SessionKey.IPLIST.getKey(), ipList); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment