JNDI Custom Resource

src

package com.mycompany.sampleweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 = "CustomResource", urlPatterns = {"/CustomResource"})
public class CustomResource extends HttpServlet {

    @Resource(lookup = "APP_PROPERTY")
    private Properties resource;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CustomResource</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Lookup from Initial Context</h1>");
            
            InitialContext ictx = new InitialContext();
            Properties prop = (Properties) ictx.lookup("APP_PROPERTY");
            out.println("<table border='1'>");
            for (Map.Entry<Object, Object> entry : prop.entrySet()) {
                out.println("<tr><td>" + entry.getKey() + "</td><td>" + entry.getValue() + "</td></tr>");
            }
            out.println("</table>");
            
            out.println("<h1>Lookup from Injection @Resource</h1>");
            out.println("<table border='1'>");
            for (Map.Entry<Object, Object> entry : resource.entrySet()) {
                out.println("<tr><td>" + entry.getKey() + "</td><td>" + entry.getValue() + "</td></tr>");
            }
            out.println("</table>");
            
            out.println("</body>");
            out.println("</html>");
        } catch (NamingException ex) {
            Logger.getLogger(CustomResource.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

実行結果

custom2.png

java.util.Properties 以外の Resource

Custom Resource には、日本語を使わない


Java#Glassfish


添付ファイル: filecustom2.png 312件 [詳細] filecusotom1.png 318件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-02-23 (日) 01:56:57 (1019d)
ISBN10
ISBN13
9784061426061