サーブレットとviewページを作成します。
Client.java
package com.mydomain.ejb; import java.io.IOException; import java.util.*; import java.util.regex.Pattern; import javax.ejb.EJB; import javax.servlet.*; import javax.servlet.http.*; public class Client extends HttpServlet { @EJB private Action action; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); try { this.action.delete(); String strRollback = req.getParameterMap().containsKey("rollback") ? req.getParameter("rollback").toString() : "0"; int rollback = Pattern.matches("[^0-9]", strRollback) ? 0 : Integer.parseInt(strRollback); try { this.action.regist(session.getId(), rollback); } catch(Exception exp){} req.setAttribute("messages", this.action.getMessages()); this.setMethodList(req, rollback); } catch(Exception ex) { session.invalidate(); ex.printStackTrace(); System.out.println("webclient servlet test failed"); throw new ServletException(ex); } } protected void setMethodList(HttpServletRequest req, int rollback) { PageData pageData = new PageData(); List<Map<String,String>> methodList = pageData.getMethodList(); req.setAttribute("methodList", methodList); List<String> rollbacks = new ArrayList<String>(); for(int ct=0; ct<methodList.size(); ct++) { rollbacks.add((rollback == ct) ? "checked" : ""); } req.setAttribute("rollbacks", rollbacks); } }PageData.java
package com.mydomain.ejb; import java.io.Serializable; import java.util.*; public class PageData implements Serializable { private static final long serialVersionUID = 1L; public PageData(){} public List<Map<String,String>> getMethodList() { List<Map<String,String>> methods = new ArrayList<Map<String,String>>(); String spc = " "; String pls = spc + spc + spc + "+"; this.addMethodRow(methods, "", ""); this.addMethodRow(methods, "+->regist( )", pls); this.addMethodRow(methods, spc + "+->decorateInsert( )", pls); this.addMethodRow(methods, spc + "+->insert( )", pls); this.addMethodRow(methods, spc + " +->decorateInsert( )", pls); this.addMethodRow(methods, spc + "+--------------------------", "--->decorateInsert( )"); this.addMethodRow(methods, spc + "+--------------------------", "--->insert( )"); this.addMethodRow(methods, "", spc + " +->decorateInsert( )"); this.addMethodRow(methods, spc + "+--------------------------", "--->requiresNewInsert( )"); this.addMethodRow(methods, "", spc + " +->insert( )"); this.addMethodRow(methods, "", spc + spc + "+->decorateInsert( )"); return methods; } protected void addMethodRow(List<Map<String,String>> methods, String act, String sub) { Map<String,String> row = new HashMap<String,String>(); row.put("act", act); row.put("sub", sub); row.put("rollback", Integer.toString(methods.size())); methods.add(row); } }index.jsp
<%@ page language="java" pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="java.util.*,com.mydomain.ejb.*" %> <jsp:include page="/servlet" /> <% List<String> rollbacks = (List<String>)request.getAttribute("rollbacks"); List<Map<String,String>> methodList = (List<Map<String,String>>)request.getAttribute("methodList"); %> <html> <head> <title>Access Log</title> <style type="text/css"> table.list, table {font-size: 9pt;} table.list th {background:#FFCC66;} table.list td {background:#FFFFCC} </style> </head> <body> <form method="post"> <table> <tr> <td> <table style="border: solid 1px;"> <tr> <th>No.</th> <th></th> <th align="left" style="text-decoration: underline;">ActionImpl</th> <th style="text-decoration: underline;">SubActionImpl</th> </tr> <% for(int ct=1; ct<rollbacks.size(); ct++){ %> <tr> <td><%= ct %></td> <td align="right"> Rollback <input type="radio" name="rollback" value="<%= ct %>" <%= rollbacks.get(ct) %>> </td> <td><%= methodList.get(ct).get("act") %></td> <td><%= methodList.get(ct).get("sub") %></td> </tr> <% } %> <tr> <td></td> <td align="right"> Commit <input type="radio" name="rollback" value="0" <%= rollbacks.get(0) %>> </td> <td colspan="3"></td> </tr> </table> <input type="submit" value="access"> </td> </tr> <tr> <td> <table class="list"> <tr> <th>No.</th> <th>Session ID<br /></th> <th>Class:Method</th> </tr> <% for(Message row : (List<Message>)request.getAttribute("messages")){ %> <tr> <td><%= row.getNo() %></td> <td><%= row.getText() %></td> <td><%= row.getBiko() %></td> </tr> <% } %> </table> </td> </tr> </table> </form> </body> </html>
web.xmlファイルを作成します。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>com.mydomain.ejb.Client</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> </web-app>
サーブレットで@EJBアノテーションを使用するためServlet2.5のスキーマを宣言します。