Web软件技术课程实验二.docx

上传人:b****6 文档编号:6937205 上传时间:2023-01-12 格式:DOCX 页数:8 大小:17.29KB
下载 相关 举报
Web软件技术课程实验二.docx_第1页
第1页 / 共8页
Web软件技术课程实验二.docx_第2页
第2页 / 共8页
Web软件技术课程实验二.docx_第3页
第3页 / 共8页
Web软件技术课程实验二.docx_第4页
第4页 / 共8页
Web软件技术课程实验二.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

Web软件技术课程实验二.docx

《Web软件技术课程实验二.docx》由会员分享,可在线阅读,更多相关《Web软件技术课程实验二.docx(8页珍藏版)》请在冰豆网上搜索。

Web软件技术课程实验二.docx

Web软件技术课程实验二

公司内部编号:

(GOOD-TMMT-MMUT-UUPTY-UUYY-DTTI-9018)

 

Web软件技术课程实验二

《Web软件技术》课程

实验报告

题目HTTP请求对及数据处理

专业信息工程

班级信工143

学号

姓名王钊健

指导教师签名范贵生

华东理工大学信息学院计算机系

2017年03月03日

实验二HTTP请求对及数据处理

姓名王钊健学号专业班级信工143成绩_________

实验日期2017/3/2实验地点信息大楼215指导教师(签名)

一.实验目的

1.掌握如何获得请求参数;

2.掌握如何检索请求头;

3.了解如何通过请求对象获得客户信息;

4.重点掌握使用请求对象存储数据和请求转发。

二.实验工具与设备

三.实验内容

(一)通过查询串传递请求参数,并在Servlet中检索请求参数

【步骤1】编写下面的Servlet。

import.*;

import.*;

publicclassHelloUserServletextendsHttpServlet{

publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

StringuserName=("userName");

PrintWriterpw=();

("");

("");

("");

("");

("

Hello,"+userName+"

");

("");

("");

}

}

【步骤2】使用下面的URL访问之。

:

8080/helloapp/hellouseruserName=John

将userName的值改为你自己的名字,看输出结果。

若不给出查询字符串,结果如何参数值使用汉字如何

(二)通过表单传递请求参数,使用HttpServletRequest对象获得请求参数

【步骤1】首先编写一个文件,存放在helloapp目录中。

该页面在屏幕上显示一个表单,其中包括姓名和年龄域,点击“确定”按钮后,将表单参数传递给ParamServlet并显示出来。

代码如下:

参数传递

从表单向Servlet传递参数

姓名:


年龄:


【步骤2】编写,其代码如下:

import.*;

import.*;

import.*;

publicclassParamServletextendsHttpServlet{

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

("gb2312");

("text/html;charset=gb2312");

Stringname=("name");

name=newString("iso8859-1"),"gb2312");

Stringage=("age");

age=newString("iso8859-1"),"gb2312");

PrintWriterpw=();

("");

("");

("");

("");

("

姓名:

"+name+"

");

("

年龄:

"+age+"

");

("");

("");

}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}

}

【步骤3】helloapp/。

输入数据,点击“确定”按钮,体会表单参数的传递。

问题1:

如何通过超链接的锚标签()传递请求参数

(三)编写一个程序实现文件上传功能。

假设学生上传作业,需要执行学号和上传的文件。

:

import.*;

import.*;

import

import

@WebServlet(name="as11",urlPatterns={"/"})

@MultipartConfig(location="D:

\\",fileSizeThreshold=1024)

publicclassas11extendsHttpServlet{

publicvoiddoPost(HttpServletRequestrequest,

HttpServletResponseresponse)

throwsServletException,IOException{

etRealPath("/");

Stringmnumber=("mnumber");

Partp=("fileName");

Stringmessage="";

if()>1024*1024){;

import.*;

import

import

import

@WebServlet(name="FirstServlet",urlPatterns="/first")

publicclassFirstServletextendsHttpServlet{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

ServletContextcontext;

publicvoidinit(){

context=getServletContext();

}

publicvoiddoPost(HttpServletRequestrequest,

HttpServletResponseresponse)

throwsServletException,IOException{

("gb2312");

Stringsnumber=("snumber");

Stringsname=("sname");

Studentstud=newStudent(snumber,sname);

("stud",stud);

RequestDispatcherview=

("/SecondServlet");

(request,response);

}

}

:

package;

import.*;

import.*;

import

import

publicclassSecondServletextendsHttpServlet{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

ServletContextcontext;

publicvoidinit(){

context=getServletContext();

}

publicvoiddoPost(HttpServletRequestrequest,

HttpServletResponseresponse)

throwsServletException,IOException{

Studentstud=(Student)("stud");

("text/html;charset=gb2312");

PrintWriterout=();

("");

("

");

("学号:

"+()+"
");

("姓名:

"+()+"
");

("返回输入页面");

("

");

("");

}

}

:

xsi=xmlns=xsi:

schemaLocation=id="WebApp_ID"version="">

ss

FirstServlet

FirstServlet

/FirstServlet

SecondServlet

SecondServlet

/SecondServlet

四.实验体会(包括收获、心得体会、存在的问题及解决问题的方法、建议等)

通过本次实验,对HTTP请求对及数据处理有了一定的了解和掌握,对javaweb编程更加熟悉。

比较容易出现404的问题,通过学习与仔细观察,处理问题的能力得到了一定的增强。

五.思考题

1.在Servlet中如何获得请求参数

答:

web组件之间参数传递有3种方式:

(1)("name");

a、通过超链接

b、通过表单

c、通过servlet转发或重定向

d、通过隐藏域完成

(2)("key");如何实现请求转发如何在请求对象中共享数据

jsp:

("/*.jsp").forward(request,response);

servlet:

sendRedirect()方法原理:

1,客户端发送请求,Servlet1做出处理。

2,Servlet1调用sendReadirect()方法,将客户端的请求重新定位到Servlet2。

3,客户端访问Servlet2.?

4,Servlet2对客户端浏览器做出响应。

forward()方法原理:

1,客户端发送请求,Servlet1做出处理。

2,Servlet调用forward()方法,将请求转发给Servlet2来处理请求,为客户端服务.

3,Servlet2对客户端浏览器做出响应。

<:

forward/>或

include/>

("").forward();

("").include();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 计算机硬件及网络

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1