格式
"<jsp:action_name [attr="value"]+...</jsp:action name>
JSP常用动作:include(加载当前页面和引用页面的全部代码)
<jsp:include page="include/date.jsp"></jsp:include>
如果需要对引用页面传值需要使用param
<jsp:include page="include/date.jsp">
<jsp:param value="<%=str %>" name="str"/>
</jsp:include>
引用页面获取值的方式:<%=request.getParameter("str") %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat"%>
<%=request.getParameter("str") %>返回:String
<%=request.getParameterValues("str") %>返回:String[]
<br>
<strong>time:<%=new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").format(new Date()) %></strong>
跟<%@ include file="include/date.jsp" %>类似但<%@ include file="include/date.jsp" %>是嵌套一个页面(<%@ include file="include/date.jsp" %>在当前页面定义的声明可直接在嵌套页面里引用)
JSP常用动作:forward(加载引用页面的全部代码后不会加载当前页面代码)
格式:
1
<jsp:forward page="include/date.jsp">
<jsp:param value="<%=str %>" name="str"/>
</jsp:forward>
2
结果不会打印1,2
JSP常用动作:useBean
约等于
<%
/*
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String password=request.getParameter("pwd");
User user=new User();
user.setNamne(name);
user.setPassword(password);
request.setAttribute("user", user);
*/
%>
useBean代码
<jsp:useBean id="user" class="dd.User" scope="request">
<jsp:setProperty name="user" property="*"/>
<jsp:setProperty name="user" param="name" property="name"/>
<jsp:setProperty name="user" param="password" property="password"/>
</jsp:useBean>
整体使用步骤流程
1.index.jsp
<%@ page
contentType="text/html; charset=UTF-8"
errorPage="indexerr.jsp"
import="java.util.Date,java.util.List,java.util.ArrayList"
trimDirectiveWhitespaces="true"
%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="control/dologin.jsp" method="post">
<fieldset>
<legend>userlogin</legend>
<p>user:<input type="text" name="name"></p>
<p>password:<input type="password" name="password"></p>
<p>
<button type="reset">cancel</button>
<button type="submit">login</button>
</p>
</fieldset>
</form>
<%String str="hi"; %>
</body>
</html>
2.dologin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="dd.User"%>
<%
/*他和下方jsp:useBean类似
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String password=request.getParameter("pwd");
User user=new User();
user.setNamne(name);
user.setPassword(password);
request.setAttribute("user", user);
*/
%>
<jsp:useBean id="user" class="dd.User" scope="request">
<jsp:setProperty name="user" param="name" property="name"/>
<jsp:setProperty name="user" param="password" property="password"/>
</jsp:useBean>
<jsp:forward page="../wlecome.jsp"></jsp:forward>
wlecome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="dd.User"
%>
<%
User user=(User)request.getAttribute("user");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>wlecome<%=user.getName() %></title>
</head>
<body>
wlecome<%=user.getName() %>
wlecome<%=user.getPassword() %>
</body>
</html>
网友评论