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

chapter 1 Servlet

4个java 包:

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

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

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

Chapter 2 Session管理

cookie

Cookie cookie=new Cookie(name,value);

response.addCookie(cookie);

获取cookie:

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

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

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

HttpSession

chapter 3 JSP

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

简单的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>

隐式对象

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

不需要创建,直接拿来使用,例如上例中的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" %>

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

动作

chapter 4 EL

EL表达式格式:${expression}

保留字:

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

chapter 8 监听器

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

注册监听器:

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

监听器接口种类:

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

chapter 9 过滤器

chapter 10 程序设计

mvc设计模式:

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

上一篇:《Servlet和jsp学习指南》 笔记2


下一篇:MySQL索引及Explain及常见优化