zoukankan      html  css  js  c++  java
  • jsp的一些基本语法

    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>
      <%! public void a(){} %> <!-- 通过声明, 可在servlet中定义方法,注意前面的"!"符合 -->
      <%! static{System.out.println("static");} %>
        <% 
        Date date=new Date();
        String time=date.toLocaleString();
        out.write(time);
         %>
         <br>
         <%=time%> <!-- 脚本表达式,用于向浏览器输出数据 -->
         
         <%for(int i=0;i<5;i++){ %>
         hello for
         <%} %>
         
         <%-- jsp注释内容 --%>
      </body>
    </html>

    生成servlet的内容:

    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 {
    
     public void a(){} 
     static{System.out.println("static");} 
      private static final javax.servlet.jsp.JspFactory _jspxFactory =
              javax.servlet.jsp.JspFactory.getDefaultFactory();
    
      private static java.util.List<java.lang.String> _jspx_dependants;
    
      private javax.el.ExpressionFactory _el_expressionfactory;
      private org.apache.tomcat.InstanceManager _jsp_instancemanager;
    
      public java.util.List<java.lang.String> 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('
    ');
          out.write('
    ');
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    
          out.write("
    ");
          out.write("
    ");
          out.write("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    ");
          out.write("<html>
    ");
          out.write("  <head>
    ");
          out.write("    <base href="");
          out.print(basePath);
          out.write("">
    ");
          out.write("    
    ");
          out.write("    <title>My JSP 'index.jsp' starting page</title>
    ");
          out.write("    
    ");
          out.write("	<meta http-equiv="pragma" content="no-cache">
    ");
          out.write("	<meta http-equiv="cache-control" content="no-cache">
    ");
          out.write("	<meta http-equiv="expires" content="0">    
    ");
          out.write("	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    ");
          out.write("	<meta http-equiv="description" content="This is my page">
    ");
          out.write("	<!--
    ");
          out.write("	<link rel="stylesheet" type="text/css" href="styles.css">
    ");
          out.write("	-->
    ");
          out.write("
    ");
          out.write("  </head>
    ");
          out.write("  
    ");
          out.write("  <body>
    ");
          out.write("  ");
          out.write(" <!-- 閫氳繃澹版槑锛?鍙�湪servlet涓�畾涔夋柟娉曪紝娉ㄦ剰鍓嶉潰鐨刓"!"绗﹀悎 -->
    ");
          out.write("  ");
          out.write("
    ");
          out.write("    ");
     
        Date date=new Date();
        String time=date.toLocaleString();
        out.write(time);
         
          out.write("
    ");
          out.write("     <br>
    ");
          out.write("     ");
          out.print(time);
          out.write(" <!-- 鑴氭湰琛ㄨ揪寮忥紝鐢ㄤ簬鍚戞祻瑙堝櫒杈撳嚭鏁版嵁 -->
    ");
          out.write("     
    ");
          out.write("     ");
    for(int i=0;i<5;i++){ 
          out.write("
    ");
          out.write("     hello for
    ");
          out.write("     ");
    } 
          out.write("
    ");
          out.write("     
    ");
          out.write("     ");
          out.write("
    ");
          out.write("  </body>
    ");
          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);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }

    包含页面

    <%@ include file="/public/head.jsp" %>  静态包含
    <% request.getRequestDispatcher("/public/head.jsp").include(request, response); %> 动态包含

    如果是错误页面,可标记为错误页面,将可获取到错误信息。

    <%@ page language="java" import="java.util.*" isErrorPage="true" pageEncoding="UTF-8"%>

    <%
    String message = exception.getMessage();
    out.write(message);
    %>

     以下代码将先输出 haha 再输出 message ,因为数据最终都是通过response.getWriter()输出的,out.write()相当于另外一个缓冲区,out.write()执行

    完后将再进入response缓冲区,最后再将response的数据发给tomcat执行输出。

    <%
        String message = exception.getMessage();
        out.write(message);
        response.getWriter().write("haha");
    %>

     pageContext封装了所有域对象,经常用在自定义标签

        <%
            request.setAttribute("data", "aaa");
            pageContext.setAttribute("data", "bbb",PageContext.REQUEST_SCOPE);
            String data = (String) pageContext.getAttribute("data",
                    PageContext.REQUEST_SCOPE);
            out.write(data);
            
            String data2=(String)pageContext.findAttribute("data");
            out.write(data2);
        %>
        ${data} <%-- el表达式 --%>
        
        <% pageContext.forward("/index2.jsp"); %>
        <% pageContext.include("/public/head.jsp"); %>

    常用jsp标签

    <jsp:forward page="/index2.jsp"></jsp:forward>
    <jsp:include page="/public/head.jsp"></jsp:include>  <!-- 动态标签 -->
    <jsp:forward page="/servlet/testServlet?data2=123"> 
    <jsp:param value="value" name="data" />
    </jsp:forward>


    取参数

    String data=(String)request.getParameter("data");

    System.out.println("data:"+data);

    String data2=(String)request.getParameter("data2");
    System.out.println("data2:"+data2);

    将jsp页面映射到servlet

        <servlet>
            <servlet-name>xxx</servlet-name>
            <jsp-file>/index3.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>xxx</servlet-name>
            <url-pattern>/1.html</url-pattern>
        </servlet-mapping>
  • 相关阅读:
    IT职场求生法则
    设计模式六大原则
    非win7系统访问win7系统发布的网站
    C#自定义导出Excel
    js操作table元素,表格的行列新增、删除汇集
    一个真正合格的程序员应该具备的素质
    项目心得
    项目心得1
    Spring boot 使用profile完成不同环境的maven打包功能
    关于std容器类的内存使用
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3404830.html
Copyright © 2011-2022 走看看