1 16 package dlog4j; 17 18 import java.io.IOException ; 19 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServlet ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 26 import dlog4j.util.image.RandomImageGenerator; 27 28 29 33 public class RandomImageServlet extends HttpServlet { 34 35 public final static String RANDOM_LOGIN_KEY = "RANDOM_LOGIN_KEY"; 36 37 public void init() throws ServletException { 38 System.setProperty("java.awt.headless","true"); 39 } 40 41 public static String getRandomLoginKey(HttpServletRequest req) { 42 return (String )req.getSession().getAttribute(RANDOM_LOGIN_KEY); 43 } 44 45 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException 46 { 47 HttpSession ssn = req.getSession(); 48 if(ssn!=null) { 49 String randomString = RandomImageGenerator.random(); 50 ssn.setAttribute(RANDOM_LOGIN_KEY,randomString); 51 res.setContentType("image/jpeg"); 52 RandomImageGenerator.render(randomString,res.getOutputStream()); 53 } 54 } 55 } 56 | Popular Tags |