互联网三大基石
- HTTP: HyperText Transfer Protocol, 传输数据
- URL:
- HTML: HyperText Markup Language, 显示数据
servlet
servlet 就是 java,是继承父类 HttpServlet 的 java 类。在服务器端运行,用于处理用户的请求并给予响应。
servlet 运行过程:
- 浏览器发送请求到服务器
- 服务器根据请求的 URL,去调用相应的 servlet(servlet 有不止一个),servlet 操作数据库,获取信息
- 通过 servlet 中打印流对象,将生成的 HTML 数据输出给服务器
- 服务器将 servlet 生成的 HTML 数据再输出给客户端浏览器
- 浏览器根据服务器传回的 HTML 数据解析,再发送请求,最终显示出页面
可以看出,浏览器和servlet不直接沟通,web服务器是媒介。下面这幅图片很好的描述了各种关系:
图:注意要区分Web服务器和应用服务器。Apache是Web服务器;Tomcat是Java应用服务器。Web服务器用来处理HTTP, URL, 和HTML;Tomcat用来处理Java的应用,JSP/Servlet。在开发Java的Web程序的时候,Apache和Tomcat配合在一起使用,Apache处理静态页面,Tomcat处理动态页面。
jsp
jsp经过编译,成为 java 类(此类的超类的超类,最终是 HttpServlet,因此也可以说 jsp 就是 servlet),所以 jsp 也是java。jsp
jsp 文件在第一次运行的时候,通过 jsp 引擎( JspServlet ),会生成对应的 java 文件和 class(位置:tomcat/work/Catalina/localhost/web项目名/org/apache/jsp)
jsp 语法分为三种不同的类型:
编译器指令:
1. page,页面设置 < %@ page … %>)
2. include,源码级别导入 < %@ include … %>)
3. taglib
脚本语法:
1. HTML注释,Servlet中会生成,会发给浏览器 < !– comments –>
2. 隐藏注释,Servlet中不会生成,不发给浏览器< %– comments –>
3. 声明,< %! … %>
4. 表达式,< %= … %>
5. java脚本段,< % … %>,在这里直接写入 java 代码,变量,方法等
动作语法:
< jsp:forward >, < jsp:include >, < jsp:plugin >等
补充
servlet 和 jsp 都是属于 Java EE 内容。Servlet 负责相应请求产生数据,并把数据通过转发技术带给 JSP,数据显示用 JSP 来做。
比较 | 优势 | 劣势 |
---|---|---|
servlet | 逻辑处理方便 | 页面表现麻烦 |
jsp | 页面表现方便 | 逻辑处理麻烦 |
所以 servlet 和 jsp 互补使用,一般项目是 jsp + servlet + javabean 的配置。更高级的话,使用ssh的框架。
动态网页,数据动态生成的网页。所谓动态生成,就是网页事先不存在,而是浏览器发出请求的时候,临时生成。它以数据库技术为基础,不独立存在于服务器上面。相关的语言有 jsp,php,asp,cgi。
web.xml:是网络程序中一个很重要的配置文件,是 xml 的格式,servlet 的配置就在这里面。只在服务器启动的时候,加载一次。一个 web 项目有一个 web.xml,一个 web 项目有多个 servlet。从浏览器发出一个请求过来,服务器怎么从多个 servlet 中找到某一个需要的 servlet 呢? web.xml 中的配置通过映射,解决了这个问题。相关的标签有< servlet >,< servlet-name >, < servlet-class >, < servlet-mapping >< url-pattern >
图:输入url,找到对应的servlet类。步骤是1-2-3-4
Tomcat: 一个开源的Java应用服务器。和 Apache 服务器只支持静态网页相比,Tomcat 支持动态网页。Tomcat 提供了servlet 引擎,还可以解析 jsp,是开发和调试 servlet,jsp 的首选。
show me the code
jsp 代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Hello World, this is my JSP page.<br>
</body>
</html>
jsp 编译过后的 java 代码:(java代码比较冗余,拼凑字符串的感觉。。)
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.List _jspx_dependants;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
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');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body> \n");
out.write(" Hello World, this is my JSP page.<br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
用浏览器打开jsp文件: