[关闭]
@nalan90 2018-06-29T11:16:07.000000Z 字数 3748 阅读 686

五、JSP的include指令

JAVAEE开发


内容

include 指令
  1. <%@ include file="URL"%>
  1. ## date.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <%@ page import="java.text.SimpleDateFormat" %>
  4. <%@ page import="java.util.Date" %>
  5. <%
  6. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. String current = sdf.format(new Date());
  8. out.println(current);
  9. %>
  10. ## include_command.jsp
  11. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  12. <html>
  13. <head>
  14. <title>Title</title>
  15. </head>
  16. <body>
  17. <h1>include指令</h1>
  18. <hr>
  19. <%@ include file="date.jsp" %>
  20. </body>
  21. </html>

include动作
  1. <jsp:include page="URL" flush="true|false" />
  1. ## date.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <%@ page import="java.text.SimpleDateFormat" %>
  4. <%@ page import="java.util.Date" %>
  5. <%
  6. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. String current = sdf.format(new Date());
  8. out.println(current);
  9. %>
  10. ## include_action.jsp
  11. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  12. <html>
  13. <head>
  14. <title>Title</title>
  15. </head>
  16. <body>
  17. <h1>include动作</h1>
  18. <hr>
  19. <jsp:include page="date.jsp" flush="true"/>
  20. </body>
  21. </html>

include指令与include动作比较

forward动作
  1. <jsp:forward page="URL"/>
  2. request.getRequestDispatcher("/url").forward(request, response);
  3. 两者等价
  1. ## login.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  3. <html>
  4. <head>
  5. <title>登录页面</title>
  6. </head>
  7. <body>
  8. <h1>用户登录</h1>
  9. <hr>
  10. <form action="forward_action.jsp" name="loginForm" method="post">
  11. 用户名: <input type="text" name="username" ><br>
  12. 密码:<input type="password" name="password" ><br>
  13. <input type="submit" value="login">
  14. </form>
  15. </body>
  16. </html>
  17. ## forward_action.jsp
  18. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  19. <%--<jsp:forward page="user.jsp"/>--%>
  20. <%
  21. request.getRequestDispatcher("user.jsp").forward(request, response);
  22. %>
  23. ## user.jsp
  24. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  25. <html>
  26. <head>
  27. <title>Title</title>
  28. </head>
  29. <body>
  30. <h1>用户资料</h1>
  31. <%
  32. request.setCharacterEncoding("utf-8");
  33. String username = "";
  34. String password = "";
  35. if (request.getParameter("username") != null) {
  36. username = request.getParameter("username");
  37. }
  38. if (request.getParameter("password") != null) {
  39. password = request.getParameter("password");
  40. }
  41. %>
  42. 用户名:<%=username%><br>
  43. 密码:<%=password%><br>
  44. </body>
  45. </html>

param动作
  1. <jsp:param name="参数名" value="参数值"/>
  2. 常常与<jsp:forward>一起使用,作为其子标签
  1. ## login.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  3. <html>
  4. <head>
  5. <title>登录页面</title>
  6. </head>
  7. <body>
  8. <h1>用户登录</h1>
  9. <hr>
  10. <form action="dologin.jsp" name="loginForm" method="post">
  11. 用户名: <input type="text" name="username" ><br>
  12. 密码:<input type="password" name="password" ><br>
  13. <input type="submit" value="login">
  14. </form>
  15. </body>
  16. </html>
  17. ## dologin.jsp
  18. <%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
  19. <jsp:forward page="user.jsp">
  20. <jsp:param name="username" value="zhangshuang"/>
  21. <jsp:param name="email" value="admin@admin.com"/>
  22. </jsp:forward>
  23. ## user.jsp
  24. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  25. <html>
  26. <head>
  27. <title>Title</title>
  28. </head>
  29. <body>
  30. <h1>用户资料</h1>
  31. <%
  32. request.setCharacterEncoding("utf-8");
  33. String username = "";
  34. String password = "";
  35. String email = "";
  36. if (request.getParameter("username") != null) {
  37. username = request.getParameter("username");
  38. }
  39. if (request.getParameter("password") != null) {
  40. password = request.getParameter("password");
  41. }
  42. if (request.getParameter("email") != null) {
  43. email = request.getParameter("email");
  44. }
  45. %>
  46. 用户名: <%=username%><br>
  47. 密码: <%=password%><br>
  48. 邮箱: <%=email%>
  49. </body>
  50. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注