javaweb学习(9)--jsp的基本

Posted by lizhao on 09-19,2016
<pre name="code" class="html"><html>
<title>forforward</title>
<head>
</head>
<body>
	forforward
	<br />		
	<%= request.getParameter("name")%>
	<%= request.getParameter("oldName")%>
	<%= request.getParameter("ccc")%>
</body>
</html>
ProgramCode<%out.println("HelloWorld");%>
<%!//全局变量int i = 1;%><%= /* 表达式,就是=号 */++i%>
<%= request.getParameter("name")%>

<% %>正常的程序代码

注释:

<%--    --%>

<%//   %>

<%/*       */   %>

后面三种就是后台注释

<% ! % >在这个感叹号里面,就是成员变量,在jsp销毁之前只有一份

<% !

inti =0;

voidmFunction();

% >

<%=  %> 表达式,字符串,只能有一行

2.<%@ page %>标签

<%@ page import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page errorPage="HelloWorldJsp.jsp" %><!--页面出错时候访问--> 
<html>
<title>Directive</title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<%@ include file="includeBar.jsp"%><!--//原封不动的将这个值拿过来,编译之前!--> 
	
	
</body>
</html>

下面是includeBar.jsp

<span style="white-space:pre">	</span><%
		out.println("HelloWorld");
	%>

import为下面函数所要引入的包

contentType为编码格式

errorPage为当当前页面出错的时候,跳转的页面

include (注意是@include)为编译整个文件之前就将那个文件拷过来了

3.由一个微型计算器来测试标签语句<jsp:include />,将会有几个页面组成,先发一下

输入界面calc.jsp

<%@ page import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>

<html>
<title>calc</title>
<head>
</head>
<body>
	<div align="center">
		<form method="post" action="Compute.jsp">
			选择要进行的运算:
			<input type="radio" name="compute" value="division" checked>
			除法
			<input type="radio" name="compute" value="multiplication"  >
			乘法
			
			<br />
			
			a:<input type="text" name="value1" >
			b:<input type="text" name="value2"  >
			<br />
		
			<input type="submit" name="submit"  value="计算结果">
		</form>
	</div>
	
</body>
</html>

用于判断的Compute.jsp

<%@ page language="java"%>

<%

	String value1 = request.getParameter("value1");
	String value2 = request.getParameter("value2");

%>

<%
	<span style="color:#ff0000;">if(request.getParameter("compute").equals("division")){ %>
		<jsp:include page="divide.jsp" flush="true">
			<jsp:param name="v1" value="<%= value1 %>" />
			<jsp:param name="v2" value="<%= value2 %>" />
		</jsp:include>
<%	}else{  %>
		<%@ include file="multiply.jsp" %></span>
	
<%}%>

具体实现除法的division.jsp

<%@ page import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>

<html>
<title>calc</title>
<head>
</head>
<body>
	<%
		try{
			float v1 = Float.parseFloat(request.getParameter("v1"));
			float v2 = Float.parseFloat(request.getParameter("v2")); 
			double result = v1/v2;
		
	%>
		<%= result%>
	<%
		
		}
		catch(Exception e){
			out.println("aaaaaaaaaa");
			e.printStackTrace();
		}
	%>
	
</body>
</html>

具体实现乘法的multiply.jsp

<%@ page contentType="text/html;charset=gb2312"%>

<html>
<title>calc</title>
<head>
</head>
<body>
	<%
		try{
			float v1 = Float.parseFloat(request.getParameter("value1"));
			float v2 = Float.parseFloat(request.getParameter("value2")); 
			double result = v1*v2;
		
	%>
		<%= result%>
	<%
		
		}
		catch(Exception e){
			out.println("不合法的数值乘数");
		}
	%>
	
</body>
</html>

我是感觉需要注意的地方是标红的那里,主要是:

request.getParameter("compute")  获取传过来的参数

<span style="color:#ff0000;"><span style="white-space:pre">		</span><jsp:include page="divide.jsp" flush="true">
			<jsp:param name="v1" value="<%= value1 %>" />
			<jsp:param name="v2" value="<%= value2 %>" />
		</jsp:include></span>

<jsp:include page="divide.jsp" flush="true"> 这种方式引入,就是按照编译顺序,一步一步下来,然后到了这里,然后添加进来编译。

<jsp:param name="v1" value="<%= value1 %>" />  加入这个页面时,带的参数。

5.转向目标网址forward和sendRedirect

forward.jsp

<html>
<title>forward</title>
<head>
</head>
<body>
	forward
	<jsp:forward page="forforward.jsp">
		<jsp:param name="name" value="m"/>
		<jsp:param name="oldName" value='<%= request.getParameter("name")%>'/>
		<jsp:param name="roles" value="manager"/>
	</jsp:forward>
</body>
</html>

forforward.jsp

<html>
<title>forforward</title>
<head>
</head>
<body>
	forforward
	<br />		
	<%= request.getParameter("name")%>
	<%= request.getParameter("oldName")%>
	<%= request.getParameter("ccc")%>
</body>
</html>

sendRedirect.jsp

<%
	response.sendRedirect("forforward.jsp");
%>

forward.jsp和sendRedirect.jsp加载的时候最后都是访问forforward.jsp

2者的区别是,当我们带了参数访问forward.jsp,这个jsp是直接将我们的网址原封不动的传给forforward.jsp,

而sendRedirect,则是我们访问的时候,先返回这个链接到客户端,由客户端重新发起了一个新的链接,这时候就没有参数了。