追記
スーパクラスの"userid"をサブクラスで利用したいです。
Java
protected int userid; protected String username; protected String roll; public CalenderAccess() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); /* ユーザー情報を取り出す */ HttpSession session = request.getSession(false); Object tmp = session.getAttribute("username"); if (tmp == null) { this.username = ""; } else { this.username = (String) tmp; } tmp = session.getAttribute("userid"); if (tmp == null) { this.userid = 0; } else { this.userid = Integer.parseInt((String) tmp); } tmp = session.getAttribute("roll"); if (tmp == null) { this.roll = ""; } else { this.roll = (String) tmp; }
Java
class CalendarBasic extends CalenderAccess { //途中省略 protected String ScehduleVew(int year, int month, int date) { System.out.println("userid=" + super.userid); String schedule = null; try { scheduleDao dao = new scheduleDao(); schedule = dao.scheduleVew(year, month, date, super.userid); } catch (SQLException e) { e.printStackTrace(); } return schedule; } }
いつもお世話になります。
スーパクラスのフィールド変数に値を代入し、サブクラスのメソッドで、スーパクラスのフィールド変数を利用しようとしていますが、代入した値が0になってしまします。
原因が見つからず困っている状況です。
ご教授頂けないでしょうか。
Java
package schedule; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class CalenderAccess */ @WebServlet("/schedule/CalenderAccess") public class CalenderAccess extends HttpServlet { private static final long serialVersionUID = 1L; protected int userid; protected String username; protected String roll; public CalenderAccess() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); /* ユーザー情報を取り出す */ HttpSession session = request.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; } System.out.println("Auserid" + userid); request.setAttribute("username", username); request.setAttribute("roll", roll); request.setAttribute("userid", userid); /* カレンダー作成 */ CalendarBasic.prepData(request); request.getRequestDispatcher("schedule/Calender.jsp").forward(request, response); } }
Java
package schedule; import java.sql.SQLException; import java.util.Calendar; import javax.servlet.http.HttpServletRequest; class CalendarBasic extends CalenderAccess { protected static void prepData(HttpServletRequest request) { // カレンダーの取得 Calendar cal = Calendar.getInstance(); // 年が設定されていれば、その値を取得。そうでなければ、今年の年号を入れる String param = request.getParameter("year"); System.out.println("19 year=" + param); if (param == null || param.length() == 0) { request.setAttribute("year", cal.get(Calendar.YEAR)); // 現在の年 } else { request.setAttribute("year", request.getParameter("year")); // 現在の年 } // 月が設定されていれば、その値を取得。そうでなければ、今の月を入れる param = request.getParameter("month"); System.out.println("27 Month=" + param); if (param == null || param.length() == 0) { request.setAttribute("month", cal.get(Calendar.MONTH) + 1); // 現在の月 } else { request.setAttribute("month", request.getParameter("month")); // 与えらられた月 } int year = Integer.parseInt(request.getAttribute("year").toString()); int month = Integer.parseInt(request.getAttribute("month").toString()); // 月初めの曜日(日-> 1) cal.set(year, month - 1, 1); int startday = cal.get(Calendar.DAY_OF_WEEK); // 月末の日付 cal.add(Calendar.MONTH, 1); cal.add(Calendar.DATE, -1); int lastday = cal.get(Calendar.DATE); // カレンダーのデータを作成する int date = 1; int maxday = 6 * 7; CalendarBasic cb = new CalendarBasic(); String ScehduleVew = cb.ScehduleVew(year, month, date); System.out.println("45 year=" + year); System.out.println("45 month=" + month); System.out.println("45 date=" + date); System.out.println("46 startday=" + startday); System.out.println("47 lastday=" + lastday); System.out.println("48 maxday=" + maxday); System.out.println("48 schedule=" + ScehduleVew); request.setAttribute("date", date); request.setAttribute("startday", startday); request.setAttribute("lastday", lastday); request.setAttribute("maxday", maxday); request.setAttribute("scheule", ScehduleVew); return; } protected String ScehduleVew(int year, int month, int date) { System.out.println("userid=" + super.userid); String schedule = null; try { scheduleDao dao = new scheduleDao(); schedule = dao.scheduleVew(year, month, date, super.userid); } catch (SQLException e) { e.printStackTrace(); } return schedule; } }
まだ回答がついていません
会員登録して回答してみよう