长久以来困扰我这个编程小白的技术难题:怎么做会自动刷新的头像? 今天终于在我看某穿越文时得到了灵感:就是一URL-MAPPING而已阿!怎么之前就这么笨呢?
<?xml version="1.0" ?>
<web-app>
<servlet>
<servlet-name>ImageRefreshServlet</servlet-name>
<servlet-class>ImageRefreshServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageRefreshServlet</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
</web-app>
然后简单写个Servlet就一切搞定,试验成功OYEAH!
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageRefreshServlet extends HttpServlet
{
//Use a static variable to record visiting times during this servlet's lifespan.
static int i=1;
//forward method is used for dispatching.
private void forward(String resource, HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException
{
ServletContext ctx = getServletContext();
RequestDispatcher dispatcher = ctx.getRequestDispatcher(resource);
if(dispatcher != null)
{
dispatcher.include(req, res);
return;
}
}
//Method that handles GET:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
if(i==1)
{
forward("/WEB-INF/img/img1.jpg", req, res);
i++;
}
else if(i==2)
{
forward("/WEB-INF/img/img2.jpg", req, res);
i++;
}
else if(i==3)
{
forward("/WEB-INF/img/img3.jpg", req, res);
i=1;
}
}
//Everything is identical to the doGET method.
public void doPOST(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doGet(req, res);
}
}
现在的问题是满世界免费的PHP空间车载斗量,免费的JSP空间还真不多,去专业论坛求对应编程代码ING。
顺便,这一切都是在WEI当年一句话提点下完成的,饮水思源,偶要谢谢他。
[ 本帖最后由 Ginar 于 2007-11-6 13:40 编辑 ] |