はじめに
* 以下のサイトで、簡単なMVCモデルを扱ってたので、勉強がてらにソースを組んでみる。http://ash.jp/java/webapp_hello.htm
http://ash.jp/java/webapp_scope.htm
構成
* Model : HelloWorldModel.java * View : HelloWorldView.jsp * Controller : HelloWorldController.java
使用している技術
useBeanタグについて
<jsp:useBean id="【id名】" type="【クラス名】" scope="【スコープ】" />
データの受け渡し(スコープ:Application/Session/Request)
Application
■値の設定 ServletContext servletContext = this.getServletConfig().getServletContext(); servletContext.setAttribute("【キー名】", 【値】);
Session
■値の設定 HttpSession session = request.getSession(); session.setAttribute("【キー名】", 【値】);
Request
http://blogs.yahoo.co.jp/dk521123/32776759.html■値の設定 request.setAttribute("【キー名】", 【値】);
サンプル
HelloWorldController.java
Controllerpackage com.controllers; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.models.HelloWorldModel; public class HelloWorldController extends HttpServlet { private static final long serialVersionUID = 1L; public HelloWorldController() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HelloWorldModel requestModel = new HelloWorldModel(); requestModel.setMessage("Hello World From Request!!"); request.setAttribute("requestBean", requestModel); HelloWorldModel sessionModel = new HelloWorldModel(); sessionModel.setMessage("Hello World From Session!!"); HttpSession session = request.getSession(); session.setAttribute("sessionBean", sessionModel); HelloWorldModel applicationModel = new HelloWorldModel(); applicationModel.setMessage("Hello World From Application!!"); ServletContext servletContext = this.getServletConfig().getServletContext(); servletContext.setAttribute("applicationBean", applicationModel); request.setCharacterEncoding("UTF-8"); RequestDispatcher dispatcher = request.getRequestDispatcher("HelloWorldView.jsp"); dispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
HelloWorldModel.java
Modelpackage com.models; public class HelloWorldModel implements Serializable { private String message; public HelloWorldModel () { this.message = null; } public String getMessage() { return this.message; } public void setMessage(String message) { this.message = message; } }
注意
* セッションに登録するObjectはSerializableであることhttp://konnichiwa-dou.cocolog-nifty.com/blog/2006/06/objectserializa_dd5d.html
http://magor.air-nifty.com/never_know/2005/12/serializable_6cff.html
http://www.wakhok.ac.jp/~tomoharu/web2004/text/index_c4.html
HelloWorldView.jsp
View<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <jsp:useBean id="requestBean" type="com.models.HelloWorldModel" scope="request" /> <jsp:useBean id="sessionBean" type="com.models.HelloWorldModel" scope="session" /> <jsp:useBean id="applicationBean" type="com.models.HelloWorldModel" scope="application" /> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World!</title> </head> <body> <h1>Request変数</h1> <%=requestBean.getMessage() %> <h1>Session変数</h1> <%=sessionBean.getMessage() %> <h1>Application変数</h1> <%=applicationBean.getMessage() %> </body> </html>
参考文献
http://www.atmarkit.co.jp/ait/articles/0301/09/news001_4.htmlhttp://www.javaroad.jp/servletjsp/sj_beans3.htm
http://ash.jp/java/webapp_hello.htm
http://ash.jp/java/webapp_scope.htm