Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- /**
- @author otoja
- */
- public class kadai extends HttpServlet {
- /**
- Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- methods.
- @param request servlet request
- @param response servlet response
- @throws ServletException if a servlet-specific error occurs
- @throws IOException if an I/O error occurs
- */
- String[] profile(String key){
- String [][] profile_list={
- {"ID:123456","1993/11/8生まれ","東京都練馬区"},
- {"ID:987654","2012/3/14生まれ","東京都三鷹市"},
- {"ID:192837","2017/6/17生まれ","東京都中野区"} };
- if(key.equals(profile_list[1])){
- return profile_list[1];
- }else if(key.equals(profile_list[0])){
- return profile_list[0];
- }else{
- return profile_list[2];
- }
- }
- /**
- * キーに該当するプロファイルを返す。
- * @param key 検索キー。
- * @return 見つかったプロファイル。該当しなければ null を返す。
- */
- static String[] searchProfile(String key) {
- final String[][] profileList = {
- {"ID:123456", "1993/11/8生まれ", "東京都練馬区"},
- {"ID:987654", "2012/3/14生まれ", "東京都三鷹市"},
- {"ID:192837", "2017/6/17生まれ", "東京都中野区"},
- };
- // 線形検索。検索時間は要素数に線形比例する。
- for (int i = 0; i < profileList.length; ++i) {
- if (key.equals(profileList[i][0])) {
- return profileList[i];
- }
- }
- return null; // 該当なし。
- }
- public static void main(String[] args) throws java.lang.Exception {
- String[] profile = searchProfile("ID:123456");
- if (profile == null) {
- System.out.println("Not found.");
- return;
- }
- for (int i = 1; i < profile.length; ++i) {
- System.out.println(profile[i]);
- }
- }
- public class MyServlet extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO: クライアントのリクエストに応じた処理を実装。
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- processRequest(request, response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- processRequest(request, response);
- }
- }
Add Comment
Please, Sign In to add comment