JSP基础_0900_Action

Action(动作指令):在运行期间的指令
  常见的四种:
1.jsp:UserBean
jsp:setProperty
jsp:getProperty
2.jsp:include[以后常用,必须掌握]     请查看include/index.jsp
3.jsp:forward[企业常考和sendRedirect的区别]    请查看forward/index.jsp
jsp:param

4.jsp:plugin[用于嵌入applet程序,一般不用了]

a.jsp:include基本用法:

实验一:

//include/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Action</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
 <body>
您需要进行的运算:
<form id="form1" name="form1" method="post" action="include/compute.jsp">
  <p>
    <input name="compute" type="radio" id="radio" value="division" checked="checked" />
  除法 
  <input type="radio" name="compute" id="radio2" value="multiply" />
乘法</p>
  <p>请输入您的数:被除数(乘数):
    <input type="text" name="value1" id="value1" />
    除数(乘数):
    <input type="text" name="value2" id="value2" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="提交" />
  </p>
</form>
</body>

</html>
//include/compute.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transit\ional//EN">
<html>
  <head>
    <title>结果页面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%
   String oper = request.getParameter("compute");
   if(oper!=null&&oper.equals("division")){
	   String v1 = request.getParameter("value1");
	   String v2 = request.getParameter("value2");%>
	   <jsp:include page="division.jsp" flush="true">
	   		<jsp:param value="<%=v1 %>" name="v1"/>
	   		<jsp:param value="<%=v2 %>" name="v2"/>
	   </jsp:include>
	   <%   
   }else{%>
   <%@ include file="mutiply.jsp" %>
   <%}%>
  </body>
</html>
//include/division.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String v1 = request.getParameter("v1");
String v2 = request.getParameter("v2");
if(v1!=null&&v2!=null){
	double d1 = Double.parseDouble(v1);
	double d2 = Double.parseDouble(v2);
	if(d2==0.0){
		out.println("除数不能为0");
	}else{
		double result = d1/d2;
		out.println("除法结果:result="+result);	
	}
}else{
	out.println("请输入数字!");
}
%>
//include/multiply.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String v1 = request.getParameter("value1");
String v2 = request.getParameter("value2");
if(v1!=null&&v2!=null){
	double d1 = Double.parseDouble(v1);
	double d2 = Double.parseDouble(v2);
	double result = d1*d2;
	out.println("乘积:result="+result);	
	
}else{
	out.println("请输入数字!");
}
%>


总结:如果不需要传入参数,直接使用Direction的include指令比较好,这样效率比较高;但需要传入参数时,必须使用Action指令


你可能感兴趣的:(JSP基础_0900_Action)