1.Jsp原理
jsp其实是一种特殊的Servlet:当jsp页面第一次被访问时,服务器会把jsp编译成java文件(servlet类);
再把java编译成.class;
然后创建该类对象;
最后调用它的service()方法;
第二次请求同一jsp时,直接调用service()方法。
在tomcat的work目录下可以找到jsp对应的.java源代码。
2.可以在web.xml中配置jsp
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <h1 style="color:orange;">现在时间是:</h1> <%= new java.util.Date() %> </body> </html>
可以直接访问index.jsp:http://localhost:8080/Javaweb01/index.jsp
也可以在web.xml里配置jsp,如下:
<!-- jsp也可以向Servlet那样在web.xml里面配置 --> <servlet> <servlet-name>index_jsp</servlet-name><!-- 名称任意 --> <jsp-file>/index.jsp</jsp-file><!-- 真实路径 --> </servlet> <servlet-mapping> <servlet-name>index_jsp</servlet-name><!-- 名称任意 --> <url-pattern>/zh_index</url-pattern><!-- 访问路径 --> </servlet-mapping>
访问路径可以是:http://localhost:8080/Javaweb01/zh_index