⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notifyservlet.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * 
 * NotifyServlet.java
 * 
 * Created on 2003-8-15 12:22:23
 * 
 */
package com.liuyang.jboss.sessionbean.notify.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.CreateException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuyang.jboss.sessionbean.notify.ejb.NotifyBeanLocal;
import com.liuyang.jboss.sessionbean.notify.ejb.NotifyBeanLocalHome;
import com.liuyang.jboss.sessionbean.product.ejb.ProductHome;

/**
 * @author liuyang
 * 
 */
public class NotifyServlet extends HttpServlet {


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		boolean addressok = request.getParameterMap().containsKey("address");
		boolean nameok = request.getParameterMap().containsKey("name");
		boolean descriptok = request.getParameterMap().containsKey("descript");		
				
		if(addressok){	
			try {
				out.println(notify.sendNotify(new InternetAddress(request.getParameter("address"))));
				return;
			} catch (AddressException e) {
				e.printStackTrace();
			}
		}
		if(nameok&&descriptok){	
			try {
				String radomid = System.currentTimeMillis()+""+(int)(Math.random()*10)+(int)(Math.random()*10)+(int)(Math.random()*10)+(int)(Math.random()*10);
				home.create(radomid,request.getParameter("name"),request.getParameter("descript"));
			} catch (CreateException e) {
				e.printStackTrace();
			}
		}else{
			out.println("please input address to notify");
		}		
	}
	private NotifyBeanLocal notify = null;
	private ProductHome home = null;	


	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		if(notify == null){
			InitialContext initContext = null;
			try {
				initContext = new InitialContext();
				Object ref  = initContext.lookup("sessionbean/notify");
				notify = ((NotifyBeanLocalHome)ref).create();
				notify.setHost("127.0.0.1");
				notify.setUser("tester","123456");				
			} catch (NamingException e2) {
				e2.printStackTrace();
			} catch (CreateException e) {
				e.printStackTrace();
			}
		}
		if(home == null){
			InitialContext initContext = null;
			try {
				initContext = new InitialContext();
				Object ref  = initContext.lookup("sessionbean/product");
				home = (ProductHome)ref;
			} catch (NamingException e2) {
				e2.printStackTrace();
			}
		}					
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -