jsp元素

时间:2023-03-09 01:30:36
jsp元素

1.指令元素:用于在JSP转换为Servlet阶段提供JSP页面的相关信息,如页面采用的字符编码集、页面中需要导入的类等信息,指令元素不会产生任何的输出到当前JSP的输出流中

指令元素有三种指令:
page指令:page指令是JSP中最常用的指令元素,page指令作用于整个JSP页面,定义了与页面相关的一些属性,通过该指令元素可以设置页面字符编码集、需要导入的类等等信息
include指令:用于在JSP页面中静态包含一个文件(可以是JSP、HTML页面、文本或者Java代码),特别是当多个页面包含共同的页面构成部分时,可以将这个共同部分抽取到一        HTML或者JSP页面,然后将这个页面 包含到其他页面中,如版权声明部分。JSP页面在转换为Servlet时,会在其中插入所包含的文本或者代码
taglib指令:

 <!-- 指令元素page -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.Date"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>
body {
margin: 0px
} </style>
<body>
<%
Date a=new Date();
System.out.print(a);
%>
<!-- 指令元素include -->
<%@ include file="include.jsp" %>
</body>
</html>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <table border="1" style="background-image: url('../imges/880689.jpg');width: 100%;height: 700px">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>

2.脚本元素:编写可在服务器端执行的Java代码块
  a .声明元素 :不产生输出,声明成员变量和方法

b.脚本段:产生输出

c.表达式:是Java语言中标准的表达式,请求处理计算表达式的值。

方法只能写在声明元素中,变量可以写在声明元素和脚本段中

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 脚本元素之声明元素-->
<%!
String str="123";
public int add(int a,int b)
{
return (a+b);
} %>
<!-- 脚本元素之脚本段 -->
<%
out.print(add(2,3)); %>
<!-- 脚本元素之表达式 -->
<input type="text" value="<%=str%>"/> </body>
</html>

动作元素:

<jsp:forward>

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
String result = request.getParameter("userN");
out.print("<h2>欢迎用户:"+result+"登录成功!</h2>");
%> <body> </body>
</html>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
//获取用户名:
String userName = request.getParameter("userName");
//密码:
String password = request.getParameter("password"); %> <body> <script type="text/javascript">
var userName = "<%=userName%>";
var password = "<%=password%>";
alert(userName)
/* if("admin"== userName && "123" == password)
{
location.href="welcome.jsp?userN="+userName+"&pass="+password;
}
else
{ } */ </script> <%
if("admin".equals(userName) && "123".equals(password))
{
%>
<jsp:forward page="welcome.jsp">
<jsp:param value="<%=userName%>" name="userN"/>
</jsp:forward>
<% }
else{
%>
<jsp:forward page="login01.jsp"/>
<% }
%> </body>
</html>