JAVA遇见HTML—JSP篇(六.JSP指令与动作元素)

本文介绍了JSP中的include指令与动作的区别,并通过实例展示了forward动作及param动作的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

include指令

语法:

<%@ include file="URL" %>

代码示例:

新建date.jsp文件


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.text.*"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
   Date d =new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
   String s = sdf.format(d);
   out.println(s);
%>

新建Include_Command.jsp文件

<%@ 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>
  <h1>Include指令</h1>
  <hr>
  <%@ include file="date.jsp" %>
</body>
</html>

Include动作

语法:

<jsp:include page="URL" flush="true|false" />

这里写图片描述

代码示例:

新建Include_Action.jsp文件

<%@ 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>
  <h1>Include动作</h1>
  <hr>
  <jsp:include page="date.jsp" flush="false"></jsp:include>
</body>
</html>

include指令与include动作之间的区别

这里写图片描述

forward动作

语法:

<jsp:forward page="URL" />

等同于:

request.getRequestDispatcher("/url").forward(request,response);

代码示例:
login.jsp文件

<%@ page language="java" import="java.util.*,java.net.*" 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>
   <h1>用户登录</h1>
   <hr>
   <%
        request.setCharacterEncoding("utf-8");
        String username = "";
        String password = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if (c.getName().equals("username")) {

                    username= URLDecoder.decode(c.getValue(), "utf-8");   
                }
                if(c.getName().equals("password")){
                    password = URLDecoder.decode(c.getValue(), "utf-8");
                }
            }

        }
    %>

   <form name="loginForm" action="dologin_param.jsp" method="post">
     <table>
       <tr>
         <td>用户名:</td>
         <td><input type="text" name="username" value="<%=username %>" /></td>
       </tr>

       <tr>
         <td>密码:</td>
         <td><input type="password" name="password" value="<%=password %>" />
       </tr>

       <tr>
         <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked" />十天内记住我的登录状态</td>
       </tr>

       <tr>
         <td colspan="2" align="center"><input type="submit" value="登录" /><input type="reset" value="取消" />
       </tr>

       <tr>
         <td colspan="2" align="center">
       </tr>

     </table>
   </form>
</body>
</html>

forward_Action.jsp文件

<%@ 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>
  <h1>Forward动作</h1>
  <hr>
  <!-- 相当于服务器内部跳转与request.getRequestDispatcher("users.jsp").forward(request, response);等价 -->
 <%--  <jsp:forward page="users.jsp"></jsp:forward> --%>
  <%
    request.getRequestDispatcher("users.jsp").forward(request, response);
  %>
</body>
</html>

users.jsp文件

<%@ page language="java" import="java.util.*,java.net.*"
    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>
    <h1>用户信息</h1>
    <hr>
    <%
        request.setCharacterEncoding("utf-8");
        String username="";
        String password="";
        if(request.getParameter("username") !=null){
              username = request.getParameter("username");
        }
        if(request.getParameter("password") !=null){
              password = request.getParameter("password");
        }
    %>
    <br>
    <br>
    <br>
    <br> 用户名:<%=username%>
    <br> 密码:<%=password%>
    <br>
</body>
</html>

param动作

语法:

<jsp:param name="参数名" value="参数值">

常常与<jsp:forward>一起使用,作为其子标签

dologin_param.jsp文件

<%@ 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>
  <jsp:forward page="users.jsp">
       <!-- 相当于给users.jsp传递了一个新的参数email -->
     <jsp:param value="admin@123.net" name="email"/>
  </jsp:forward>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值