zoukankan      html  css  js  c++  java
  • Servlet交互与JSP

    主要内容介绍

     

    数据共享与页面跳转

     

     

    1. 为什么要有跳转:

     

    Servlet需要跳转到其它Servlet中,因为我们需要职责分明,不同Servlet来完成不同的功能

     

    Servlet跳转到JSP中,Servlet输出动态页面实现恶心,咱们需要使用JSP来做页面的显示

     

    2. 为什么要共享:

     

    http是无状态的,因为在不断的跳转过程中我们需要对数据进行共享。昨天咱们已经学习了一个数据共享Session了。

     

     

    Web页面跳转和信息共享

    .Servlet作用域对象

    .动态网页:JSP

    请求转发(forward

    需求AServlet中没有做完的事情跳转到BServlet中继续执行

    语法req.getRequestDispatcher(String path).forward(ServletRequest req,ServletResponse resp);

     

    getRequestDispatcher  获得转发器

    path : 跳转到哪里?

    特点

    1. 转发过程中浏览器地址栏路径没变

    2. 只发了一个请求

    3. 共享同一个请求(同一个请求对象),在请求中共享数据;

    4. 响应由最后一个决定

    5. 只能够访问当前应用中的资源,不能够跨域跳转(老师我想跳转到源代码官网去看视频...可以使用相对路径,不推荐

    6. 可以访问WEB-INF中的资源

    7. html页面跳转到Servlet需要先加上server.xml中的path(如果有),Servlet中的跳转是不需要加的!

    8. 疑问: 既然可以访问WEB-INF中的资源了,那怎么之前又说放在里安全呢?

       

       a) 程序没有提供的路径就不能够访问;b) 在跳转之前可以做权限判断

     URL重定向(redirect

    需求:AServlet中没有做完的事情跳转到BServlet中继续执行

     

    语法:resp.sendRedirect(String path);

     

    特点: 测试下面每个点请留意浏览器地址栏!!!

    1. 浏览器中地址会变,相当于访问了两次

    注意: path中应该加上server.xml中的path的值,如果有的话

    1. 发送了两个请求

    2. 因为是不同的请求,所以不能够共享请求中的数据

    3. 最终的响应是由最后一个Servlet决定

    4. 可以跨域访问资源(尝试访问itsource.cn

        请求包含(include-不用)  

    需求AServlet中没有做完的事情跳转到BServlet中继续执行

    语法req.getRequestDispatcher(String path).include(ServletRequest req,ServletResponse resp);

    特点: 基本上和forward一样,只是把两次响应的结果都输出了

    1. 常见面试题:

      1. 请求转发forwardURL重定向的区别?

      回答方式:

      ① 分别解释各自的定义

      ② 罗列各自的特点(最好是表格对比)

      ③ 各自的使用场景(其实也是来源于特点)

      2. 两者如何选择?

      如果需要共享请求中的数据,只能用请求转发

      若要访问WEB-INF中的资源 ,只能用请求转发

      ③若要跨域访问,只能用URL重定向

      请求转发可能造成表单重复提交问题

      1. 数据共享与作用域对象

        作用域对象意义;web组件之间传递和共享 数据  
      2. 作用域经典案例(测试)

      @WebServlet("/scope/main")
      public class MainServlet extends HttpServlet {
          @Override
          protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //request
              if(req.getAttribute("count")==null){
                  req.setAttribute("count", 1);
              }else{
                  req.setAttribute("count", ((Integer)(req.getAttribute("count"))+1));
              }
              //session
              HttpSession session = req.getSession();
              if(session.getAttribute("count")==null){
                  session.setAttribute("count", 1);
              }else{
                  session.setAttribute("count", ((Integer)(session.getAttribute("count"))+1));
              }
              //application
              ServletContext application = req.getServletContext();
              if(application.getAttribute("count")==null){
                  application.setAttribute("count", 1);
              }else{
                  application.setAttribute("count", ((Integer)(application.getAttribute("count"))+1));
              }
              req.getRequestDispatcher("result").forward(req, resp);
          }
      }
      • @WebServlet("/scope/result")
        public class ResultServlet extends HttpServlet {

            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //request
                int count1 = (Integer)request.getAttribute("count");
                response.getWriter().println("request:"+count1);
                //session
                HttpSession session = request.getSession();
                int count2 = (Integer)session.getAttribute("count");
                response.getWriter().println("session:"+count2);
                //application
                ServletContext app = request.getServletContext();
                int count3 = (Integer)app.getAttribute("count");
                response.getWriter().println("application:"+count3);
            }

        }

        ServletContext对象

        1. 获得ServletContext对象的方式:

        super.getServletContext()

        this.getServletContext()

        getServletContext()

        都是一样的,提示我们这个方法是父类中的

        ServletContext中的常用方法

        String getContextPath():获取当前应用的上下文路径.

        就是 Tomcat/conf/server.xml中的path

        String getRealPath(String path):获取某个资源的绝对路径.

        例如获得 F:workspaceservlet_jspwebapp

        String getInitParameter(String paramName);全局初始化参数:

        先看传统的Servlet的初始化参数:

        因为配置在当前ServletContextDemo,所以只能被ServletContextDemo使用,其他Servlet无法使用.

        而在开发中,多个Web组件(Servlet/Filter)可以拥有共同的初始化参数,此时只能配置N,不利于维护.

        解决方案:使用全局的初始化参数,Web组件都可以共用,配置问web.xml文件中.

      • 面试题1:

        req.getParameter("")req.getAttribute("")的区别?

        req.getParameter :用户传过来的

        req.getAttribute :从作用域里拿的(必需是放到作用域里的)

        面试题2:

        ServletConfig.getInitParameter

        application.getInitParameter  有什么区别

        ServletConfig.getInitParameter 只作为于一个Servlet

        application.getInitParameter 整个应用都可以拿到

        配置的不同

        JSP原理分析(第一个JSP页面) 一个jsp其实就是一个Servlet

        动态网页:JSP

      • Java Server Pages:Java的服务网页(Java动态网页)
      • 从访问方式来看:

        html,   通过tomcat和直接访问效果是一样的

        jsp     如果直接放在浏览器中显示源码,通过tomcat获得是正常内容

        jsp运行在服务器中

        访问方式   http://localhost/xx/hellojsp/Hello.jsp

        JSP三大指令

         page指令:     设置当前JSP配置信息.

    errorPage="/myerror.jsp" :如果当前页面出错,就跳转到对应的页面

    isErrorPage="true" :这个页面是否是错误页面(当我们要在页面上直接使用exception的时候就可以加上它)

    演示1: 局部错误页面跳转

    演示2: 全局错误页面

    在项目的web.xml中配置

    include指令:  JSP包含其他文件.

    :taglib指令:   引人标签库.

    JSP九大内置对象和四大作用域对象

    application

     JSP语法

    1. 注释
      1. <!-- html注释-->    直接写html注释,会输出到页面上让用户看到
      2. <%-- jsp注释--%>   用户从页面上是看不到的

         2.输出数据到页面   <%=hello%>

          3. JSP中的Java脚本片段   <% 语句1语句2;%>

    4.定义类的成员<% String str; void testMethod(){}       %>

     

     

  • 相关阅读:
    JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP
    mysql存储过程批量插入数据
    mysql 5.7.12 winx64安装配置方法图文教程
    mysql-5.7.12-winx64 安装
    spring下载地址
    jdbc动态切换数据库
    Tomcat7 新的数据库连接池Tomcat jdbc pool介绍和配置
    WebSercice Demo
    面试问题之数据结构与算法:最小生成树算法
    面试问题之数据结构与算法:最大连续子序列和
  • 原文地址:https://www.cnblogs.com/wzscom/p/10316085.html
Copyright © 2011-2022 走看看