• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:JSP脚本的9个内置对象(1)


    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 增加Cookie </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    // 获取请求参数
    String name = request.getParameter("name");
    // 以获取到的请求参数为值,创建一个Cookie对象
    Cookie c = new Cookie("username" , name);
    // 设置Cookie对象的生存期限
    c.setMaxAge(24 * 3600);
    // 向客户端增加Cookie对象
    response.addCookie(c);
    %>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 中文Cookie </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    // 以编码后的字符串为值,创建一个Cookie对象
    Cookie c = new Cookie("cnName" 
        , java.net.URLEncoder.encode("孙悟空" , "gbk"));
    // 设置Cookie对象的生存期限
    c.setMaxAge(24 * 3600);
    // 向客户端增加Cookie对象
    response.addCookie(c);
    
    // 获取本站在客户端上保留的所有Cookie
    Cookie[] cookies = request.getCookies();
    // 遍历客户端上的每个Cookie
    for (Cookie cookie : cookies)
    {
        // 如果Cookie的名为username,表明该Cookie是需要访问的Cookie
        if(cookie.getName().equals("cnName"))
        {
            //使用java.util.URLDecoder对Cookie值进行解码
            out.println(java.net.URLDecoder
                .decode(cookie.getValue()));
        }
    }
    %>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>测试config内置对象</title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <!-- 直接输出config的getServletName的值 -->
    <%=config.getServletName()%>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>测试config内置对象</title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <!-- 输出该JSP名为name的配置参数 -->
    name配置参数的值:<%=config.getInitParameter("name")%><br/>
    <!-- 输出该JSP名为age的配置参数 -->
    age配置参数的值:<%=config.getInitParameter("age")%>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 取钱的表单页 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <!-- 取钱的表单 -->
    <form method="post" action="first.jsp">
        取钱:<input type="text" name="balance">
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" isErrorPage="true" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 异常处理页面 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    异常类型是:<%=exception.getClass()%><br/>
    异常信息是:<%=exception.getMessage()%><br/>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ page import="java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> request处理 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    // 获取请求的钱数
    String bal = request.getParameter("balance");
    // 将钱数的字符串转换成双精度浮点数
    double qian = Double.parseDouble(bal);
    // 对取出的钱进行判断
    if (qian < 500)
    {
        out.println("给你" + qian + "");
        out.println("账户减少" + qian);
    }
    else
    {
        // 创建了一个List对象
        List<String> info = new ArrayList<String>();
        info.add("1111111");
        info.add("2222222");
        info.add("3333333");
        // 将info对象放入request范围内
        request.setAttribute("info" , info);
    %>
    <!-- 实现转发 -->
    <jsp:forward page="second.jsp"/>
    <%}%>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCtype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 收集参数的表单页 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <form id="form1" action="request1.jsp">
    用户名:<br/>
    <input type="text" name="name"><hr/>
    性别:<br/>
    男:<input type="radio" name="gender" value="男">
    女:<input type="radio" name="gender" value="女"><hr/>
    喜欢的颜色:<br/>
    红:<input type="checkbox" name="color" value="红">
    绿:<input type="checkbox" name="color" value="绿">
    蓝:<input type="checkbox" name="color" value="蓝"><hr/>
    来自的国家:<br/>
    <select name="country">
        <option value="中国">中国</option>
        <option value="美国">美国</option>
        <option value="俄罗斯">俄罗斯</option>
    </select><hr/>
    <input type="submit" value="提交">
    <input type="reset" value="重置">
    </form>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>application测试</title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <!-- 直接输出application 变量值 -->
    <%=application.getAttribute("counter")%>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ page import="java.sql.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>application测试</title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    //从配置参数中获取驱动
    String driver = application.getInitParameter("driver");
    //从配置参数中获取数据库url
    String url = application.getInitParameter("url");
    //从配置参数中获取用户名
    String user = application.getInitParameter("user");
    //从配置参数中获取密码
    String pass = application.getInitParameter("pass");
    //注册驱动
    Class.forName(driver);
    //获取数据库连接
    Connection conn = DriverManager.getConnection(url,user,pass);
    //创建Statement对象
    Statement stmt = conn.createStatement();
    //执行查询
    ResultSet rs = stmt.executeQuery("select * from news_inf");
    %>
    <table bgcolor="#9999dd" border="1" width="480">
    <%
    //遍历结果集
    while(rs.next())
    {
    %>
        <tr>
            <td><%=rs.getString(1)%></td>
            <td><%=rs.getString(2)%></td>
        </tr>
    <%
    }
    %>
    <table>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%-- 通过contentType属性指定响应数据是图片 --%>
    <%@ page contentType="image/png" language="java"%>
    <%@ page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*"%>
    <%
    // 创建BufferedImage对象
    BufferedImage image = new BufferedImage(340 , 
        160, BufferedImage.TYPE_INT_RGB);
    // 以Image对象获取Graphics对象
    Graphics g = image.getGraphics();
    // 使用Graphics画图,所画的图像将会出现在image对象中
    g.fillRect(0,0,400,400);
    // 设置颜色:红
    g.setColor(new Color(255,0,0));
    // 画出一段弧
    g.fillArc(20, 20, 100,100, 30, 120);
    // 设置颜色:绿
    g.setColor(new Color(0 , 255, 0));
    // 画出一段弧
    g.fillArc(20, 20, 100,100, 150, 120);
    // 设置颜色:蓝
    g.setColor(new Color(0 , 0, 255));
    // 画出一段弧
    g.fillArc(20, 20, 100,100, 270, 120);
    // 设置颜色:黑
    g.setColor(new Color(0,0,0));
    g.setFont(new Font("Arial Black", Font.PLAIN, 16));
    // 画出三个字符串
    g.drawString("red:climb" , 200 , 60);
    g.drawString("green:swim" , 200 , 100);
    g.drawString("blue:jump" , 200 , 140);
    g.dispose();
    // 将图像输出到页面的响应
    ImageIO.write(image , "png" , response.getOutputStream());
    %>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ page import="java.sql.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> out测试 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    // 注册数据库驱动
    Class.forName("com.mysql.jdbc.Driver");
    // 获取数据库连接
    Connection conn = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/javaee","root","32147");
    // 创建Statement对象
    Statement stmt = conn.createStatement();
    // 执行查询,获取ResultSet对象
    ResultSet rs = stmt.executeQuery("select * from news_inf");
    %>
    <table bgcolor="#9999dd" border="1" width="400">
    <%
    // 遍历结果集
    while(rs.next())
    {
        // 输出表格行
        out.println("<tr>");
        // 输出表格列
        out.println("<td>");
        // 输出结果集的第二列的值
        out.println(rs.getString(1));
        // 关闭表格列
        out.println("</td>");
        // 开始表格列
        out.println("<td>");
        // 输出结果集的第三列的值
        out.println(rs.getString(2));
        // 关闭表格列
        out.println("</td>");
        // 关闭表格行
        out.println("</tr>");
    }
    %>
    <table>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> pageContext测试 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <%
    // 使用pageContext设置属性,该属性默认在page范围内
    pageContext.setAttribute("page","hello");
    // 使用request设置属性,该属性默认在request范围内
    request.setAttribute("request","hello");
    // 使用pageContext将属性设置在request范围中
    pageContext.setAttribute("request2","hello" 
        , pageContext.REQUEST_SCOPE);
    // 使用session将属性设置在session范围中
    session.setAttribute("session","hello");
    // 使用pageContext将属性设置在session范围中
    pageContext.setAttribute("session2","hello" 
        , pageContext.SESSION_SCOPE);
    // 使用application将属性设置在application范围中
    application.setAttribute("app","hello");
    // 使用pageContext将属性设置在application范围中
    pageContext.setAttribute("app2","hello" 
        , pageContext.APPLICATION_SCOPE);
    //下面获取各属性所在的范围:
    out.println("page变量所在范围:" + 
        pageContext.getAttributesScope("page") + "<br/>");
    out.println("request变量所在范围:" +
        pageContext.getAttributesScope("request") + "<br/>");
    out.println("request2变量所在范围:"+
        pageContext.getAttributesScope("request2") + "<br/>");
    out.println("session变量所在范围:" +
        pageContext.getAttributesScope("session") + "<br/>");
    out.println("session2变量所在范围:" +
        pageContext.getAttributesScope("session2") + "<br/>");
    out.println("app变量所在范围:" +
        pageContext.getAttributesScope("app") + "<br/>");
    out.println("app2变量所在范围:" + 
        pageContext.getAttributesScope("app2") + "<br/>");
    %>
    </body>
    </html>
    <%@ page contentType="text/html; charset=gb2312" language="java" %>
    <%@ page import="java.util.*"%>
    <%
    //取出session范围的itemMap属性
    Map<String,Integer> itemMap = (Map<String,Integer>)session
        .getAttribute("itemMap");
    //如果Map对象为空,则初始化Map对象
    if (itemMap == null)
    {
        itemMap = new HashMap<String,Integer>();
        itemMap.put("书籍" , 0);
        itemMap.put("电脑" , 0);
        itemMap.put("汽车" , 0);
    }
    //获取上个页面的请求参数
    String[] buys = request.getParameterValues("item");
    //遍历数组的各元素
    for (String item : buys)
    {
        //如果item为book,表示选择购买书籍
        if(item.equals("book"))
        {
            int num1 = itemMap.get("书籍").intValue();
            //将书籍key对应的数量加1
            itemMap.put("书籍" , num1 + 1);
        }
        //如果item为computer,表示选择购买电脑
        else if (item.equals("computer"))
        {
            int num2 = itemMap.get("电脑").intValue();
            //将电脑key对应的数量加1
            itemMap.put("电脑" , num2 + 1);
        }
        //如果item为car,表示选择购买汽车
        else if (item.equals("car"))
        {
            int num3 = itemMap.get("汽车").intValue();
            //将汽车key对应的数量加1
            itemMap.put("汽车" , num3 + 1);
        }
    }
    //将itemMap对象放到设置成session范围的itemMap属性
    session.setAttribute("itemMap" , itemMap);
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> 购买的物品列表 </title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    您所购买的物品:<br/>
    书籍:<%=itemMap.get("书籍")%><br/>
    电脑:<%=itemMap.get("电脑")%><br/>
    汽车:<%=itemMap.get("汽车")%><p><a href="shop.jsp">再次购买</a></p>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>application测试</title>
        <meta name="website" content="http://www.crazyit.org" />
    </head>
    <body>
    <!-- JSP声明 -->
    <%!
    int i;
    %>
    <!-- 将i值自加后放入application的变量内 -->
    <%
    application.setAttribute("counter",String.valueOf(++i));
    %>
    <!-- 输出i值 -->
    <%=i%>
    </body>
    </html>
  • 相关阅读:
    看了支付宝账单,我才知道我是『有钱人』
    用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
    测试开发城市群,第一站深圳,我们来了!
    测试工作常用 Linux/ Shell 命令之 Bash 逻辑控制基础
    性能测试实战 | JMeter 录制/回放做 App 后端性能压测
    测试圣诞狂欢,万元红包、冲榜豪礼等你拿!
    自动化测试工具 or 自动化策略?孰轻孰重?
    Java 面试题
    疫情之下,普通人高薪就业指南:方向对了,路就不会遥远!
    SharePoint WebPart:扩展SharePoint 2007中图片展示功能
  • 原文地址:https://www.cnblogs.com/tszr/p/12363891.html
Copyright © 2020-2023  润新知