daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. /**
  9.  
  10. @author otoja
  11. */
  12. public class kadai extends HttpServlet {
  13. /**
  14. Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  15. methods.
  16.  
  17. @param request servlet request
  18. @param response servlet response
  19. @throws ServletException if a servlet-specific error occurs
  20. @throws IOException if an I/O error occurs
  21. */
  22. String[] profile(String key){
  23.     String [][] profile_list={
  24.         {"ID:123456","1993/11/8生まれ","東京都練馬区"},
  25.         {"ID:987654","2012/3/14生まれ","東京都三鷹市"},
  26.         {"ID:192837","2017/6/17生まれ","東京都中野区"} };
  27.     if(key.equals(profile_list[1])){
  28.         return profile_list[1];
  29.     }else if(key.equals(profile_list[0])){
  30.         return profile_list[0];
  31.     }else{
  32.         return profile_list[2];
  33.     }
  34. }
  35.    
  36. /**
  37.  * キーに該当するプロファイルを返す。
  38.  * @param key 検索キー。
  39.  * @return 見つかったプロファイル。該当しなければ null を返す。
  40.  */
  41. static String[] searchProfile(String key) {
  42.     final String[][] profileList = {
  43.         {"ID:123456", "1993/11/8生まれ", "東京都練馬区"},
  44.         {"ID:987654", "2012/3/14生まれ", "東京都三鷹市"},
  45.         {"ID:192837", "2017/6/17生まれ", "東京都中野区"},
  46.     };
  47.     // 線形検索。検索時間は要素数に線形比例する。
  48.     for (int i = 0; i < profileList.length; ++i) {
  49.         if (key.equals(profileList[i][0])) {
  50.             return profileList[i];
  51.         }
  52.     }
  53.     return null; // 該当なし。
  54. }
  55.  
  56. public static void main(String[] args) throws java.lang.Exception {
  57.     String[] profile = searchProfile("ID:123456");
  58.     if (profile == null) {
  59.         System.out.println("Not found.");
  60.         return;
  61.     }
  62.     for (int i = 1; i < profile.length; ++i) {
  63.         System.out.println(profile[i]);
  64.     }
  65. }
  66.    
  67. public class MyServlet extends HttpServlet {
  68.  
  69.     protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  70.         // TODO: クライアントのリクエストに応じた処理を実装。
  71.     }
  72.  
  73.     @Override
  74.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  75.         processRequest(request, response);
  76.     }
  77.  
  78.     @Override
  79.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  80.         processRequest(request, response);
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top