JSP中include動做與指令

  include指令

  JSP中有三大指令:page,include,taglib,以前已經說過了page的用法。這裏介紹下include。html

  使用語法以下:java

<%@ include file="URL"%>

  好比有一個頁面要包含另外一個date.jsp頁面,date.jsp提供一個時間輸出:jsp

<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=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">
</head>
<body>
    <h1>include</h1>        
    <%@ include file="date.jsp"%>
</body>
</html>

  包含的date.jsp以下:ide

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    import="java.util.*" pageEncoding="utf-8"%>
<% SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:MM:ss"); Date d = new Date(); String s = sdf.format(d); out.println(s); %>

  運行結果:post

  查看生成的源碼,能夠發現,兩個頁面變成了一個文件:ui

  查看源碼發現,能夠更好的理解url

  include動做

  使用include動做標籤也能夠完成上述的操做,添加標籤以下:spa

<jsp:include page="date.jsp" flush="true" />

  能夠達到上面一樣的效果。.net

  觀察發現,此時訪問jsp生成了四個文件:code

  觀察源碼能夠更好的理解:

  include指令與動做的區別

  一張圖很好的說明了他們的區別(來源:慕課網):

  forward動做

  forward動做是使用jsp:forwad標籤實現:

<jsp:forward page="URL" />

  能夠達到與request.getRequestDispatcher("/url").forward(request,response)一樣的效果。

  param動做

  經常與forward標籤搭配使用,傳遞一些參數值:

<jsp:forward page="userForward.jsp">
        <jsp:param value="test@qq.com" name="email"/>
        <jsp:param value="666666" name="password"/>
    </jsp:forward>

  例如,登錄界面loginForward.jsp登陸用戶名密碼,通過處理界面doLoginForward.jsp處理後,修改密碼並新添加email參數後,轉發給顯示頁面userForward.jsp。

  loginForward.jsp代碼以下

<%@ page language="java" contentType="text/html; charset=utf-8"
    import="java.net.*" 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>用戶登陸</title>
</head>
<body>
    <h1>loginForward</h1>
    <hr>
    <form name="loginForm" action="doLoginForward.jsp" method="post">
        <table>
            <tr>
                <td>username</td>
                <td><input type="text" name="username" ></input></td>
            </tr>
            <tr>
                <td>password</td>
                <td><input type="password" name="password" ></input></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="submit"/></td>
            </tr>
        </table>
    </form>
</body>
</html>
View Code

  doLoginForward.jsp代碼以下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    import="java.net.*" 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>用戶登陸</title>
</head>
<body>
    <h1>loginForward</h1>
    <hr>
    <jsp:forward page="userForward.jsp">
        <jsp:param value="test@qq.com" name="email"/>
        <jsp:param value="666666" name="password"/>
    </jsp:forward>
</body>
</html>
View Code

  userForward.jsp代碼以下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    import="java.net.*" 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>用戶登陸</title>
</head>
<body>
    <h1>userForward</h1>
    <hr>
    <% request.setCharacterEncoding("utf-8"); String username = ""; String password = ""; String email = ""; if(request.getParameter("username")!=null){ username = request.getParameter("username"); } if(request.getParameter("password")!=null){ password = request.getParameter("password"); } if(request.getParameter("email")!=null){ email = request.getParameter("email"); } %> 用戶名:<%=username %> 密碼:<%=password %> 郵箱:<%=email %>
    
</body>
</html>
View Code
相關文章
相關標籤/搜索