在JSP中, 可以使用scriptlet标记将Java代码写入jsp页面。首先让我们看看脚本元素是什么。
JSP脚本元素
脚本元素提供了将Java代码插入jsp的功能。脚本元素共有三种类型:
- scriptlet标签
- 表达标签
- 声明标签
JSP scriptlet标签
scriptlet标记用于在JSP中执行Java源代码。语法如下:
<% java source code %>
JSP scriptlet标签示例
在此示例中, 我们显示欢迎消息。
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
打印用户名的JSP scriptlet标记示例
在此示例中, 我们创建了两个文件index.html和welcome.jsp。 index.html文件从用户那里获取用户名, 而welcome.jsp文件将显示用户名以及欢迎消息。
档案:index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
文件:welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
评论前必须登录!
注册