zoukankan      html  css  js  c++  java
  • JavaWeb与JSP初识

    JavaWeb执行过程

    目录结构

    1. Web程序部署在Tomcat的/webapps下面。

    2. 一个webapps文件夹可以部署多个不同的Web应用,webapps/web1,webapps/web2。

    3. 如果不适用上下文路径,Web程序需放到webapps/ROOT下面。ROOT文件下的程序使用http://localhost:8080访问。

    JSP的特点

    HTMl代码与Java程序共同存在。

    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    <%
        Locale locale = request.getLocale();
        Calendar calendar = Calendar.getInstance(locale); // 获取用户所在地的时间
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取小时
        String greeting = "";
        if (hour <= 6) {
            greeting = "凌晨好,您该睡觉了。良好的睡眠是美好一天的开始。";
        } else if (hour <=9 ) {
            greeting = "早上好,早餐要有营养。";
        } else if (hour <= 12) {
            greeting = "上午好,工作注意保护眼睛。";
        } else if (hour <=18) {
            greeting = "下午好,小心工作打瞌睡。";
        } else if (hour <= 24) {
            greeting = "晚上好,注意休息,睡觉不要太晚哦~";
        }
    %>
    <html>
      <head>
        <title>来自智能管家的问候</title>
      </head>
      <body>
      <%= greeting%>
      </body>
    </html>
    
    

    JSP脚本

    JSP脚本必须使用"<%"与"%>"括起来,否则会被视为模板数据。

    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    <%
        int num = 10;
        int result = 1;
        for (int i=1;i<=num;i++) {
            result *= i;
        }
        out.println("数字" + num + "的阶乘为:"+result);
    %>
    <html>
      <head>
        <title>jsp测试</title>
      </head>
      <body>
      </body>
    </html>
    

    结果:数字10的阶乘为:3628800

    JSP输出

    <%= 
        // something
    %>
    
    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    <%
        int num = 10;
        int result = 1;
        for (int i=1;i<=num;i++) {
            result *= i;
        }
    
    %>
    <html>
      <head>
        <title>jsp测试</title>
      </head>
      <body>
       数字 <%= num%>的阶乘为:<%= result%>
      </body>
    </html>
    

    JSP注释

    <%--
        这是JSP注释,可以添加多行注释
    --%>
    

    JSP方法使用

    <%!
        // 方法
    %>
    

    out.println()输出到客户端。
    在out.println()中,out是response的实例,是以response为对象进行流输出的,即将内容输出到客户端。

    System.out.println()打印在控制台当中。
    System.out.println()用的是标准输出流,这个是输出在控制台上的,而JSP不是控制台程序。不管是在JSP还是在JAVA程序中,System.out.println()都是打印在控制台上。 如果想打印在页面,简单点的方法是:
    out.print( "要打印的内容" );

    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    <%!
       String t = "";
       public void test() {
           t = "JSP方法测试";
       }
    %>
    <html>
      <head>
        <title>jsp测试</title>
      </head>
      <body>
       <%
           test();
       %>
       <%= t%>
      </body>
    </html>
    

    JSP 中if语句的使用

    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    
    <html>
      <head>
        <title>jsp测试</title>
      </head>
      <body>
       <%
           String param = request.getParameter("param");
           if ("1".equals(param)) {
       %>
       Java
       <%
           }
           else if ("2".equals(param)) {
       %>
       PHP
       <%
           }
       %>
    
      </body>
    </html>
    

    for循环

    <%--
      Created by IntelliJ IDEA.
      User: e550
      Date: 2017/1/9
      Time: 23:24
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
    
    <html>
      <head>
        <title>jsp测试</title>
      </head>
      <body>
       <%
           for(int i=1;i<=10;i++) {
       %>
       <div>
           <%out.print("hello world!"+i);%>
       </div>
       <%
           }
       %>
    
      </body>
    </html>
    

    方法论:感觉很像php。

  • 相关阅读:
    LeetCode Count of Range Sum
    LeetCode 158. Read N Characters Given Read4 II
    LeetCode 157. Read N Characters Given Read4
    LeetCode 317. Shortest Distance from All Buildings
    LeetCode Smallest Rectangle Enclosing Black Pixels
    LeetCode 315. Count of Smaller Numbers After Self
    LeetCode 332. Reconstruct Itinerary
    LeetCode 310. Minimum Height Trees
    LeetCode 163. Missing Ranges
    LeetCode Verify Preorder Serialization of a Binary Tree
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6284505.html
Copyright © 2011-2022 走看看