EclipseããGlassFishãèµ·åãã¦ãServletã¨EJBãåãã¨ãã¾ã§ããã
ç°å¢
ãã£ããã¨
Eclipseããglassfishèµ·åãã
ã¾ããServersãã¥ã¼ããGlassFishãµã¼ãã追å ã§ããããã«ããããã«ãGlassFish Toolsãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ãããHelp -> Eclipse Marketplaceãé¸æãã¦ãglassfishã¨ãã§æ¤ç´¢ããããã®ãã¨ã使ã£ã¦ãEclipseã®ãã¼ã¸ã§ã³ã«åããããã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ãããããã§ã¯Keplerãªã®ã§ãfor Keplerãé¸ã¶ã
Serversãã¥ã¼ããæ°ããServerè¨å®ã追å ãããä»åã¯4.0ã使ãã®ã§ããã¡ããé¸ã¶ã
GlassFish Server Directoryã«ã¯%GLASSFISH_ROOT%\glassfishãæå®ããã
ããã§èµ·åããã¦ãhttp://localhost:8080/ ã§åæãã¼ã¸ãè¦ããããhttp://localhost:4848 ã§ç®¡çã³ã³ã½ã¼ã«ãè¦ãããããã°OK
Mavenã§ããã¸ã§ã¯ããä½ã
ããã£ã¦ã¿ãã¨ãarchetypeã¯ã¨ããããwebapp-javaee7ã使ãã°ããããããã¨ãåããã
ã§ããã¾ä½¿ã£ã¦ãEclipseã®ããã©ã«ãã§ã¯ã½ã¬ãåºã¦ããªããã®ã§ãã¾ãArchetype Catalogã追å ãããããæ¹ã¯JavaEE7(GlassFish4.0)のEclipse4.3(Kepler)におけるMaven(M2E)連携 | あらぶるトラブル ãåèã«ãã¾ãããã¾ããWindow -> Preferences -> Maven -> Archtypes ãé¸ã¶ã
Add Remote Catalogãæ¼ãã¦ãCatalog Fileã« http://repo1.maven.org/maven2/archetype-catalog.xml ã¨å
¥åãã¦ãVefiryãæ¼ãã
æºåã§ããã®ã§New -> Maven Projectããããã¸ã§ã¯ããä½ã£ã¦ãããSelect an Archtypeã§webapp-javaee7ãé¸ã¶ã
ãããã¼ã«å
¥åããã
ããã¸ã§ã¯ããä½æãçµããã¨ãä¸è¨ã®ãããªã¨ã©ã¼ãåºãã
Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-dependency-plugin:2.6:copy (execution: default, phase: validate)
JavaEE7(GlassFish4.0)のEclipse4.3(Kepler)におけるMaven(M2E)連携 | あらぶるトラブルã«ããã¨Permanently mark goal copy in pom.xml as ignored in Eclipse build.ã®ã¯ã¤ãã¯ãã£ãã¯ã¹ãé¸ã¹ã°è¯ããã¨ã®ãã¨ã
ããã¸ã§ã¯ãã®æ§æã¯ãããªæããTestServletã¯ä¿ºã追å ãããã®ã ãã©ã
Servletãããã
ç°å¢ãåºæ¥ãã®ã§ãã¨ããããã¯Servletãä½ã£ã¦ã¿ãã
package glassfish4test; 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; @WebServlet("/hoge") public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hoge get"); } }
http://localhost:8080/glassfish4test/hoge ã§ãã©ã¦ã¶ã«hoge getã¨è¡¨ç¤ºãããã°OK
EJBãããã
ãã®ServletããEJBãå¼ãã§ã¿ãã
Wizardãããã®ã§ãã£ããä½ããNew -> EJBã§Session Beanãé¸ã¶ã
State typeã¯Statelessã«ãã¦ããã
ã§ãã¯ã©ã¹ãçæãããã®ã§ãããã«ãã£ãé©å½ãªã¡ã½ããã追å ããã
package glassfish4test; import java.util.Calendar; import javax.ejb.LocalBean; import javax.ejb.Stateless; /** * Session Bean implementation class TestEJB */ @Stateless @LocalBean public class TestEJB { /** * Default constructor. */ public TestEJB() { // TODO Auto-generated constructor stub } public String getHogeMessage() { return Calendar.getInstance().getTime() + " hoge"; } }
ãã£ãä½ã£ãServletãã¡ãã£ã¨ä¿®æ£ãããTestEJBãDIãããããã«ãã¦ãdoGetã®ä¸ã§EJBã®ã¡ã½ãããå¼ã¶ããã«ããã
@WebServlet("/hoge") public class TestServlet extends HttpServlet { @Inject private TestEJB testEJB; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hoge get:" + testEJB.getHogeMessage()); } }
ããã§ãã©ã¦ã¶ãã http://localhost:8080/glassfish4test/hoge ã«ã¢ã¯ã»ã¹ãã¦é©å½ãªæååã表示ãããã°OK