首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Servlet-学习札记-2

2012-12-26 
Servlet-学习笔记-2日期:2011-3-22 am****************************************************************

Servlet-学习笔记-2
日期:2011-3-22 am

**********************************************************************************

1、servlet 使用表单传值

doGet(req,resp)/doPost(req,resp)
   同级调用



在 doPost(req,resp) 方法中 同级调用 doGet(req,resp)
   优点:1、可避免代码冗余 。 2、可随意切换表单提交方式。

 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)         throws ServletException, IOException {req.setCharacterEncoding("GBK");//设置 请求和响应 编码,避免乱码 resp.setCharacterEncoding("GBK");PrintWriter out = resp.getWriter();Object str = req.getParameter("name");out.println("name:"+str);                Object str2 = req.getParameter("age");                out.println("age:"+age);//super.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);//同级调用doGet 方法                //super.doPost(req, resp);}

注意 : 在此若 提交方式改为 Post 方式则使用req.getQueryString() 返回为null。

热点排行