JSP复习整理基本语法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="计算" > </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=<%=request.getParameter("user") %>></td> </tr> <tr> <td>输入密码:</td> <td><input type=password name=password></td> </tr> <tr colspan=2> <td><input type=submit value=login></td> </tr> </table> </form> </body> </html>
checklogin.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>checklogin</title> </head> <body bgcolor=#B39EA5> <% String name = request.getParameter("name"); String password = request.getParameter("password"); //if success forward--->success.jsp //else forward------>login.jsp if(name.equals("HaiXiao")&&password.equals("123456")){ %> <jsp:forward page="success.jsp"> <jsp:param name="user" value="<%=name %>"/> </jsp:forward> <% } else{ %> <jsp:forward page="login.jsp"> <jsp:param name="user" value="<%=name%>"/> </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复习整理基本语法的主要内容,如果未能解决你的问题,请参考以下文章