zoukankan      html  css  js  c++  java
  • JSP

    jsp三个指令,三种注释方法,jsp脚本写java代码三种方式

    <!-- page指令 -->
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <!-- 使用jsp指令导入jstl标签库 -->
        <!-- taglib指令 -->
     <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!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>
    <h3>第一行</h3>
    <!-- include指令 -->
    <%@include file="part1.jsp" %>
    <h3>第二行</h3>
    <h3>第三行</h3>
    <h3>第四行</h3>
    </body>
    
    
    
    <!--Html注释  -->
    <%--jsp注释 --%>
    
    <%
    //定义变量   java注释
    int a=1;
    int b=2;
    int c=a+b;
    %>
    
    <!-- 显示数据 -->
    <%=c 
    %>
    
    <!-- 定义java代码(方法) -->
    <%!
    public String show(){
        return "郑州";
    }
    %>
    <%=show() %>
    </html>

    jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,我们在jsp脚本中可以直接使用这9个对象

    <%@ 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>
    <!-- 改页面上的内容不需要重启服务器 -->
    <!-- 使用内置对象 -->
    <!-- uri通用资源定位 -->
    <%
    out.write("hello world");
    
    String url=request.getRequestURI();//获取当前请求路径(url)
    //out.write(url);
    
    //response.sendRedirect("part1.jsp");//跳转
    
    String name=config.getServletName();
    //out.write(name);
    //String girlName=config.getInitParameter("girlName");
    //out.write(girlName);
    
    String girlname=application.getInitParameter("girlName");//jsp中的application就是servletcontext
    out.write(girlname);
    
    %>
    </body>
    </html>

    四大作用域

    <%@ 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>
    <%
    //pageContext对象(域对象--- request  servletContext)
    //域的作用范围,只在本页面有效
    pageContext.setAttribute("name", "花花");
    %>
    <%=pageContext.getAttribute("name") %>
    
    
    <%
    //pageContext可以向其他域中存取数据(鸡肋----其他对象)
    //1代表pageContext[当前jsp页面]
    //2代表request[一次请求]
    //3代表session[作用返回一次会话间有效   一次会话包含n次请求]
    //4代表application[整个web生命周期    从项目开始运行到服务器关闭]
    pageContext.setAttribute("aa", "100", 4);
    session.setAttribute("bb", 500);
    %>
    <%=pageContext.getAttribute("aa", 4)%>
    
    <!-- 自动巡航找数据[从小到大域] -->
    <%=pageContext.findAttribute("bb") %>
    
    <%
    //通过pageContext对象可以获得其他8大对象(别人都是内置对象,为什么要用pageContext获取其他内置对象?(鸡肋))
    pageContext.getRequest();
    pageContext.getSession();
    pageContext.getServletContext();
    
    %>
    
    </body>
    </html>

    EL表达式和JSTL

    <%@ page language="java"  import="java.util.*" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
      <!--jstl(JSP Standard Tag Library),JSP标准标签库  目的:同el一样也是要代替jsp页面中的脚本代码-->  
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!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>
    <!-- el(Express Lanuage)表达式的出现就是为了简化页面脚本使用【脚本格式非常乱,现在不被推介使用】 -->
    <!-- el最主要的作用是获得四大域中的数据 -->
    取request域:<%=request.getAttribute("aaa") %><br>
    取request域:${requestScope.aaa}<br>
    取request域:${aaa}<br>
    取session域:<%=session.getAttribute("bbb") %><br>
    取session域:${bbb}<br>
    取session域:${sessionScope.bbb}<br>
    取application域:<%=application.getAttribute("ccc") %><br>
    取application域:${ccc}<br>
    取application域:${applicationScope.ccc}
    
    
    
    <!-- el也支持丰富的表达式操作-->
    <%
    String ss=null;
    Object ob=new Object();
    request.setAttribute("ssa", ss);
    request.setAttribute("oba", ob);
    %>
    <br>
    ${2*9+3}
    ${empty ssa}
    ${1==2?"小明":"小红"}
    
    
    
    
    <hr>
    
    <!-- 需求遍历集合list -->
    <%
    List list=new ArrayList();
    list.add("中国");
    list.add("日本");
    list.add("法国");
    list.add("泰国");
    request.setAttribute("list", list);
    
    int age=17;
    request.setAttribute("myage", age);
    
    for(Object item:list){
    %>
    <%=item %>
    <%}%>
    
    <hr>
    <c:forEach items="${list}" var="aaa">
    ${aaa}
    </c:forEach>
    
    <hr>
    <!-- 要在el表达式里面  里面比较 -->
    <c:if test="${myage>18}">
    成年
    </c:if>
    </body>
    </html>
  • 相关阅读:
    Erlang性能的八个误区
    Unity预览
    一步步实现cnblogs博客采集工具>实现辅助对话框
    Asp.Net MVC 必备插件MVC Route Visualizer(Visual Studio 2012 版)
    IBM SOA[ESB,BPM,Portal等]基础架构图解
    PowerShell收发TCP消息包
    Sonar安装使用篇
    在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke
    排序算法
    ASP.NET MVC Web API 学习增删改查
  • 原文地址:https://www.cnblogs.com/qfdy123/p/11176759.html
Copyright © 2011-2022 走看看