标签:
安装配置好jdk。tomcat,我用的版本号是7.0.54,我放在C:\server\apache-tomcat-7.0.54下,
CATALINA_BASE:C:\server\apache-tomcat-7.0.54
CATALINA_HOME:C:\server\apache-tomcat-7.0.54
也能够在Path以下加入配置 ;C:\server\apache-tomcat-7.0.54\bin ,这样就能够在cmd下直接输入startup启动服务器了。
背景:运用web容器tomcat和jsp Servlet 技术建立第一个Hello,文件文件夹例如以下:
web.xml这个文件能够在C:\server\apache-tomcat-7.0.54\webapps\examples\WEB-INF下找到,加入默认的页面即可了(默认是index.html,index.htm,index.jsp)
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ 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>java web</title> </head> <body> Java Web HelloWorld<br /> <% out.println(new java.util.Date());%> <%! public int count; public String info(){ return "hello"; } %> <% out.println(count++); %> <%=info() %> </body> </html>通过Tomcat容器,我们能够查看到编译后的代码。详细地址在C:\server\apache-tomcat-7.0.54\work文件夹下:
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { public int count; public String info(){ return "hello"; } 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("<!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=UTF-8\">\r\n"); out.write("<title>java web</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\tJava Web HelloWorld<br />\r\n"); out.write("\t"); out.println(new java.util.Date()); out.write('\r'); out.write('\n'); out.write(' '); out.write('\r'); out.write('\n'); out.write(' '); out.println(count++); out.write('\r'); out.write('\n'); out.write(' '); out.print(info() ); out.write("\r\n"); 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); } } }
Java类中能够看到有Init,destroy,service这些我们在Servlet里面看到的方法。事实上Jsp本质上也就是一个Servlet,是一个Java类。
而这一个转换过程须要的Web容器Tomcat起到了居功至伟的作用。终于对应的页面会以HTML形式展现给浏览器。
对于Java Web应用而言,WEB-INF是一个特殊的目录,Web容器会包括该目录的内容,client浏览器无法訪问WEB-INF路径下的不论什么内容。
<%%> 标签是JSP差别HTML标签的一个重要标志,里面能够写Java代码。构成JSP的动态部分。
<%=%>是JSP的输出表达式,输出表达式后不能有分号。
<:%!%>是声明标签,编译过后我们看到他在最外层,假设没有!号。将编译报错。(等于说把这段代码搁在解析方法里面了,由于不能有方法中的方法啊~)
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/5135152.html