zoukankan      html  css  js  c++  java
  • java web学习

    ---恢复内容开始---

    1.HTTP响应包含三部分:请求行,请求头,请求体。具体结构如图所示

    2.状态码

    3.contextType的作用是浏览器采用何种方式对响应体进行处理

    4.请求转发和重定向:

    多个servlet中的跳转有两种方式:

    第一种:request.getRequestDispatcher().forward();//请求转发 它是在服务器,即tomcat内部,把请求从第一个servlet转发到第二个servlet.。请求转发是服务器跳转,只会产生一次请求。

    第二种:response.sendRedirect;//响应重定向 从浏览器重新发送一个新的请求给servlet,会产生两次请求

    设置请求自定义属性值 :设置请求属性:request.setAttribute(属性名,属性值)

    获取属性值:request.getAttribute(属性名);

    5.浏览器cookie:cookie是浏览器保存在本地的文本内容,常用来保存用户登录状态,用户资料等小文本.cookie具有时效性,cookie内容会伴随请求发送给tomcat

    package com.xiaoqiang.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class FirstServlet
     */
    @WebServlet("/Test")
    public class FirstServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FirstServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*    String output="<h1 style='color:red'>"+"小强</h1>";
            response.setContentType("text/xml;charset=utf-8");
            response.getWriter().println(output);*/
        
            /*response.sendRedirect("/servlet_study/Test_1");*/
            Cookie cookie=new Cookie("user", "xiaoqiang");
            response.addCookie(cookie);
            request.setAttribute("xiaoqiang", "小强");
            request.getRequestDispatcher("/Test_1").forward(request, response);
            
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }*/
    
    }

    cookies的创建

    //获取cookie中的所有值
    //伪代码
    Cookie [] cookies=request.getCookies();
    
    for(Cookie coo:cookies)

    6.session:session(用户会话)用于保存与“浏览器窗口”对应的数据,session的数据存储在tomcat服务器的内存中,具有时效性。session通过

    浏览器cookie的sessionId值提取用户数据。

     session传递的小例子:

    package com.xiaoqiang.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class FirstServlet
     */
    @WebServlet("/Test")
    public class FirstServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FirstServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*    String output="<h1 style='color:red'>"+"小强</h1>";
            response.setContentType("text/xml;charset=utf-8");
            response.getWriter().println(output);*/
        
            /*response.sendRedirect("/servlet_study/Test_1");*/
          /*  Cookie cookie=new Cookie("user", "xiaoqiang");
            response.addCookie(cookie);*/
            HttpSession session=request.getSession();
            session.setAttribute("xiaoqiang", "小强123");
        /*    request.setAttribute("xiaoqiang", "小强");*/
            request.getRequestDispatcher("/Test_1").forward(request, response);
            
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }*/
    
    }
    package com.xiaoqiang.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class SecondServlet
     */
    @WebServlet("/Test_1")
    public class SecondServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public SecondServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String output="<h1 style='color:red'>"+request.getSession().getAttribute("xiaoqiang")+"</h1>";
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println(output);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }*/
    
    }

    7.servletContext:ServletContext(Servlet上下文对象),是web应用全局变量,一个web应用只会创建一个ServletContext对象,

    一个ServletContext随着web应用启动而创建

    //伪代码
    ServletContext servletcontext=request.getServletContext();
    servletcontext.setAuttribute(K,V);

    8.java web 三大作用域对象:HttpServletRequest 请求对象  HttpSession 用户会话对象   ServletContext web应用全局对象

    它们的作用域和生命周期是依次递增的。

    9.解决post请求中的中文乱码:由于tomcat的默认使用字符集ISO-8859-1,属于西欧字符集

     解决字符集乱码的核心是把字符集iso-8859-1转化为UTF-8,servlet中请求与响应都需要设置UTF-8。

    可以用request.setCharacterEncoding("UTF-8") 方法用于将请求体中的文字字符集转换为UTF-8。

    对于tomcat8.x版本以上的get发送中文请求默认是utf-8的格式,无需转换。

    response响应也要设置utf-8的编码

    10.web.xml中常用配置

    在web.xml中写入标签全局变量参数 <context-parm><parm-name>名称</parm-name><parm-value>值</parm-value> </context-parm>

    错误跳转<error-page><error-code>错误编码</error-code><location>路径</locathion></error-page>

    11.jsp中的九大内置对象

    1.request 请求对象 HttpServletRequest

    2.response 响应对象 HttpServletRespone

    3.session 用户会话 HttpSession

    4.application 应用全局变量 ServletContext

    5.out 输出对象 PrintWrite

    6.page 当前页面对象 this

    7.pageContext  页面上下文对象 PageContext

    8.config 应用配置对象 ServletConfig

    9.exception 应用异常对象 Throwable

  • 相关阅读:
    [转贴] IPSEC From 知乎
    intel 的架构图
    IPV6 简单验证
    exsi6.0远程修改密码
    Oracle单个datafile大小的限制
    用Linux完成Oracle自动物理备份
    vSphere Client 更改 ESX/ESXi 主机的端口
    netstat Recv-Q和Send-Q
    Linux下安装(卸载)KDE和GNOME
    完美解决xhost +报错: unable to open display ""
  • 原文地址:https://www.cnblogs.com/socketqiang/p/10823719.html
Copyright © 2011-2022 走看看