JSP页面最终是编译为Servlet执行的,你可以在Tomcat的%CATALINA_HOME%/work/Catalina/localhost中找到一个文件夹,该文件夹跟你的web-project的虚拟路径一致!比如说,我在%CATALINA_HOME%/conf/server.xml中配置了我的web-project为如下
<Context path="/mldn" docBase="E:/web.workspace/mldndemo/WebContent" reloadable="true"/>
那么在%CATALINA_HOME%/work/Catalina/localhost中就有一个文件夹名为“mldn”!
并且,你在\mldn\org\apache\jsp目录下还会找到你所有jsp页面对应的.class文件,这就证明了JSP页面最终是编译为Servlet执行的!
所以,本质上一个JSP页面对应一个Servlet!
因此,看到web.xml如下的配置,不要奇怪!因为JSP本质上就是Servlet!你看看JSP页面的代码,我们完全可以将其写为Servlet!
web.xml
<servlet>
<servlet-name>dbinit</servlet-name>
<jsp-file>/WEB-INF/init.jsp</jsp-file>
<init-param>
<param-name>driver</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mldn</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dbinit</servlet-name>
<url-pattern>/config.mldn</url-pattern>
</servlet-mapping>
init.jsp(输入http://127.0.0.1:8080/mldn/config.mldn访问)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>MLDN</title>
</head>
<body>
<%
String dbDriver = config.getInitParameter("driver");
String dbURL = config.getInitParameter("url");
%>
<h3>驱动程序:<%=dbDriver %></h3>
<h3>链接地址:<%=dbURL %></h3>
</body>
</html>
等价的Servlet:
package org.lxh.servletdemo; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DBInitServlet extends HttpServlet {
private String dbDriver = null, dbURL = null;
@Override
public void init(ServletConfig config) throws ServletException {
// Javadoc: When overriding this form of the method, call super.init(config).
super.init(config);
this.dbDriver = config.getInitParameter("driver");
this.dbURL = config.getInitParameter("url");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("<h3>驱动程序:" + dbDriver + "</h3>");
writer.println("<h3>链接地址:" + dbURL + "</h3>");
} protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {
this.doGet(req, resp);
}
}
该Servlet的配置
<!-- DBInitServlet -->
<servlet>
<servlet-name>dbInitServlet</servlet-name>
<servlet-class>
org.lxh.servletdemo.DBInitServlet
</servlet-class>
<init-param>
<param-name>driver</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mldn</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dbInitServlet</servlet-name>
<url-pattern>/DBInitServlet</url-pattern>
</servlet-mapping>
调用该Servlet的页面:
dbinit_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/DBInitServlet"/>
</body>
</html>
这二者显示的结果是一样的!