@nalan90
2018-06-29T11:16:07.000000Z
字数 3748
阅读 686
JAVAEE开发
<jsp:forward>
动作<jsp:param>
动作<jsp:plugin>
动作
<%@ include file="URL"%>
## date.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current = sdf.format(new Date());
out.println(current);
%>
## include_command.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>include指令</h1>
<hr>
<%@ include file="date.jsp" %>
</body>
</html>
<jsp:include page="URL" flush="true|false" />
## date.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current = sdf.format(new Date());
out.println(current);
%>
## include_action.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>include动作</h1>
<hr>
<jsp:include page="date.jsp" flush="true"/>
</body>
</html>
<%@ include file="URL">
<jsp:include page="URL" flush="true|false"/>
<jsp:forward page="URL"/>
request.getRequestDispatcher("/url").forward(request, response);
两者等价
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="forward_action.jsp" name="loginForm" method="post">
用户名: <input type="text" name="username" ><br>
密码:<input type="password" name="password" ><br>
<input type="submit" value="login">
</form>
</body>
</html>
## forward_action.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<jsp:forward page="user.jsp"/>--%>
<%
request.getRequestDispatcher("user.jsp").forward(request, response);
%>
## user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户资料</h1>
<%
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");
}
%>
用户名:<%=username%><br>
密码:<%=password%><br>
</body>
</html>
<jsp:param name="参数名" value="参数值"/>
常常与<jsp:forward>一起使用,作为其子标签
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp" name="loginForm" method="post">
用户名: <input type="text" name="username" ><br>
密码:<input type="password" name="password" ><br>
<input type="submit" value="login">
</form>
</body>
</html>
## dologin.jsp
<%@ page contentType="text/html;charset=UTF-8" import="java.net.*" language="java" %>
<jsp:forward page="user.jsp">
<jsp:param name="username" value="zhangshuang"/>
<jsp:param name="email" value="admin@admin.com"/>
</jsp:forward>
## user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户资料</h1>
<%
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%><br>
密码: <%=password%><br>
邮箱: <%=email%>
</body>
</html>