Servlet小结

package conclusion;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/ServletDemo")
public class ServletDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //小结:
        //ServletContext: 生命周期 -> 开始: 服务器启动  结束: 服务器关闭
        //获得对象: request.getServletContext()   this.getServletContext()   getServletConfig().getServletContext()
        //获得的是同一个  application
        ServletContext application = request.getServletContext();
        //ServletContext application1 = getServletContext();
        //ServletContext application2 = getServletConfig().getServletContext();
        //获得参数  -> 需要在web.xml文件中添加
        //      <context-param>
        //          <param-name>name</param-name>
        //          <param-value>zjh</param-value>
        //      </context-param>
        String name = application.getInitParameter("name");
        System.out.println(name);//zjh
        //获得应用名
        String contextPath = application.getContextPath();
        System.out.println(contextPath);///20220816
        //获取资源的绝对路径
        String realPath = application.getRealPath("a.txt");
        System.out.println(realPath);//D:\Nochinese\Java\JavaEE\out\artifacts\20220816_war_exploded\a.txt
        //存储数据
        application.setAttribute("count",1);
        Object count = application.getAttribute("count");
        System.out.println(count);//1
        application.removeAttribute("count");
        System.out.println(count);//1
        //request:
        //获得请求参数
        String password = request.getParameter("password");
        System.out.println(password);//1234
        //域对象的使用 -> 转发
        request.getRequestDispatcher(getServletContext().getContextPath()).forward(request,response);
        //乱码问题
        request.setCharacterEncoding("UTF-8");
        //获得Cookie
        Cookie cookie = new Cookie("id", "123");
        response.addCookie(cookie);
        String value = cookie.getValue();
        System.out.println(value);//123
        //获得session
        HttpSession session = request.getSession();
        session.setAttribute("age",18);
        String JSESSIOID = session.getId();
        Object age = session.getAttribute("age");
        System.out.println(age);//18
        System.out.println(JSESSIOID);//1016AB523BCB02185096A6010E9BC21F
        //response:
        //设置响应类型
        response.setStatus(302);
        //重定向, 响应体
        response.sendRedirect(getServletContext().getContextPath());
        //cookie有关
        //session: 原理, 使用, 持久化
        //持久化 设置cookie路径名称必须一致
        Cookie cookie1 = new Cookie("JSESSIONID",session.getId());
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(10 * 60);
        response.addCookie(cookie1);
        //cookie: 原理, 使用, 常用api
        Cookie cookie2 = new Cookie("job", "manager");
        //设置存在时间,单位秒
        cookie2.setMaxAge(1000);
        //获得key
        cookie2.getName();
    }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值