《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("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n");
      out.write("<title>jsp页é¢</title> \r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("\t\t");

            DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.LONG);
            String s=dateFormat.format(new Date());
            out.println("Today is " + s);
        
        
      out.write("\r\n");
      out.write("\t\t<br>\r\n");
      out.write("\t\tNow time is ");
      out.print(getTodaysDate() );
      out.write(" \r\n");
      out.write("</body>\r\n");
      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设计模式:

 

转载于:https://www.cnblogs.com/mryangbo/p/9984294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值