ServletContext练习
2015-09-26 15:25
260 查看
import java.io.IOException; import java.io.PrintWriter; import java.util.*; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SendMsgServlet extends HttpServlet{ private ServletContext application; public void init(){ System.out.println("init"); //获取上下文 application = this.getServletContext(); List<String> msglist = new ArrayList<String>(); application.setAttribute("MsgList", msglist); } public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{ System.out.println("doGet"); String ip = req.getRemoteAddr();//获取ip String msg = req.getParameter("msg");//发送的消息 String face = req.getParameter("face");//表情图片 String context = ip+" say:"+msg+"<img src='faces/"+face+".gif'>"; List<String> msgs = (List<String>)application.getAttribute("MsgList"); msgs.add(context); PrintWriter pw = resp.getWriter(); pw.write("<html>"); pw.write("<body>"); pw.write(context); pw.write("<div class=\"send\">"+" <form action=\"sendMsg\" method=\"post\">"+" <input type=\"text\" id=\"sendText\" name=\"msg\"/>"+" <select name=\"face\" class=\"face\">"+" <option value=\"1\">suprise</option>"+" <option value=\"2\">embarase</option>"+" <option value=\"3\">sex</option>"+" </select>"+" <input type=\"submit\" value=\"submit\" id=\"submit\"/>"+" </form>"+" </div>"); pw.write("</body>"); pw.write("</html>"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); System.out.println("doPost"); } }
相关文章推荐
- hibernate4.3.5需要的jar包
- 【Java】hashMap和hashTable的区别
- 使用 python3 环境开发。
- 《深入理解java虚拟机》之垃圾回收
- 网店刷收藏会不会被降权
- Semantic matching & Semantic correlation matching
- IP网际协议
- Implicit declaration of function 'NSFileTypeForHFSTypeCode' is invalid in C99
- 2014-2015:被项目“折磨”的一年
- linux下mysql yum 安装 新建用户 设置权限
- .net 开源组件推荐 之 StackExchange
- 大道至简第二章读后感
- C#基于UDP实现的P2P语音聊天工具
- Unity微端场景加载(三)动态加载光照贴图
- exit(0),ExitProcess和ThrminateProcess的区别的联系
- 运行Oracle SQL Developer出错
- Android WebView js和Java互相调用注意事项
- 数据缓冲区高速缓冲
- Linux下core文件的生成和使用
- BZOJ 2431: [HAOI2009]逆序对数列( dp )