src

はじめて、InitialContext? が、なんで "Initial" なのか分かったわ

package com.mycompany.sampleweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
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(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
            out.println(dumpJndi("", new InitialContext()));
            out.println("</body>");
            out.println("</html>");
        } catch (Exception ex) {
            Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private String dumpJndi(String prefix, Context ctx) throws NamingException {
        
        StringBuilder sb = new StringBuilder();
        sb.append("<ul>");
        
        NamingEnumeration<Binding> bindings = ctx.listBindings("");
        while(bindings.hasMoreElements()) {
            Binding binding = bindings.next();
            
            String name = binding.getName();
            Object obj = binding.getObject();
            
            sb.append("<li>")
              .append(prefix)
              .append(binding.getName())
              .append("<font size=2 color='gray'> ")
              .append(binding.getClassName())
              .append("</font>");
            
            if (obj instanceof Context) {
                sb.append(dumpJndi(prefix + name + "/", (Context)obj));
            } else if (obj instanceof Reference) {
                sb.append(dumpReference(prefix + name + "/", (Reference)obj));
            }
            
            sb.append("</li>");
        }
        
        sb.append("</ul>");
        return sb.toString();
    }
    
    private String dumpReference(final String prefix, final Reference ref) {
        StringBuilder sb = new StringBuilder();
        sb.append("<ul>");

        Enumeration<RefAddr> refAddrs = ref.getAll();
        while(refAddrs.hasMoreElements()) {
            RefAddr refAddr = refAddrs.nextElement();
            String type = refAddr.getType();
            Object content = refAddr.getContent();
            sb.append("<li>")
              .append(type).append("=").append(content)
              .append("</li>");
        }
        
        sb.append("</ul>");
        
        return sb.toString();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

実行結果

jndi.png

Java#Glassfish


添付ファイル: filejndi.png 1817件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-02-20 (木) 01:19:47 (3962d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=66ecda7d42
ISBN10
ISBN13
9784061426061