Webアプリ開発環境構築(Eclipse&Tomcat)とWARサンプルデプロイ 2018年5月20日 †一連の流れをメモした
サンプルコード1 †例として、http://bj006.comからHTTP GETした結果をそのまま表示するservletの動作確認用javaを以下に示す BenefitsServlet?.java †package com.dizzyworld; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; //import java.util.Enumeration; import java.net.*; public class BenefitsServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // ServletOutputStream out = response.getOutputStream(); // // String[] benefitNames = request.getParameterValues("benefit"); // // response.setContentType("text/html"); // for (String benefit : benefitNames) { // if (benefit.equals("vacation")) { // RequestDispatcher rd = request.getRequestDispatcher("company.jsp"); // rd.include(request, response); // continue; // } // if (benefit.equals("health")) { // RequestDispatcher rd = request.getRequestDispatcher("health.jsp"); // rd.include(request, response); // continue; // } // if (benefit.equals("vision")) { // RequestDispatcher rd = request.getRequestDispatcher("vision.jsp"); // rd.include(request, response); // continue; // } // if (benefit.equals("dental")) { // RequestDispatcher rd = request.getRequestDispatcher("dental.jsp"); // rd.include(request, response); // continue; // } // } // // out.print("<BR><A HREF='./welcome.html'>Back To Home Page</A><BR>"); PrintWriter out = response.getWriter(); URL url = new URL("http://bj006.com"); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("GET"); con.connect(); response.setContentType("text/html;charset=Shift_JIS"); BufferedReader reader = new BufferedReader(new InputStreamReader (con.getInputStream(), "JISAutoDetect")); String buffer = reader.readLine(); System.out.println("accessed"); while (null != buffer) { out.println(buffer); buffer = reader.readLine(); } } } サンプルコード2 †GetInformation?ボタンを押せばservletに処理が渡る。 BenefitsServlet?.java †package com.dizzyworld; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BenefitsServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); // ServletOutputStream out = response.getOutputStream(); PrintWriter out = response.getWriter(); String[] benefitNames = request.getParameterValues("benefit"); for (String benefit : benefitNames) { if (benefit.equals("vacation")) { RequestDispatcher rd = request.getRequestDispatcher("company.jsp"); rd.include(request, response); continue; } if (benefit.equals("health")) { RequestDispatcher rd = request.getRequestDispatcher("health.jsp"); rd.include(request, response); continue; } if (benefit.equals("vision")) { RequestDispatcher rd = request.getRequestDispatcher("vision.jsp"); rd.include(request, response); continue; } if (benefit.equals("dental")) { RequestDispatcher rd = request.getRequestDispatcher("dental.jsp"); rd.include(request, response); continue; } } out.print("<BR><A HREF='./welcome.html'>Back To Home Page</A><BR>"); } } 参考ページ †http://blogs.yahoo.co.jp/dk521123/34220519.html |