• 《Servlet和jsp学习指南》 笔记1


    chapter 1 Servlet

    4个java 包:

    对于每一个http请求,Servlet请求都会创建一个ServletRequest实例,并将它传给Servlet的service方法。ServletRequest封装有关请求的信息。

    Chapter 2 Session管理

    cookie

    Cookie cookie=new Cookie(name,value);

    response.addCookie(cookie);

    获取cookie:

    删除cookie(创建一个同名cookie,将它的maxAge属性设置为0,添加到响应中):

    HttpSession

    chapter 3 JSP

    简单的jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.Date" %>
    <%@ page import="java.text.DateFormat" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>jsp页面</title>
    </head>
    <body>
            <%
                DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.LONG);
                String s=dateFormat.format(new Date());
                out.println("Today is " + s);
            
            %>
    </body>
    </html>

    隐式对象

     

    不需要创建,直接拿来使用,例如上例中的out;

     脚本元素

    1. Scriptlet 。写在 <% %>中的代码块。该代码块会编译在servlet的service(_jspService方法)方法中,作为局部变量;
    2. 表达式。<%=java.util.Calendar.getInstance().getTime()%>,其效果等同于<% out.print(java.util.Calendar.getInstance().getTime()); %>,注意:表达式的后面不能有分号。
    3. 声明。写在<%!  %>中声明的变量和方法。该代码块会编译在servlet类中,作为全局变量或方法。(声明可以放在jsp中的任何位置,可以有多个声明)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.Date" %>
    <%@ page import="java.text.DateFormat" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>jsp页面</title> 
    </head>
    <body>
        <%!
        public Date getTodaysDate(){
            return new java.util.Date();
        }
        public void jspInit(){        //覆盖servlet的init方法
            System.out.println("jspInit......");
        }
        public void jspDestroy(){    //覆盖servlet的destroy方法
            System.out.println("jspDestroy......");
        }
        %>
            <%
                DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.LONG);
                String s=dateFormat.format(new Date());
                out.println("Today is " + s);
            
            %>
            <br>
            Now time is <%=getTodaysDate() %> 
    </body>
    </html>

     编译的servlet类:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.47
     * Generated at: 2018-11-20 01:42:51 UTC
     * Note: The last modified time of this file was set to
     *       the last modified time of the source file after
     *       generation to assist with modification tracking.
     */
    package org.apache.jsp.page.test;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import java.util.Date;
    import java.text.DateFormat;
    
    public final class a_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
    
        public Date getTodaysDate(){
            return new java.util.Date();
        }
        public void jspInit(){        //覆盖servlet的init方法
            System.out.println("jspInit......");
        }
        public void jspDestroy(){    //覆盖servlet的destroy方法
            System.out.println("jspDestroy......");
        }
        
      private static final javax.servlet.jsp.JspFactory _jspxFactory =
              javax.servlet.jsp.JspFactory.getDefaultFactory();
    
      private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
    
      private javax.el.ExpressionFactory _el_expressionfactory;
      private org.apache.tomcat.InstanceManager _jsp_instancemanager;
    
      public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
        return _jspx_dependants;
      }
    
      public void _jspInit() {
        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
      }
    
      public void _jspDestroy() {
      }
    
      public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException, javax.servlet.ServletException {
    
        final javax.servlet.jsp.PageContext pageContext;
        javax.servlet.http.HttpSession session = null;
        final javax.servlet.ServletContext application;
        final javax.servlet.ServletConfig config;
        javax.servlet.jsp.JspWriter out = null;
        final java.lang.Object page = this;
        javax.servlet.jsp.JspWriter _jspx_out = null;
        javax.servlet.jsp.PageContext _jspx_page_context = null;
    
    
        try {
          response.setContentType("text/html; charset=UTF-8");
          pageContext = _jspxFactory.getPageContext(this, request, response,
                      null, true, 8192, true);
          _jspx_page_context = pageContext;
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;
    
          out.write("
    ");
          out.write("
    ");
          out.write("
    ");
          out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    ");
          out.write("<title>jsp页面</title> 
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("	");
          out.write("
    ");
          out.write("		");
    
                DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.LONG);
                String s=dateFormat.format(new Date());
                out.println("Today is " + s);
            
            
          out.write("
    ");
          out.write("		<br>
    ");
          out.write("		Now time is ");
          out.print(getTodaysDate() );
          out.write(" 
    ");
          out.write("</body>
    ");
          out.write("</html>");
        } catch (java.lang.Throwable t) {
          if (!(t instanceof javax.servlet.jsp.SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              try { out.clearBuffer(); } catch (java.io.IOException e) {}
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
            else throw new ServletException(t);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }
    <%@ page import="com.test.vo.UserVO" %>

     

     

    动作

    chapter 4 EL 

    EL表达式格式:${expression}

    保留字:

     chapter 8 监听器

    创建监听器类实现Listener接口。

    注册监听器:

    1. 注解方式:监听器类上添加@WebListener
    2. 部署描述符:在web.xml中添加<listener><listener-class>fully-qualified listener class</listener-class></listener>

    监听器接口种类:

    chapter 9 过滤器

    chapter 10 程序设计

    mvc设计模式:

  • 相关阅读:
    Beego 学习笔记12:文件的操作
    Beego 学习笔记11:文件的上传下载
    Beego 学习笔记10:Easyui使用
    Beego 学习笔记9:Boostrap使用介绍
    Beego 学习比较8:SQL语句
    Beego 学习笔记7:JS分页
    Beego学习笔记6:分页的实现
    【嵌入式linux】用户登录密码验证配置
    【Linux 环境搭建】ubuntu下nfs安装与配置
    【嵌入式 Linux文件系统】如何使用NFS文件系统
  • 原文地址:https://www.cnblogs.com/mryangbo/p/9984294.html
Copyright © 2020-2023  润新知