java web jsp初识

Jsp初识

主要内容

  • <%>,<%=%>,<%!>的使用
  • application做一个累加计数器

<%>,<%=%>,<%!>的使用

首先,我们要了解jsp运行原理。JSP的本质就是一个Servlet,JSP的运行之前会先被Tomcat服务器翻译为.java文件,然后在将.java文本编译
为.class文件,而我们在访问jsp时,处理请求的就是那个翻译后的类。

  • <%%>叫做脚本片段,其中写的内容会翻译在Servlet的Service方法中,显然我们可以在Service方法中定义局部变量或者调用其他方法,但是不能在Service中再定义其他的方法,也就是我们可以在<%%>中定义局部变量或者调用方法,
  1. <%!%>称作声明,其中写的内容将来会直接翻译在Servlet类中,因为我们可以在类中定义方法和属性以及全局变量,所以我们可以在<%!%>中声明方法、属性、全局变量。

  2. . <%=%>称作jsp表达式,用于将已经声明的变量或者表达式输出到网页上面

  3. 直接写在jsp页面中的代码称作模板元素,将来会Servlet的Service方法中的out.write("___")中,作为输出内容。

在jsp做登录界面

  1. 改用<%%>用于写入java代码,用于调用方法,判断验证码是否错误
 String loginName = request.getParameter("loginName");
    String loginPwd = request.getParameter("loginPwd");
    String validCode=request.getParameter("validCode");

    String code=(String)request.getSession().getAttribute("code");
    String msg=null;
    if(!StrUtil.isBlank(validCode))
    {
        if(validCode.toLowerCase().equals(code.toLowerCase())){
            if ("123".equals(loginName) && "1234".equals(loginPwd)) {
                msg = "Hello" + loginName;
            } else {
                msg = "账号或者密码错误";
            }
        }else {
            msg ="验证码错误";
        }
    }else {
        msg="请输入验证码";
    }


%>
  1. <%=%>输出声明的变量和表达式
  2. 内部可以直接用web代码来表示
<%= "这次是你第"+count+"次刷新网页" %>
<form action='' method='post'>
    登录名称:<input type='text' name='loginName' value=''><br>
    登录密码:<input type='password' name='loginPwd' value=''><br>
    验证码:<input type='text' name='validCode' value=''><br><img src=./captche  width='100' height='100'><br>
    <input type='reset' name='reset' value='取 消'>
    <input type='submit' name='submit' value='登 录'>
    <%= msg %>
</form>

用application做一个简易的计数器

application存储的数据在整个web项目中可用。

//刷新次数
    int count =0;
    //application对全局有效,count获取application的值
    Object o=application.getAttribute("count");
    if(o!=null)
    {
        count=(Integer)o;
    }
    count++;
    application.setAttribute("count",count);

完整代码

<html>
<head>
    <title>Title</title>
</head>
<body>


<%
  //刷新次数
    int count =0;
    //application的getattribute对全局有效
    Object o=application.getAttribute("count");
    if(o!=null)
    {
        count=(Integer)o;
    }
    count++;
    application.setAttribute("count",count);

    String loginName = request.getParameter("loginName");
    String loginPwd = request.getParameter("loginPwd");
    String validCode=request.getParameter("validCode");

    String code=(String)request.getSession().getAttribute("code");
    String msg=null;
    if(!StrUtil.isBlank(validCode))
    {
        if(validCode.toLowerCase().equals(code.toLowerCase())){
            if ("3187102420".equals(loginName) && "12345".equals(loginPwd)) {
                msg = "Hello" + loginName;
            } else {
                msg = "账号或者密码错误";
            }
        }else {
            msg ="验证码错误";
        }
    }else {
        msg="请输入验证码";
    }


%>

<%= "这次是你第"+count+"次刷新网页" %>
<form action='' method='post'>
    登录名称:<input type='text' name='loginName' value=''><br>
    登录密码:<input type='password' name='loginPwd' value=''><br>
    验证码:<input type='text' name='validCode' value=''><br><img src=./captche  width='100' height='100'><br>
    <input type='reset' name='reset' value='取 消'>
    <input type='submit' name='submit' value='登 录'>
    <%= msg %>
</form>

</body>
</html>

效果如下
在这里插入图片描述

  • 不足
  • 输入完验证码会第一次就跳出来
  • 输入登录后的页面不是弹窗而是出现文本形式有待改进
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值