• web


    Servlet

    • 例子
      创建一个servlet类让其继承Servlet
    
    public class HelloServlet implements Servlet {
    
        public HelloServlet() {
            System.out.println("构造器");
        }
        @Override
        public void init(ServletConfig servletConfig) throws ServletException {
            System.out.println("init");
        }
    
        @Override
        public ServletConfig getServletConfig() {
            return null;
        }
    
        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
            System.out.println(" service    helloServlet");
        }
    
        @Override
        public String getServletInfo() {
            return null;
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy");
        }
    }
    

    编写配置文件web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <!--servlet标签给tomcat配置servlet程序-->
        <servlet>
            <!--给servlet程序起一个别名 一般是类名-->
            <servlet-name>HelloServlet</servlet-name>
            <!--servlet程序的全类名-->
            <servlet-class>com.hyq.servert.HelloServlet</servlet-class>
        </servlet>
        <!--给servlet程序配置访问路径-->
        <servlet-mapping>
            <!--告诉服务器,当前配置的地址给那个servlet程序使用-->
            <servlet-name>HelloServlet</servlet-name>
            <!--访问地址-->
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    </web-app>
    

    浏览器输入地址:http://localhost:8080/java_02_web_Web/hello
    控制台打印的是servlet类中service方法中的结果:helloServlet

    • servlet声明周期,执行顺序:构造器-->init()-->service()--->destroy()
      仍然用上面的servlet类,访问http://localhost:8080/java_02_web_Web/hello
      浏览器打印了如下结果:
      构造器
      init
      service   helloServlet
    

    服务未关闭情况下,再次访问http://localhost:8080/java_02_web_Web/hello时,只会执行service的方法。在关闭服务时,执行destroy()方法

    servletContext的作用

    ① 获取web.xml中配置的上下文参数context-param
    ② 获取当前的工程路径,格式:/工程路径
    ③ 获取工程部署后在服务器硬盘上的绝对路径
    ④ 像Map一样存储数据
    SerletContext是一个接口,它表示Serlet上下文对象
    ⑥ 一个web工程,只有一个SerletContext对象实例。
    ServletContext是在web工程部署启动的时候创建。在web工程停止的时候销毁。
    例子:
    编写一个servlet类,继承HttpServlet

    public class ServletContext extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            javax.servlet.ServletContext context = getServletContext();
            //获取web.xml中配置的上下文参数 context-param
            String url = context.getInitParameter("url");
            System.out.println(url);  // www.baidu.com
            // 企图获取 init-param 参数值;getInitParameter()方法只能获取 context-param 里面的值
            System.out.println(context.getInitParameter("name")); //null
            //获取当前工程路径 格式:/工程名称
            System.out.println(context.getContextPath()); // 【/java_02_web_Web】
            //获取工程部署后 在服务器硬盘上的绝对路径
            //【E:CodeMgrideacodejavastudyoutartifactsjava_02_web_Web_exploded】
            System.out.println(context.getRealPath("/"));
        }
    }
    

    配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">   
        <servlet>
            <servlet-name>ServletContext</servlet-name>
            <servlet-class>com.hyq.servert.ServletContext</servlet-class>
            <!--初始化参数-->
            <init-param>
                <param-name>namespace</param-name>
                <param-value>hyq</param-value>
            </init-param>
        </servlet>   
        <servlet-mapping>
            <servlet-name>ServletContext</servlet-name>
            <url-pattern>/ServletContext</url-pattern>
        </servlet-mapping>
        <!--上下文参数-->
        <context-param>
            <param-name>url</param-name>
            <param-value>www.baidu.com</param-value>
        </context-param>
    </web-app>
    


    例子

    • 编写web.xml配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">   
        <servlet>
            <servlet-name>ServletContext</servlet-name>
            <servlet-class>com.hyq.servert.ServletContext</servlet-class>      
        </servlet>   
        <servlet>
            <servlet-name>ServletContext2</servlet-name>
            <servlet-class>com.hyq.servert.ServletContext2</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ServletContext</servlet-name>
            <url-pattern>/ServletContext</url-pattern>
        </servlet-mapping> 
        <servlet-mapping>
            <servlet-name>ServletContext2</servlet-name>
            <url-pattern>/ServletContext2</url-pattern>
        </servlet-mapping>
    </web-app>
    
    • 编写servlet类继承httpServlet
    public class ServletContext extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            javax.servlet.ServletContext context = getServletContext();
            Object name = context.getAttribute("name");
            System.out.println("name="+name);
            context.setAttribute("name","hyq");
            System.out.println("name="+context.getAttribute("name"));
        }
      
    }
    
    • 编写servlet2类继承httpServlet
    
    public class ServletContext2 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = getServletContext();
            System.out.println("name="+context.getAttribute("name"));
        }
    }
    

    在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext
    访问结果:

    name=null
    name=hyq
    

    服务没有停止时,在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext2
    访问结果:

    name=hyq
    

    再此在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext
    访问结果:

    name=hyq
    name=hyq
    

    结论:ServletContext启动后,工程中只存在一个

    Get/Post请求

    响应的Http协议格式

    1、响应行
    (1)响应的协议和版本号
    (2)响应状态码
    (3)响应状态描述符
    2、响应头
    key:value 不同的响应头,有其不同合义
    空行
    3、响应体: 就是回传给客户端的数据

    Servlet的请求转发和重定向

    编写Servlet1Servlet2

    //Servlet1
    public class Servlet1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            //获取参数值
            String name = req.getParameter("name");
            //把参数存入request中
            req.setAttribute("myName",name);
            //请求转发到服务器2
            // 请求转发必须以【/】打头./在服务器转发时代表的地址是 http://ip:port/工程名
            RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet2");
            dispatcher.forward(req,resp); //转发
        }
    }
    
    //Servlet2
    public class Servlet2 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name = req.getParameter("name");
            System.out.println("myName=="+name);
        }
    }
    

    编写web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>Servlet1</servlet-name>
            <servlet-class>com.hyq.servert.Servlet1</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Servlet1</servlet-name>
            <url-pattern>/servlet1</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>Servlet2</servlet-name>
            <servlet-class>com.hyq.servert.Servlet2</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Servlet2</servlet-name>
            <url-pattern>/servlet2</url-pattern>
        </servlet-mapping>
    </web-app>
    

    测试:在地址栏输入地址:http://localhost:8080/java_02_web/servlet1?name=hyq
    结果是:myName==hyq
    请求转发的结论:
    ① 地址栏没有发生变化
    ② 从A转发到B 共享Request的数据,且是一次请求
    ③ 可以转发到WEB-INF目录下,正常访问,不允许访问WEB-INF目录下的文件
    ④ 不允许访问 站外资源,如www.baidu.com 。
    重定向

    web 中 / 的作用

    ① / 如果被浏览器解析,得到的地址是 http://ip:port/;即当前站点
    如:

    <!-- 此时若点击斜杠会跳转到 http://localhost:8080/ -->
    <a href='/'>斜杠</a>
    <!-- 此时若点击跳转会跳转到 http://localhost:8080/a.html -->
    <a href='/a.html'>跳转</a>
    

    ② / 如果被服务器解析,得到的地址是 http://ip:port/工程名称
    如:

      1. <url-pattern>/ServletContext2</url-pattern>
      2. ServletContext.getRealPath("/")
      3. req.getRequestDispatcher("/");
    

    特殊情况:response.sendRediect("/"); 把斜杠发送给浏览器解析,得到的地址是:http://ip:port/

    Servlet解决中文乱码

     //设置服务器编码
     response.setCharacterEncoding("utf-8");
     //告诉浏览器 使用什么编码
     response.setHeader("Content-Type","text/html;charset=UTF-8");
    
  • 相关阅读:
    华强北三代悦虎1562A怎么样?
    改丝印的假华强北三代1562A,用芯良苦!
    华强北三代过软件检测的佳和1562A
    Unlua静态导出
    Unlua编程基础
    Android JNI调用
    手机屏幕参数
    UE4 stats性能埋点
    【JWT】JSON Web Token
    【算法】一致性哈希算法实现
  • 原文地址:https://www.cnblogs.com/huyuqing/p/14531665.html
Copyright © 2020-2023  润新知