解説動画
URLのパス情報を拾ってくると、引っ越しとかで便利ですよね。
■動画はこちら
■Youtube版の解説で使用しているソースコード
動画と一緒にこちらも参考にどうぞ。
1つめ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UrlTestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //パス情報の取得 // String scheme = request.getScheme(); String serverName = request.getServerName(); int serverPort = request.getServerPort(); String contextPath = request.getContextPath(); String servletPath = request.getServletPath(); String pathInfo = request.getPathInfo(); String queryString = request.getQueryString(); // //コンソール出力 // System.out.println("---------------------------------------------------"); System.out.println("scheme:" + scheme); System.out.println("serverName:" + serverName); System.out.println("serverPort:" + serverPort); System.out.println("contextPath:" + contextPath); System.out.println("servletPath:" + servletPath); System.out.println("pathInfo:" + pathInfo); System.out.println("queryString:" + queryString); System.out.println("---------------------------------------------------"); // //ブラウザ出力 // PrintWriter pw = response.getWriter(); pw.println("<html>"); pw.println("<body>"); //パス情報出力 pw.println("---------------------------------------------------<br>"); pw.println("scheme:" + scheme + "<br>"); pw.println("serverName:" + serverName + "<br>"); pw.println("serverPort:" + serverPort + "<br>"); pw.println("contextPath:" + contextPath + "<br>"); pw.println("servletPath:" + servletPath + "<br>"); pw.println("pathInfo:" + pathInfo + "<br>"); pw.println("queryString:" + queryString + "<br>"); pw.println("---------------------------------------------------<br>"); pw.println("</body>"); pw.println("</html>"); pw.flush(); pw.close(); } } |
2つめ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UrlTestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //パス情報の取得 // String scheme = request.getScheme(); String serverName = request.getServerName(); int serverPort = request.getServerPort(); String contextPath = request.getContextPath(); String servletPath = request.getServletPath(); String pathInfo = request.getPathInfo(); String queryString = request.getQueryString(); // //コンソール出力 // System.out.println("---------------------------------------------------"); System.out.println("scheme:" + scheme); System.out.println("serverName:" + serverName); System.out.println("serverPort:" + serverPort); System.out.println("contextPath:" + contextPath); System.out.println("servletPath:" + servletPath); System.out.println("pathInfo:" + pathInfo); System.out.println("queryString:" + queryString); System.out.println("---------------------------------------------------"); // //ブラウザ出力 // PrintWriter pw = response.getWriter(); pw.println("<html>"); pw.println("<body>"); //パス情報出力 pw.println("---------------------------------------------------<br>"); pw.println("scheme:" + scheme + "<br>"); pw.println("serverName:" + serverName + "<br>"); pw.println("serverPort:" + serverPort + "<br>"); pw.println("contextPath:" + contextPath + "<br>"); pw.println("servletPath:" + servletPath + "<br>"); pw.println("pathInfo:" + pathInfo + "<br>"); pw.println("queryString:" + queryString + "<br>"); pw.println("---------------------------------------------------<br>"); //画像を表示 pw.println("<img src=\"" + scheme + "://" + serverName + ":" + serverPort + contextPath + "/images/yurufuwa.png\">"); pw.println("</body>"); pw.println("</html>"); pw.flush(); pw.close(); } } |