Day8_JavaWeb——jsp脚本、jsp表达式和jsp指令

一. 储备知识

1.1 jsp脚本片段

翻译:按照编写顺序拷贝到service方法中
编写:<% %>
作用:在service中编写java业务逻辑代码,可通过多个jsp脚本片段结合控制页面内容的显示

由于jsp脚本片段翻译后在service方法中使用,所以可以直接使用service方法中的参数request、response

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
       <%
           /*
                        由于jsp脚本片段翻译后在service方法中使用,
                        所以可以直接使用service方法中的参数request、response
           */
           //获取请求参数age
           String ageStr = request.getParameter("age");
           int age = 0;
           if(ageStr != null){
        	   age = Integer.parseInt(ageStr);
           }
           if(age > 18){
       %>
       <h4>你已经18岁了</h4>
       <%
           }
       %>
</body>
</html>

1.2 jsp表达式

翻译:作为out.print的参数写到响应体中
语法:<%= %>
作用:用来向页面中输出变量或对象的值

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
      <%
          String name = request.getParameter("username");
          if(name != null){
      %>
         <h2>name的值是:</h2><%=name %>
      <%
          }
      %>
</body>
</html>

1.3 jsp指令

介绍常用的include指令:静态包含指令,可以将其他页面中的内容拷贝到当前页面中

特点
将另一个页面拷贝到当前页面后,服务器再将当前页面翻译为Java类,并编译为class文件,最后执行(只有一个文件被翻译被翻译)
应用
将多个页面共有的部分提取为一个单独的页面,在多个页面中再通过include指令引入,便于共有部分的修改

好的,下面是一个简单的Java Web计算器的实现过程: 1. 首先创建一个JSP页面,用于显示计算器界面。在该页面中,需要有一些按钮,如数字、运算符、清除、等于等。 2. 创建一个JavaBean类,用于处理计算器的逻辑。在该类中,需要定义一些成员变量,如操作数1、操作数2、运算符等,以及一些方法,如加、减、乘、除等。 3. 在JSP页面中,使用`<jsp:useBean>`指令引用JavaBean类,并通过`<jsp:setProperty>`指令设置JavaBean类的属性。 4. 在JSP页面中,使用`<% %>`标签引用JavaBean类中的方法,用于处理用户的操作。例如,当用户点击加号按钮时,调用JavaBean类中的加法方法。 5. 最后,使用`<%= %>`标签输出计算结果。 下面是一个简单的Java Web计算器的代码实现: Calculator.jsp: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.bean.CalculatorBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <h1>Calculator</h1> <form method="post"> <table> <tr> <td colspan="4"><input type="text" name="result" value="${calculatorBean.getResult()}" /></td> </tr> <tr> <td><input type="button" value="7" onclick="document.forms[0].result.value += '7'" /></td> <td><input type="button" value="8" onclick="document.forms[0].result.value += '8'" /></td> <td><input type="button" value="9" onclick="document.forms[0].result.value += '9'" /></td> <td><input type="button" value="+" onclick="document.forms[0].operator.value = '+'" /></td> </tr> <tr> <td><input type="button" value="4" onclick="document.forms[0].result.value += '4'" /></td> <td><input type="button" value="5" onclick="document.forms[0].result.value += '5'" /></td> <td><input type="button" value="6" onclick="document.forms[0].result.value += '6'" /></td> <td><input type="button" value="-" onclick="document.forms[0].operator.value = '-'" /></td> </tr> <tr> <td><input type="button" value="1" onclick="document.forms[0].result.value += '1'" /></td> <td><input type="button" value="2" onclick="document.forms[0].result.value += '2'" /></td> <td><input type="button" value="3" onclick="document.forms[0].result.value += '3'" /></td> <td><input type="button" value="*" onclick="document.forms[0].operator.value = '*'" /></td> </tr> <tr> <td><input type="button" value="0" onclick="document.forms[0].result.value += '0'" /></td> <td><input type="button" value="C" onclick="document.forms[0].result.value = ''" /></td> <td><input type="submit" value="=" /></td> <td><input type="button" value="/" onclick="document.forms[0].operator.value = '/'" /></td> </tr> <tr> <td colspan="4"><input type="hidden" name="operator" /></td> </tr> </table> </form> </body> </html> ``` CalculatorBean.java: ```java package com.example.bean; public class CalculatorBean { private double operand1; private double operand2; private String operator; private double result; public CalculatorBean() { } public double getOperand1() { return operand1; } public void setOperand1(double operand1) { this.operand1 = operand1; } public double getOperand2() { return operand2; } public void setOperand2(double operand2) { this.operand2 = operand2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void add() { result = operand1 + operand2; } public void subtract() { result = operand1 - operand2; } public void multiply() { result = operand1 * operand2; } public void divide() { result = operand1 / operand2; } } ``` 在以上代码中,我们通过`<jsp:useBean>`指令引用了CalculatorBean类,并使用`<jsp:setProperty>`指令设置了CalculatorBean类的属性。在表单提交时,我们通过JavaScript获取到用户的操作数和运算符,然后调用相应的JavaBean方法处理计算逻辑,最后通过`<%= %>`标签输出计算结果。 希望这篇文章能够帮助你实现一个简单的Java Web计算器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值