import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Calendar; import java.sql.*; public class MonthView9 extends HttpServlet{ protected Connection conn = null; public void init() throws ServletException{ String url = "jdbc:mysql://localhost/servletschedule"; String user = "scheduleuser"; String password = "schedulepass"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, user, password); }catch (ClassNotFoundException e){ log("ClassNotFoundException:" + e.getMessage()); }catch (SQLException e){ log("SQLException:" + e.getMessage()); }catch (Exception e){ log("Exception:" + e.getMessage()); } } public void destory(){ try{ if (conn != null){ conn.close(); } }catch (SQLException e){ log("SQLException:" + e.getMessage()); } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html;charset=Shift_Jis"); PrintWriter out = res.getWriter(); int[] calendarDay; int count; int year; int month; int day = 1; calendarDay = new int[42]; /* 最大で7日×6週 */ count = 0; String param = req.getParameter("YEAR"); if (param == null || param.length() == 0){ year = -999; }else{ try{ year = Integer.parseInt(param); }catch (NumberFormatException e){ year = -999; } } param = req.getParameter("MONTH"); if (param == null || param.length() == 0){ month = -999; }else{ try{ month = Integer.parseInt(param); }catch (NumberFormatException e){ month = -999; } } /* パラメータが指定されていない場合は本日の日付を設定 */ if (year == -999 || month == -999){ Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DATE); }else{ if (month == 12){ month = 0; year++; } if (month == -1){ month = 11; year--; } } /* ユーザー情報を取り出す */ HttpSession session = req.getSession(false); Object tmp = session.getAttribute("username"); String username; if (tmp == null){ username = ""; }else{ username = (String)tmp; } tmp = session.getAttribute("userid"); int userid; if (tmp == null){ userid = 0; }else{ userid = Integer.parseInt((String)tmp); } tmp = session.getAttribute("roll"); String roll; if (tmp == null){ roll = ""; }else{ roll = (String)tmp; } StringBuffer sb = new StringBuffer(); sb.append(""); sb.append(""); sb.append("
"); sb.append(""); sb.append(""); sb.append(username); sb.append("さんのスケジュールです"); if (roll.equals("1")){ sb.append(" [ユーザーの追加]"); } sb.append(" [ログアウト]"); sb.append("
"); /* 日付データを配列に格納 */ count = setDateArray(year, month, day, calendarDay, count); /* 年月のリンク作成 */ sb.append(createMonthLink(year, month)); sb.append("日 | 月 | 火 | 水 | 木 | 金 | 土 |
"); sb.append(calendarDay[j] - 35); }else{ sb.append(" | "); sb.append(calendarDay[j]); } sb.append(" | "); } sb.append("
"); sb.append("前月 "); sb.append(year); sb.append("年"); sb.append(month + 1); sb.append("月 "); sb.append("翌月"); sb.append("
"); return (new String(sb)); } }