jsp元素

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

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

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

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

 b.脚本段:产生输出

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

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

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <!--  脚本元素之声明元素-->
11 <%!
12 String str="123";
13 public int add(int a,int b)
14 {
15     return (a+b);
16 }
17 
18 %>
19 <!-- 脚本元素之脚本段 -->
20 <% 
21   out.print(add(2,3));
22   
23   
24 %>
25 <!--  脚本元素之表达式 -->
26 <input type="text" value="<%=str%>"/>
27 
28 </body>
29 </html>
View Code

 动作元素:

<jsp:forward>

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9   <%
10      String result = request.getParameter("userN");
11      out.print("<h2>欢迎用户:"+result+"登录成功!</h2>");
12   %>
13 
14 <body>
15 
16 </body>
17 </html>
View Code
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9  <%
10    //获取用户名:
11    String userName = request.getParameter("userName");
12     //密码:
13     String password = request.getParameter("password");
14  
15  %>
16 
17  
18 
19 <body>
20  
21   <script type="text/javascript">
22     var userName = "<%=userName%>";
23     var password = "<%=password%>";
24     alert(userName)
25 /*    if("admin"== userName && "123" == password)
26    {
27       location.href="welcome.jsp?userN="+userName+"&pass="+password;  
28    }
29    else
30    {
31        
32    } */
33  
34  </script>
35  
36  <%
37    if("admin".equals(userName) && "123".equals(password))
38    {
39    %>
40        <jsp:forward page="welcome.jsp">
41            <jsp:param value="<%=userName%>" name="userN"/>
42        </jsp:forward>
43   <% }
44   else{
45   %>
46       <jsp:forward page="login01.jsp"/>
47  <%  }
48   %>
49 
50   
51 
52 </body>
53 </html>
View Code
原文地址:https://www.cnblogs.com/zclqian/p/7218546.html