栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Web项目对应配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Web项目对应配置

  • web.xml

	
    Same
    
    cn.iu.HelloServlet
  
  
  	
    Same
    
    /hello
  
  • 继承HttpServlet类
Httpservlet类
重写doGet()和doPost(),通常doPost()调用doGet()。

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }



  • 继承类获得对应方法
在doGet()方法里写。

getServletContext();上下文对象,一个Servlet存放,另一个取,实现数据共享。


存

//        this.getInitParameter();      初始化参数
//        this.getServletConfig();      Servlet配置
//        this.getServletContext();     Servlet上下文
        ServletContext servletContext = this.getServletContext();

        String userName = "IU";
        servletContext.setAttribute("username",userName);       //将一个数据保存在了ServletContext中。键值对形式。


取

在取的类里写
ServletContext servletContext = this.getServletContext();
        String username = (String)servletContext.getAttribute("username");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("李知恩:" + username);
       
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672492.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号