<%@ page contentType="text/html;charset=gb2312" %>
 <html>
 <h1>计算器</h1>
 <br>
 <hr>
 <head>
 <script language="javascript">
 //写一个函数用来验证用户是不是空提交
 function checkNum()
 {
 <!--
     //判断num1是否为空
     if (form1.num1.value == "") {
         window.alert("num1不能为空!");
         return false;
     }

     //验证是否为一个数
     if (Math.round(form1.num1.value) != form1.num1.value) {
         window.alert("num1不是一个整数");
         return false;
     }
 -->
 }
 </script>
 </head>
 <body>
 <%
 //得到第一个数
     String g_num1 = request.getParameter("num1");
     String g_num2 = request.getParameter("num2");
     String flag = request.getParameter("flag");
     //res要放在外边定义,不然下面的<%=res%>访问不到
         ;
     if (g_num1 != null && g_num2 != null && flag != null) {
         int num1 = Integer.parseInt(g_num1);
         int num2 = Integer.parseInt(g_num2);
         res = ;

         if (flag.equals("+")) {
             res = num1 + num2;
         } else if (flag.equals("-")) {
             res = num1 - num2;
         } else if (flag.equals("*")) {
             res = num1 * num2;
         } else {
             res = num1 / num2;
         }
         String result = res + "";
     }
 %>
 <form name="form1", action="myCal.jsp">
 please input the first number:<input type="text" name="num1"><br>
 <select name="flag">
 <option value=+>+</option>
 <option value=->-</option>
 <option value=*>*</option>
 <option value=/>/</option>
 </select>
 <br>
 please input the second number:<input type="text" name="num2"><br>
 <input type="submit" value="Calculate", onclick="return checkNum()"><br>
 </form>
 <hr>
 结果为<%=res%>
 </body>
 </html>

简单计算器

//向一个界面发送数据可以用

session: 在服务器为客户端存放的一块内存。类型:<string, object>(名字,值)

session占服务器内存,一般不往session,保持用户信息,保持购物车

JSP的动作元素:

<jsp:include page=""/>(标记) 与  <% @include file="test.jsp" %> (指令)

include指令时在JSP容器转换JSP时将包含的内容插入到当前页面文件,也称静态包含;

include标记是在JSP页面执行过程中将包含页面的执行结果插入到当前页面的执行结果中,也称动态包含。

文件上传:

   <form action="uploadProcess.jsp" enctype="multipart/form-data" method="post">
         选择文件:<input type="file" name="fname">
         <input type="submit" value="上传">
     </form>
 <!-- 通过调用request对象的getInputStream()方法获得输入流对象,然后从输入流中读取数据并保存在文本文件upload.txt中。   -->
 <%
          ServletInputStream sis = request.getInputStream();
          String filePath = application.getRealPath("/upload.text");
          FileOutputStream fos = new FileOutputStream(filePath);
          int len = sis.read();
          System.out.println(filePath);
          ) {
              fos.write(len);
              len = sis.read();
          }
       %>

文件下载:

 <%
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=upload.jsp");

            InputStream is = application.getResourceAsStream("upload.jsp");
            ServletOutputStream os = response.getOutputStream();
            int len = is.read();
            ) {
                os.write(len);
                len = is.read();
            }
            os.close();
            is.close();
     %>

实现验证码

 <%
         response.setContentType("image/jpeg");

         //生成一个图片
         ;
         ;
         String sourceNum = "abcdefghijklmnopqrstuvwxyz";
         String sigleNum = "";
         String checkNum = "";
         ; i < ; i++) {
             ) % ;
             sigleNum = sourceNum.substring(index, index + );
             checkNum += sigleNum;
         }
         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
         Graphics g = image.getGraphics();
         g.setColor(Color.white);
         g.fillRect(, , width, height);
         g.setColor(Color.black);
         g.setFont());
         g.drawString(checkNum, , );

         ; i < height; i = i + ) {
             ; j < ; j++) {
                 int y = (int) (width * Math.random() % width);
                 g.drawLine(y, i, y, i);
             }
         }

         g.dispose();
         //输出到客户端
         ImageIO.write(image, "JPEG", response.getOutputStream());
      %>

JSP学习记录的更多相关文章

  1. 我的Spring学习记录(五)

    在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...

  2. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  3. Struts2学习记录-Value Stack(值栈)和OGNL表达式

    仅仅是学习记录.把我知道的都说出来 一.值栈的作用 记录处理当前请求的action的数据. 二,小样例 有两个action:Action1和Action2 Action1有两个属性:name和pass ...

  4. jsp学习:jsp学习阶段性总结2019.9.21

    Jsp学习 jsp语法格式: 脚本程序:<% 代码片段 %> jsp声明:<%! declaration; [ declaration; ]+ ... %> 表达式:<% ...

  5. 学习记录-java基础部分(一)

    学习记录-java基础部分(一) 参考:GitHub上的知名项目:javaGuide : https://github.com/Snailclimb/JavaGuide/blob/master/doc ...

  6. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

  7. Java 静态内部类与非静态内部类 学习记录.

    目的 为什么会有这篇文章呢,是因为我在学习各种框架的时候发现很多框架都用到了这些内部类的小技巧,虽然我平时写代码的时候基本不用,但是看别人代码的话至少要了解基本知识吧,另外到底内部类应该应用在哪些场合 ...

  8. Apache Shiro 学习记录4

    今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...

  9. JSP学习网站

    JSP学习网站 http://www.yiibai.com/jstl/ http://www.w3cschool.cc/jsp/jsp-jstl.html

随机推荐

  1. Web API 2 自定义默认Identity Table Name

    One of the first issues you will likely encounter when getting started with ASP.NET Identity centers ...

  2. 基于docker部署使用ELK+FileBeat日志管理平台

    Docker从狭义上来讲就是一个进程,从广义上来讲是一个虚拟容器,专业叫法为 Application Container(应用容器).Docker进程和普通的进程没有任何区别,它就是一个普通的应用进程 ...

  3. Android EditView 获取焦点 不弹出软键盘

    很简单的做法: 找到AndroidManifest.xml文件 然后在对应的activity中增加android:windowSoftInputMode="adjustPan" & ...

  4. Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)

    题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算  如c(8,3) 如果手算就是   8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...

  5. P2084 进制转换

    原题链接 https://www.luogu.org/problemnew/show/P2084 这个题的思路就是先将输入的数字存到字符数组里,然后求出这一串数字中的非0元素的个数total,并记录最 ...

  6. MT【260】单调函数

    设$f(x)$是定义在$(0,+\infty)$上的单调函数,且对定义域内的任意实数$x$,都有$f(f(x)-\log_2 x)=3$, 求$f(x)-f^{'}(x)=2$的解所在的区间.____ ...

  7. IDEA+Springboot+JRebel热部署实现

    步骤一:在IDEA中安装JRebel插件(File->settings->plugins->search in repositories),如下图 步骤二:安装完成之后,重启idea ...

  8. 【转】STM32擦除内部FLASH时间过长导致IWDG复位分析

    @20119-01-29 [小记] STM32擦除内部FLASH时间过长导致IWDG复位分析

  9. POJ2411 铺地砖 Mondriaan's Dream

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15962   Accepted: 923 ...

  10. LOJ#6282. 数列分块入门 6

    一个动态的插入过程,还需要带有查询操作. 我可以把区间先分块,然后每个块块用vector来维护它的插入和查询操作,但是如果我现在这个块里的vector太大了,我可能的操作会变的太大,所以这时候我需要把 ...