Servlet笔记

常用方法
    getRequestURL()  获取客户端发出请求时的完整URL(http://localhost:8080/s01/sr05)
    getRequestDispatcherURI   获取请求行中的资源名称部分(项目名开始)(/s01/sr05)
    getQueryString()  获取请求行中的参数部分 ?后的参数
    getMethod()   获取客户端请求方式(get或post)
    getProtocol()   获取HTTP版本号
    getContextPath  获取项目对外访问路径(/sr05)
    getParameter(name)   获取指定名称的参数
    getParameterValues(String name)   获取指定名称参数的所有值
 
乱码   
    Tomcat8以上版本get请求不会乱码,post请求会乱码,需设置
        request.setCharacterEncoding("UTF-8"),在获取参数前设置

页面请求转发跳转方法
    request.getRequestDispatcher(url).forward(request,response);
    浏览器中的地址栏不会发生改变
    对象共享

    
jsp页面请求转发跳转方法
    request.getRequestDispatcher(".jsp").forward(request,response);
    <%--如果要在jsp中写java代码,需要写在脚本段中--%>

request作用域
    设置域对象内容
        request.setAttribute(String name,Object(可以放int,String,list) value);
    获取域对象内容
        request.getAttribute(String name);
    删除域对象内容
        request.removeAttribute(String name);

响应数据
    两种方式:
        getWriter()   字符输出流(输出字符串)
        PrintWriter writer = response.getWriter();  获取字符输出流
        writer.writer(s:"hello");   输出数据

        getOutputStream()   字节输出流(输出一切数据)
        SereletOutputStream out = response.getOutputStream()  得到字节输出流
        out.writer("Hi".getBytes());   输出数据
        
        两种不能同时使用

响应乱码

    字符流响应输出乱码解决方法,同时设置服务端和客户端的编码格式(如果只设置服务端编码格式,由于服务端和客户端编码格式不一样还会乱码),且需要在获取前设置

    (这两个是分别设置(麻烦))
    设置服务端的编码格式
    response.setCharacterEncoding("UTF-8");
    设置客户端的编码格式和响应的MIME类型
    response.setHearder("content-type","text/html;charset=UTF-8");

    (同时设置客户端和服务端的编码格式)
    response.setContentType("text/html;charset=UTF-8");


    字节流响应输出乱码解决方法

    (同时设置客户端和服务端的编码格式)
    response.setContentType("text/html;charset=UTF-8");


重定向
    response.sendRedirect("@WebServlet中的对外访问路径")
    跳转        对象不共享
    浏览器中的地址栏不会发生改变


Cookie
    通过new 生成cookie
    cookie.setMaxAge();   设置存活时间  负数,关闭浏览器销毁;0,表示立即销毁;正数,存活秒数


    添加cookie
    response.addCookie();

    for ... each 遍历
    if(Cookies != null && Cookies.length > 0){
        for(Cookie cookie:Cookies){
            String name = cookie.getName();
            String value = cookie.getValue();
            sout.("名称:" + name + ",值:" + value);
            
        }
    }


    Cookie不能出现中文,也不建议存中文,如果有中文则通过URLEncoder.encode()来进行编码,获取则通过
    URLDecoder.decode()来进行解码

    String name = "姓名";
    String value = "张三";
    
    name=URLEncoder.encode(name);
    value=URLEncoder.encode(value);
    Cookie cookie = new Cookie(name,value);
    response.addCookie(cookie);
    sout(URLDecoder.decode(cookie.getName()));
    sout(URLDecoder.decode(cookie.getValue()));


    Cookie路径问题

    当前项目路径为"/s01"
    cookie.setPath("/");
    //设置路径为"/",表示当前服务器下任何项目的任何资源都可获取Cookie对象

    当前项目路径为"/s01"
    cookie.setPath("/s01") ;
    默认不设置,表示当前项目下的任何项目都可以访问到Cookie对象

    当前项目路径为"/s01"
    cookie.setPath("/s02");
    //设置路径为"/s02",表示在s02项目下才能访问到Cookie对象
    //只能在s02项目下获取Cookie,即使Cookie对象是在s01中创建的,s01也不能获取它

    当前项目路径为"/s01"
    cookie.setPath("/s01/cook");
    //设置路径为"/s01/cook",表示在"/s01/cook"目录下才能访问到Cookie对象

HttpSession对象
   
   获取session对象
   HttpSession session = request.getSession();
   获取session的会话标识符
   String id = session.getId();
   //获取session的创建时间
   sout(session.getCreationTime());
   //获取session的最后一次访问时间
   sout(session.getLastAccessedTime());
   //判断是否为新的sesHttpSession对象
   sout(session.isNew());

Session域对象
   获取session对象
   HttpSession session = request.getSession();
   设置session域对象
   session.setAttribute("uname","upwd");
   获取指定名称的session域对象
   session.getSession().getAttribute("uname");
   移除指定名称的session域对象
   session.removeAttribute("upwd");

   session对象的销毁
   tomacat中默认session对象存活30分钟
   在tomact中的config的web.xml文件中更改<session-config>
   
   idea中修改最大存活时间
   session.setMaxInactiveInterval(15);//15秒
   获取session的最大存活时间
   session.getMaxInactiveInterval();

   立即销毁
   session.invalidate();

   关闭浏览器失效
   session底层依赖cookie,

   关闭服务器失效,session销毁,cookie对象默认只在浏览器中存活,关闭浏览器即失效

   ServletContext对象
   每个web中有且仅有一个ServletContext对象,又称Application对象
   获取ServletContext对象
   
   通过request对象获取
   ServletContext servletContext1 = request.getServletContext();
   
   通过session对象获取
   ServletContext servletContext2 = session.getSession().getServletContext(); 
   
   通过ServletConfig对象获取
   ServletContext servletContext3 =getServletConfig().getServletContext();  
   
   直接获取
   ServletContext servletContext4 =getServletContext();

   ServletContext对象的常用方法
   获取当前服务器的版本信息
   String serverInfo = request.getServletContext().getServerInfo();
   获取项目的真实路径
   String realPath = request.getServletContext().getRealPath();

ServletContext域对象
    ServletContext不建议存放过多数据,因为ServletContext中的数据一旦存进去没有手动删除将会一直保存

    获取ServletContext对象
    ServletContext servletContext = request.getServletContext();
    设置域对象
    servletContext.setAttribute("name","zhangsan");
    获取域对象
    String name = (String) servletContext.getAttribute("name");
    移除域对象
    servletContext.removeAttribute("name");

文件上传
    前台页面实现
    <form method="post" enctype="multipart/form-data" action="uploadServlet">
    1.准备表单
    2.设置表单的提交类型为post请求 method="post"
    3.设置表单类型为文件上传表单 enctype="multipart/form-data"
    4.设置文件提交地址
    5.准备表单元素
        1.普通表单项 type="text"
        2.文件项 type="file"
    6.设置表单元素的name属性值(表单提交一定要设置表单元素的name属性值,否则后台无法接收数据)
        姓名:<input type="text" name="uname"> <br>
        文件:<input type="file" name="myfile"> <br>

    后台代码实现
    @WebServlet("/uploadServlet")
    @MultipartConfig //如果是文件上传,必须要设置该注解
    //设置请求的编码格式
    request.setCharacterEncoding("UTF-8");
    //获取普通表单
    String uname = request.getAttribute("uname");
    //获取Part对象(Servlet将multipart/form-data的post请求封装成Part对象)
    Part part = request.getPart("myfile");
    //通过part对象得到上传的文件名称
    String fileName = part.getSubmittedFileName();
    //得到文件存放路径
    String filePath = request.getServletContext().getRealPath("/");
    //上传文件到指定目录
    part.write(filePath +"/" + fileName);

文件下载
    超链接下载(前台实现)  设置download属性,浏览器能不能识别都会下载
    <a href="站点名/包名/文件名" download="属性名">文件下载</a>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值