imageservlet.java~22~
来自「Java 集成开发实例精解」· JAVA~22~ 代码 · 共 85 行
JAVA~22~
85 行
package cmp2image;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import java.sql.*;import javax.naming.*;import javax.rmi.PortableRemoteObject;public class ImageServlet extends HttpServlet { private CatalogHome catalogHome; public void init() throws ServletException { } //Process the HTTP Get request public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Flower flower = null; HttpSession session = request.getSession(true); try { Context context = getInitialContext(); //look up jndi name Object ref = context.lookup("Catalog"); //look up jndi name and cast to Home interface catalogHome = (CatalogHome) PortableRemoteObject.narrow(ref, CatalogHome.class); Catalog catalog = catalogHome.create(); String str = request.getParameter("pic"); String name=str.trim(); ArrayList collection = catalog.findOne(name); byte[] buf=null; Iterator i = collection.iterator(); while (i.hasNext()) { Object obj = i.next(); if (obj instanceof byte[]) { buf = (byte[]) obj; } } response.getOutputStream().write(buf); str = request.getParameter("action"); String action=str.trim(); // if (action.equals("all")) getServletContext().getRequestDispatcher("/display.jsp").include(request, response); } catch (Exception ex) { ex.printStackTrace(); } } private Context getInitialContext() throws Exception { String url = "t3://localhost:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch (Exception e) { System.out.println("Unable to connect to WebLogic server at " + url); System.out.println("Please make sure that the server is running."); throw e; } } //Clean up resources public void destroy() { }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?