博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Servlet和jsp学习指南》 笔记1
阅读量:5143 次
发布时间:2019-06-13

本文共 5866 字,大约阅读时间需要 19 分钟。

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" %>
jsp页面 <% DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.LONG); String s=dateFormat.format(new Date()); out.println("Today is " + s); %>

隐式对象

 

不需要创建,直接拿来使用,例如上例中的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" %>
jsp页面 <%! 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); %>
Now time is <%=getTodaysDate() %>

 编译的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
_jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map
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("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("
\r\n"); out.write("
jspé¡µé¢ \r\n"); out.write("\r\n"); out.write("\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
\r\n"); out.write("\t\tNow time is "); out.print(getTodaysDate() ); out.write(" \r\n"); out.write("\r\n"); out.write(""); } 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

你可能感兴趣的文章
VMware环境和Window环境进行网络连接的问题
查看>>
macOS10.12允许所有来源设置
查看>>
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>
python搜索引擎(转)
查看>>
关于height,line-height导致的样式混乱的问题
查看>>
《SEO实战密码》读后一点感受
查看>>
bzoj 4815 [Cqoi2017]小Q的表格——反演+分块
查看>>
Swift 入门之简单语法(六)
查看>>
shim和polyfill有什么区别
查看>>
Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll
查看>>
Zabbix3.4服务器的搭建--CentOS7
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
夜太美---酒不醉--人自醉
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
多线程学习笔记三之ReentrantLock与AQS实现分析
查看>>
【转】进程与线程的一个简单解释
查看>>