jsp的脚本_JSP脚本标签

jsp的脚本

Scriptlet Tag allows you to write java code inside JSP page. Scriptlet tag implements the _jspService method functionality by writing script/java code. Syntax of Scriptlet Tag is as follows :

Scriptlet Tag允许您在JSP页面内编写Java代码。 Scriptlet标记通过编写脚本/ Java代码来实现_jspService方法功能。 Scriptlet Tag的语法如下:

<% JAVA CODE %>

JSP:Scriptlet示例 (JSP: Example of Scriptlet)

In this example, we will show number of page visit.

在此示例中,我们将显示页面访问次数。

<html>
    <head>
        <title>My First JSP Page</title>
    </head>
    <%
        int count = 0;
    %>
    <body>
        Page Count is <% out.println(++count); %>
    </body>
</html>

We have been using the above example since last few lessons and in this scriptlet tags are used. Everything written inside the scriptlet tag is compiled as java code. Like in the above example, we initialize count variable of type int with a value of 0. And then we print it while using ++ operator to perform addition on it.

从上一课开始,我们一直在使用上面的示例,在此scriptlet中使用了标记。 scriptlet标记内编写的所有内容均被编译为Java代码。 像上面的示例一样,我们将int类型的count变量初始化为0。然后在使用++运算符对其执行加法操作时将其打印出来。

JSP makes it so easy to perform calculations, database interactions etc directly from inside the HTML code. Just write your java code inside the scriptlet tags.

JSP使直接从HTML代码内部执行计算,数据库交互等变得如此容易。 只需在scriptlet标记内编写Java代码即可。

JSP Scriptlet标签示例 (Example of JSP Scriptlet Tag)

In this example, we will create a simple JSP page which retrieves the name of the user from the request parameter. The index.html page will get the username from the user.

在此示例中,我们将创建一个简单的JSP页面,该页面从request参数中检索用户名。 index.html页面将从用户那里获取用户名。

index.html

index.html

<form method="POST" action="welcome.jsp">
    Name <input type="text" name="user" >
    <input type="submit" value="Submit">
</form>

In the above HTML file, we have created a form, with an input text field for user to enter his/her name, and a Submit button to submit the form. On submission an HTTP Post request ( method="POST" ) is made to the welcome.jsp file ( action="welcome.jsp" ), with the form values.

在上面HTML文件中,我们创建了一个表单,带有供用户输入其姓名的输入文本字段和一个提交按钮以提交表单。 提交后,将使用表单值对welcome.jsp文件( action=" welcome.jsp " )发出HTTP Post请求( method=" POST " )。

welcome.jsp

welcome.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome Page</title>
    </head>
    <%
      String user = request.getParameter("user");
    %>

    <body>
        Hello, <% out.println(user); %>
    </body>
</html>

As we know that a JSP code is translated to Servlet code, in which _jspService method is executed which has HttpServletRequest and HttpServletResponse as argument. So in the welcome.jsp file, request is the HTTP Request and it has all the parameters sent from the form in index.html page, which we can be easily get using getParameter() with name of parameter as argument, to get its value.

众所周知,JSP代码已转换为Servlet代码,在其中执行_jspService方法,该方法以HttpServletRequestHttpServletResponse作为参数。 因此,在welcome.jsp文件中, 请求是HTTP请求,它具有从index.html页中的表单发送的所有参数,我们可以轻松地使用以参数名称作为参数的getParameter()来获取其值。

混合scriptlet标签和HTML (Mixing scriptlet Tag and HTML)

Let's see how we can utilize the power of JSP scripting with HTML to build dynamic webpages with help of a few examples.

让我们看看如何借助几个示例,利用HTML的JSP脚本功能来构建动态网页。

If we want to create a Table in HTML with some dynamic data, for example by reading data from some MySQL table or file. How to do that? Here we will describe you the technique by creating a table with numbers 1 to n.

如果我们想用一些动态数据在HTML中创建一个表,例如通过从某些MySQL表或文件中读取数据。 怎么做? 在这里,我们将通过创建一个从1到n的表格来描述您的技术。

<table border = 1>
    for ( int i = 0; i < n; i++ ) {
        %>
        <tr>
        <td>Number</td>
        <td></td>
        </tr>
        
    }
%>
</table>

The above piece of code will go inside the <body> tag of the JSP file and will work when you initialize n with some value.

上面的代码将位于JSP文件的<body>标记内,并且在您使用某些值初始化n时将起作用。

Also, observer closely we have only included the java code inside the scriptlet tag, and all the HTML part is outside of it. Similarly we can do plenty of stuff.

同样,观察者们很接近,我们仅将Java代码包括在scriptlet标记内,而所有HTML部分都在其外部。 同样,我们可以做很多事情。

Here is one more very simple example :

这是另一个更简单的示例:


    if ( hello ) {
        %>
        <p>Hello, world</p>
        
    } else {
        %>
        <p>Goodbye, world</p>
        
    }
%>

Above code is using if-else condition to evaluate what to show, based on the value of a boolean variable named hello.

上面的代码基于一个名为hello布尔变量的值,使用if-else条件评估显示内容。

You can even ask user to enter the value of hello, using HTML Form and evaluate your JSP code based on that.

您甚至可以要求用户使用HTML Form输入hello的值,并根据该值评估您的JSP代码。

翻译自: https://www.studytonight.com/jsp/jsp-scriptlet-tag.php

jsp的脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值