**JSP程序设计程第二版
JSP内置对象实例实训报告
JSP内置对象实例实训
设备仪器/软件环境**
1.Windows 10系统
2.JDK、eclipse、Tomcat
实验(实训)/目标
1.掌握JSP中request对象、reponse对象、session对象及application对象的使用方法。
2.理解request对象、reponse对象、session对象及application对象的作用及功能。
实验(实训)
过程/步骤
(1)使用request内置对象完成用户注册页面的用户信息的获取和显示。
a.建立 index.jsp页面,显示用户注册表单信息。
<form action="dealwith.jsp" method="post" name="form1">
<table>
<tr>
<td height="50">账 号:</td>
<td height="50"><input type="text" name="account"></td>
</tr>
<tr>
<td height="50"> 码:</td> <td height="50"><input type="password" name="password"></td>
</tr>
<tr>
<td height="50">真实姓名: </td>
<td height="50"><input type="text" name="realname"></td>
</tr>
<tr>
<td height="50">Email地址: </td>
<td height="50"><input type="text" name="email"></td>
</tr>
<tr>
<td height="50">性 别:</td>
<td height="50"><input type="radio" name="sex" value="男" checked="checked">男
<input type="radio" name="sex" value="女" checked="checked">女
</td>
</tr>
<tr>
<td width="110" height="50"> </td>
<td width="152"><input type="submit" name="Submit" value="注册" >
</td>
</tr>
</table>
</form>
b.在用户注册页面,当用户输入完注册信息后,单击注册按钮,可以将表彰信息提交到dealwith.jsp页面。在此页面中,通过request对象的getParameter()方法将表单中的对象获取,之后将获取的对象保存在request范围内,并通jsp:forward元素转向showUserInfo.jsp页面。
<%String account=new String (request.getParameter("account").getBytes("ISO8859_1"),"UTF-8");
String password=new String (request.getParameter("password").getBytes("ISO8859_1"),"UTF-8");
String realname=new String (request.getParameter("realname").getBytes("ISO8859_1"),"UTF-8");
String email=new String (request.getParameter("email").getBytes("ISO8859_1"),"UTF-8");
String sex=new String (request.getParameter("sex").getBytes("ISO8859_1"),"UTF-8");
request.setAttribute("account",account);
request.setAttribute("password",password);
request.setAttribute("realname",realname);
request.setAttribute("email",email);
request.setAttribute("sex",sex); %>
<jsp:forward page="showUserInfo.jsp" />
c .在showUserInfo.jsp页面,通过request对象中的getAttibute方法将request范围内中用户注册信息进行获取并显示。
<table width="244" border="0" align="center">
<tr>
<td width="110" height="50">账 号: </td>
<td width="151" height="50"><%=request.getAttribute("account")%>
</td>
</tr>
<tr>
<td height="30">真实姓名: </td>
<td height="50"><%=request.getAttribute("realname")%>
</td>
</tr>
<tr>
<td height="30">Email地址: </td>
<td height="50"><%=request.getAttribute("email")%>
</td>
</tr>
<tr>
<td height="30">性 别: </td>
<td height="50"><%=request.getAttribute("sex")%>
</td>
</tr>
</table>
(2)session对象实现局部网页计数器。关闭浏览器,重新打开浏览器,再次运行本程序,结果是?
<%
int number=0;
if(null!=session.getAttribute("number")){
number=(Integer)session.getAttribute("number");
}
number++;
out.println("当前网页访问次数:"+number);
session.setAttribute("number",number);
%>
(3)使用application对象实现全局网页计数器。即使将页面关闭重新打开,或者从不同客户端浏览器打开该网页,计数器结果如何?
<%
int number=0;
if(null!=application.getAttribute("number")){
number=(Integer)application.getAttribute("number");
}
number++;
out.println("当前网页访问次数:"+number);
application.setAttribute("number",number);
%>
一、运行截图
(1)使用request内置对象完成用户注册页面的用户信息的获取和显示。
(2)使用session对象实现局部网页计数器。关闭浏览器,重新打开浏览器,再次运行本程序,结果是?
(3)使用application对象实现全局网页计数器。即使将页面关闭重新打开,或者从不同客户端浏览器打开该网页,计数器结果如何?
二、分析总结
request对象属于 Javax. ervlet ServletRequest接口的实例化对象。
【作用】 request对象不但可以用来设置和取得 request范围变量,还可以用来获得客户端请求参数、请求的来源、表头、 cookies等。
【机制】当用户请求一个JSP页面时,JSP页面所在的 Tomcat服务器将用户的请求封装在内置对象 request中。 request内置对象代表了客户端的请求信息,主要用于接收客户端通过HTTP协议传送给服务器端的数据。在客户端的请求中如果有参数,则该对象就有参数列表。