jsp内置对象的使用(二)

 

2.record.jsp:处理registor.jsp提交的数据,record.jsp接收text、

            textarea、radio、select的数据,并显示出来(注意汉字问题)。如果

            用户名和口令空转向regError.jsp(采用<jsp:forward>

                 record.jsp中用session存储新注册的用户信息。

                 record.jsp包含返回首页index.jsp的超链接。

            3.regError.jsp:显示注册错误的信息。

            4.index.jsp:首页,其中用session提取注册用户信息并显示出来。

然后编写record.jsp的代码。主要用到了request.getParameter()方法。在此中,用到了session对象,主要用到了session.setAttribute()方法来保持注册用户的信息,从而在index.jsp中就可以用session对象的session.getAttribute()方法获取用户的信息;主要是根据键/值对来获取;在这个过程中较为复杂的是checkboxrequest对象的使用,因为checkbox可以有多个值。对此,需要用到request.getParameterValues()方法,保存到字符串数组中,最后通过数组遍历来得到每个值;还有就是对于汉字的处理。用到的方法是:<%byte b[]=username.getBytes("ISO-8859-1");

username=new String(b);%>

在显示完用户的信息后,使其自动跳转到index.jsp的页面,用到了response.setHeader();方法;

当用户名或密码为空时,用jsp:forward跳至regError.jsp页面,用到的代码是<%if(username.equals("")||passwd.equals(""))

{%>

<jsp:forward page="regError.jsp">

    <jsp:param value="1" name="incname"/>

    </jsp:forward>

<% }%>

regError.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 user=request.getParameter("incname"); %>

<%if(user.equals("1")) user="用户名或密码为空!";%>

<%=user %>

</body>

</html>

最终用于record.jsp的代码为:

<%@ page language="java" contentType="text/html; charset=GBK"

    pageEncoding="GBK"%>

<!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=GBK">

<title>Insert title here</title>

<style type="text/css">

div{

   margin:0 auto;

   width:500px;

   }

   body{

   font-size:15px;

   font-color:#ffbbcc;

   font-height:160%;

   }

   h4{

   font-color:red;

   }

</style>

</head>

<body>

<div>

<%String username=request.getParameter("username");%>

<%String passwd=request.getParameter("passwd") ;%>

<%if(username.equals("")||passwd.equals(""))

{%>

<jsp:forward page="regError.jsp">

    <jsp:param value="1" name="incname"/>

    </jsp:forward>

<% }%>

<%byte b[]=username.getBytes("ISO-8859-1");

username=new String(b);%>

<%byte a[]=passwd.getBytes("ISO-8859-1");

passwd=new String(a);%>

<p>注册用户的用户名为:

<%=username %>

<% session.setAttribute("user",username);%>

<p>注册用户的密码为:

<%=passwd %>

<p>注册用户的性别为:

<%String sex=request.getParameter("sex");

if(sex.equals("1"))

sex="男";

else if(sex.equals("2"))

sex="女";

%>

<%=sex %>

<% session.setAttribute("sex",sex);%>

<p>注册用户出生年月为:

<%String s1=request.getParameter("year");

String s2=request.getParameter("month");

if(s1.equals("1")) s1="2000";

if(s1.equals("2")) s1="2001";

if(s1.equals("3")) s1="2002";

if(s1.equals("4")) s1="2003";

if(s1.equals("5")) s1="2004";

if(s1.equals("6")) s1="2005";

if(s1.equals("7")) s1="2006";

if(s1.equals("8")) s1="2007";

if(s1.equals("9")) s1="2008";

if(s1.equals("10")) s1="2009";

if(s1.equals("11")) s1="2010";

if(s1.equals("12")) s1="2011";%>

<%=s1 %>年<%=s2 %>月

<% session.setAttribute("year",s1);%>

<% session.setAttribute("month",s2);%>

</p>

<p>您的爱好是:

<%String aihao[]=request.getParameterValues("fav");

if(aihao!=null)

{int size=java.lang.reflect.Array.getLength(aihao);

for(int i=0;i<size;i++)

{

if(aihao[i].equals("1")) aihao[i]="唱歌";

if(aihao[i].equals("2")) aihao[i]="跳舞";

if(aihao[i].equals("3")) aihao[i]="散步";

out.println(aihao[i]+"  ");}

}%>

</p>

<p>注册用户的自我介绍是:

<% String intro=request.getParameter("introduction");

byte d[]=intro.getBytes("ISO-8859-1");

intro=new String(d);

%>

<%=intro %>

</p>

<h4>五秒钟后将跳转至首页!</h4>

<%response.setHeader("Refresh","5;URL=index.jsp"); %>

</div>

</body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值