首页 > 代码库 > JSP复习整理(二)基本语法

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>JSP整理</title></head><body><%!int n = 1; %><%--声明一个变量 --%><%! public int count(){	return n++;}/* 声明一个方法  */	%><%//jsp程序代码out.println("You had kown it...");out.println("Welcome......");%><br><%="You are the "+count()+"个来到时光之旅的神。。" %><br>}<%=(new java.util.Date()).toLocaleString()%></body></html>

 技术分享

二、方法声明

<%@ 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>计算圆的面积</title></head><body><p>输入可爱的圆的半径吧:</p><form action="DecMethod.jsp" method="get" name="form"><input type="text" name="radius"><input type = "submit" name="submit" value="http://www.mamicode.com/计算" ></form><%!double area(double r){	return Math.PI * r * r;}double perimeter(double r){	return Math.PI*2*r;}%><%String s=request.getParameter("radius");if(s!=null){	try{		double r;		r=Double.parseDouble(s);		%>				<p>圆的面积为:<%= area(r)%>		<p>圆的周长为:<%= perimeter(r) %><%	}catch(Exception e){		out.println(e.getMessage());	}}%></body></html>

 输入半径-》x显示结果:

技术分享技术分享

三、forward

login.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>JSP:Forward</title></head><body bgcolor=#B39EA5><form method=get action=checklogin.jsp>  <table>     <tr>        <td>输入用户名:</td>        <td><input type=text name=name              value=http://www.mamicode.com/"/>    	   </jsp:forward>    	   <%    }    else{    	%>    	<jsp:forward page="login.jsp">    	    <jsp:param name="user" value="http://www.mamicode.com/"/>    	    </jsp:forward>    	    <%    }%></body></html>

 success.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>success</title></head><body bgcolor=#B39EA5>登陆成功<br>welcome!!!<%=request.getParameter("user") %></body></html>

 结果:

技术分享技术分享技术分享

 

 先写到这了。。

JSP复习整理(二)基本语法