ServletsJsp 实验2HTTP请求对象的使用Word格式文档下载.docx
- 文档编号:19198663
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:20
- 大小:202.03KB
ServletsJsp 实验2HTTP请求对象的使用Word格式文档下载.docx
《ServletsJsp 实验2HTTP请求对象的使用Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《ServletsJsp 实验2HTTP请求对象的使用Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。
/head>
body>
h3>
Hello,"
+userName+"
/h3>
/body>
/html>
}
步骤2:
编译该Servlet,然后修改web.xml文件,将下面代码添加到其中:
servlet>
<
servlet-name>
helloUserServlet<
/servlet-name>
servlet-class>
com.demo.HelloUserServlet<
/servlet-class>
/servlet>
servlet-mapping>
helloUser<
url-pattern>
/url-pattern>
/servlet-mapping>
步骤3:
使用下面的URL访问之。
http:
//localhost:
8080/helloapp/hellouser.do?
userName=John
将userName的值改为你自己的名字,看输出结果。
若不给出查询字符串,结果如何?
参数值使用汉字如何?
2.通过表单传递请求参数,使用HttpServletRequest对象获得请求参数
首先编写一个input.html文件,存放在helloapp目录中。
该页面在屏幕上显示一个表单,其中包括姓名和年龄域,点击“确定”按钮后,将表单参数传递给ParamServlet并显示出来。
input.html代码如下:
title>
参数传递<
/title>
metahttp-equiv="
Content-Type"
content="
text/html;
charset=gb2312"
>
center>
从表单向Servlet传递参数<
formmethod="
GET"
action="
paramtest"
>
姓名:
inputtype="
text"
name="
name"
br>
年龄:
age"
submit"
value="
确定"
reset"
重置"
/form>
【步骤2】编写ParamServlet.java,其代码如下:
importjava.util.*;
publicclassParamServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,
throwsServletException,IOException{
response.setCharacterEncoding("
gb2312"
response.setContentType("
Stringname=request.getParameter("
name=newString(name.getBytes("
iso8859-1"
),"
Stringage=request.getParameter("
age=newString(age.getBytes("
PrintWriterout=response.getWriter();
out.println("
姓名:
+name+"
p>
年龄:
+age+"
}
publicvoiddoPost(HttpServletRequestrequest,
throwsServletException,IOException{
doGet(request,response);
【步骤3】修改web.xml文件,将下面代码添加到其中:
ParamServlet<
com.demo.ParamServlet<
/paramtest<
【步骤4】在浏览器的地址栏中输入:
8080/helloapp/input.html。
输入数据,点击“确定”按钮,体会表单参数的传递。
问题1:
如何通过超链接的锚标签(<
ahref>
)传递请求参数?
3.编写一个Servlet,显示所有请求头
【步骤1】ShowHeadersServlet.java的Servlet能够检索所有的请求头,代码如下:
publicclassShowHeadersServletextendsHttpServlet{
response.setContentType("
PrintWriterout=response.getWriter();
out.println("
AllHeaders<
bodybgcolor=\"
#fdf5e6\"
h1align=\"
center\"
AllRequestHeaders<
/h1>
tableborder=1align=\"
\n"
+
"
trbgcolor=\"
#ffad00\"
th>
HeaderName<
HeaderValue"
EnumerationheaderNames=request.getHeaderNames();
while(headerNames.hasMoreElements()){
StringheaderName=(String)headerNames.nextElement();
tr>
td>
+headerName);
+request.getHeader(headerName));
/table>
\n<
doGet(request,response);
【步骤2】修改web.xml,加入下面内容:
ShowHeadersServlet<
com.dom.ShowHeadersServlet<
<
ShowHeadersServlet<
/showheaders<
【步骤3】在浏览器地址栏中输入下面URL访问该Servlet
8080/helloapp/showheaders
4.简单的综合应用
【步骤1】创建一个名为input.html的HTML页面,其中包括一个表单,表单中包含两个文本域,分别供用户输入学号和姓名,该页面也包含提交和取消按钮。
图2.1input.html页面显示结果
【步骤2】定义一个名为com.demo.Student类,其中包括学号sno和姓名name两个private的成员变量,定义访问和修改sno和name的方法。
【步骤3】编写名为FirstServlet的Servlet,要求当用户在input.html中输入信息后点击“提交”按钮,请求FirstServlet对其处理。
在FirstServlet中使用表单传递的参数(学号和姓名)创建一个Student对象并将其作为属性存储在请求对象中,然后通过请求对象的getRequestDispatcher()方法获得RequestDispatcher()对象,将请求转发到SecondServlet。
【步骤4】在SecondServlet中取出请求对象上存储的Student对象,并显示输出该学生的学号和姓名。
【步骤5】在SecondServlet的输出中应该包含一个链接,点击连接返回到input.html页面。
图2.2SecondServlet显示结果
input.html的代码如下:
LoginPage<
formaction="
firstservlet"
method="
post"
tablewidth="
298"
border="
0"
align="
center"
cellpading="
2"
cellspacing="
1"
tdalign="
right"
学号:
/td>
left"
snumber"
size="
15"
/tr>
sname"
tdcolspan="
登录"
&
nbsp;
取消"
//FirstServlet.java的代码如下:
importcom.model.Student;
publicclassFirstServletextendsHttpServlet{
ServletContextcontext;
publicvoidinit(){
context=getServletContext();
request.setCharacterEncoding("
Stringsnumber=request.getParameter("
Stringsname=request.getParameter("
Studentstud=newStudent(snumber,sname);
request.setAttribute("
stud"
stud);
RequestDispatcherview=
request.getRequestDispatcher("
/secondservlet"
view.forward(request,response);
//SecondServlet.java的代码如下:
publicclassSecondServletextendsHttpServlet{
Studentstud=(Student)request.getAttribute("
+stud.getSnumber()+"
+stud.getSname()+"
ahref=\"
/helloapp/input.html\"
返回输入页面<
/a>
/center>
web.xml文件对两个Servlet的配置代码如下:
firstServlet<
/firstservlet<
secondServlet<
com.demo.SecondServlet<
/secondservlet<
实验代码:
publicclassStudent{
privateStringsno;
privateStringname;
publicStudent(Stringsno,Stringname){
super();
this.sno=sno;
this.name=name;
publicStudent(){
publicStringgetSno(){
returnsno;
publicvoidsetSno(Stringsno){
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
Web.xml:
?
xmlversion="
1.0"
encoding="
UTF-8"
web-appversion="
2.5"
xmlns="
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
display-name>
/display-name>
com.demo.ShowHeadersServlet<
FirstServlet<
com.demo.FirstServlet<
SecondServlet<
/showheaders.do<
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
三.思考题
1.在Servlet中如何获得请求参数?
答:
web组件之间参数传递有3种方式:
(1)request.getParameter("
a、通过超链接
b、通过表单
c、通过servlet转发或重定
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Servlets Jsp 实验2HTTP请求对象的使用 实验 HTTP 请求 对象 使用
![提示](https://static.bdocx.com/images/bang_tan.gif)