以下为创建页面的顺序:
注册界面(zhuye.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>Insert title here</title>
</head>
<body>
<form action="zhuye2.jsp" method="post">
<h1 > 欢迎光临!</h1>
请输入您的用户名:<input type="text" name="name"><br>
请输入您的密码:<input type="password" name="pass"><br><br>
<input type="submit" value="注册"> <input type="reset" value="重置">
<%
String name=request.getParameter("name"); //获取name的值
if(name==null)
name="";
else
session.setAttribute("name", name); //将用户名的信息保存到关键字为name中
%>
</form>
</body>
</html>
点餐界面(diancan.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>Insert title here</title>
</head>
<body>
<form action="jiesuan.jsp" method="post">
<h1>菜品</h1>
<input type="checkbox" name="cai" value="鸭肠15元">鸭肠15元<br>
<input type="checkbox" name="cai" value="毛肚40.5元">毛肚40.5元<br>
<input type="checkbox" name="cai" value="牛肉48.3元">牛肉48.3元<br>
<input type="checkbox" name="cai" value="山药20元">山药20元<br>
<input type="checkbox" name="cai" value="小菜15.5元">小菜15.5元<br>
<input type="submit" value="提交"> <input type="reset" value="重置">
</form>
</body>
</html>
diancan2.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>Insert title here</title>
</head>
<body>
<%
String order[]=request.getParameterValues("data");
if(order!=null)
{
StringBuffer str=new StringBuffer();
for(int k=0;k<order.length;k++)
{
str.append(order[k]+"<br>");
}
session.setAttribute("order",str);
}
%>
</body>
</html>
diancan3.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>Insert title here</title>
</head>ss
<body>
<%! public String ss(String s)
{
try{
byte[] bb=s.getBytes("iso-8859-1");
s=new String(bb);
}catch(Exception e)
{ }
return s;
}
%>
</body>
</html>
结算界面(jiesuan.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>Insert title here</title>
</head>
<body>
<body bgcolor="f0fff0">
<% String username=(String)session.getAttribute("name");
if(username==null||username.length()==0)
{ out.print("返回到输入姓名"); } %>
<%StringBuffer order=(StringBuffer)session.getAttribute("order");
String order1=new String(order);
double sum=0;
String[] price=order1.split("[^0123456789.]");
if(price!=null)
{ for(String item:price)
{ try{ sum=sum+Double.parseDouble(item);
}catch(NumberFormatException e2)
{ } } }
%> <br><br><%=handleStr(username) %>点的餐:<br>
<%=handleStr(order1) %><br>
总付款:<%=sum %>
</body>
</html>
简单的对比
request.getParameter用的比较多,相对熟悉
request.getParameterValues(String name)是获得如checkbox类(名字相同,但值有多个)的数据。 接收数组变量 ,如checkobx类型
request.getParameter(String name)是获得相应名的数据,如果有重复的名,则返回第一个的值 . 接收一般变量 ,如text类型
例
1. 三个名字相同的text标签
<input type="text" name="test" value="1" />
<input type="text" name="test" value="2" />
<input type="text" name="test" value="3" />
request.getParameterValues("test") ----------------- [1, 2, 3]
request.getParameter("test") ------------------ 1