码迷,mamicode.com
首页 > Web开发 > 详细

【读书札记】建立第一个Web项目

时间:2014-08-04 21:41:08      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:web   web.xml   apache   

        安装配置好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,文件目录如下:

        bubuko.com,布布扣

        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>

          然后就是写jsp页面了(index.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>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);
    }
  }
}

          可以看到count被转化成了全局变量,所以当我们开启第二个浏览器刷新时,值会在前一个浏览器基础之上不断叠加。

         Java类中可以看到有Init,destroy,service这些我们在Servlet里面看到的方法,其实Jsp本质上也就是一个Servlet,是一个Java类。而这一个转换过程需要的Web容器Tomcat起到了居功至伟的作用。最终相应的页面会以HTML形式展现给浏览器。对于Java Web应用而言,WEB-INF是一个特殊的文件夹,Web容器会包含该文件夹的内容,客户端浏览器无法访问WEB-INF路径下的任何内容。

        <%%> 标签是JSP区别HTML标签的一个重要标志,里面可以写Java代码,构成JSP的动态部分。

       <%=%>是JSP的输出表达式,输出表达式后不能有分号。

       <:%!%>是声明标签,编译过后我们看到他在最外层,如果没有!号,将编译报错。(等于说把这段代码搁在解析方法里面了,因为不能有方法中的方法啊~)

      

      


【读书札记】建立第一个Web项目,布布扣,bubuko.com

【读书札记】建立第一个Web项目

标签:web   web.xml   apache   

原文地址:http://blog.csdn.net/pearyangyang/article/details/38373451

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!