《Web软件技术》课程实验二Word文档格式.docx
- 文档编号:14785651
- 上传时间:2022-10-24
- 格式:DOCX
- 页数:15
- 大小:111.95KB
《Web软件技术》课程实验二Word文档格式.docx
《《Web软件技术》课程实验二Word文档格式.docx》由会员分享,可在线阅读,更多相关《《Web软件技术》课程实验二Word文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
(一)通过查询串传递请求参数,并在Servlet中检索请求参数
【步骤1】编写下面的Servlet。
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassHelloUserServletextendsHttpServlet{
publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
StringuserName=request.getParameter("
userName"
);
PrintWriterpw=response.getWriter();
pw.println("
<
html>
"
head>
/head>
body>
h3>
Hello,"
+userName+"
/h3>
/body>
/html>
}
【步骤2】使用下面的URL访问之。
http:
//localhost:
8080/helloapp/hellouser?
userName=John
将userName的值改为你自己的名字,看输出结果。
若不给出查询字符串,结果如何?
参数值使用汉字如何?
(二)通过表单传递请求参数,使用HttpServletRequest对象获得请求参数
【步骤1】首先编写一个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,HttpServletResponseresponse)
throwsServletException,IOException{
response.setCharacterEncoding("
gb2312"
response.setContentType("
Stringname=request.getParameter("
name=newString(name.getBytes("
iso8859-1"
),"
Stringage=request.getParameter("
age=newString(age.getBytes("
PrintWriterpw=response.getWriter();
pw.println("
姓名:
+name+"
p>
年龄:
+age+"
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
doGet(request,response);
【步骤3】测试参数传递。
在浏览器的地址栏中输入:
8080/helloapp/input.html。
输入数据,点击“确定”按钮,体会表单参数的传递。
问题1:
如何通过超链接的锚标签(<
ahref>
)传递请求参数?
(三)编写一个程序实现文件上传功能。
假设学生上传作业,需要执行学号和上传的文件。
as11.java:
importjavax.servlet.annotation.*;
@WebServlet(name="
as11"
urlPatterns={"
/fileUpload.do"
})
@MultipartConfig(location="
D:
\\"
fileSizeThreshold=1024)
publicclassas11extendsHttpServlet{
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
//返回Web应用程序文档根目录
Stringpath=this.getServletContext().getRealPath("
/"
Stringmnumber=request.getParameter("
mnumber"
Partp=request.getPart("
fileName"
Stringmessage="
;
if(p.getSize()>
1024*1024){//上传的文件不能超过1MB大小
p.delete();
message="
文件太大,不能上传!
}else{
path=path+"
\\member\\"
+mnumber;
Filef=newFile(path);
if(!
f.exists()){//若目录不存在,则创建目录
f.mkdirs();
Stringh=p.getHeader("
content-disposition"
//得到文件名
Stringfname=h.substring(h.lastIndexOf("
)+1,h.length()-1);
p.write(path+"
+fname);
文件上传成功!
request.setAttribute("
message"
message);
RequestDispatcherrd=request.getRequestDispatcher("
/fileUpload.jsp"
rd.forward(request,response);
fileUpload.jsp:
%@pagecontentType="
charset=UTF-8"
pageEncoding="
UTF-8"
%>
上传文件<
${message}<
formaction="
enctype="
multipart/form-data"
method="
post"
table>
tr>
tdcolspan="
2"
align="
center"
文件上传<
/td>
/tr>
td>
会员号:
size="
30"
/>
文件名:
file"
tdalign="
right"
提交"
left"
/>
/table>
xml文件部分代码:
(四)简单的综合应用<
?
xmlversion="
1.0"
encoding="
web-appxmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xmlns="
xsi:
schemaLocation="
id="
WebApp_ID"
version="
2.5
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Web软件技术 Web 软件技术 课程 实验
![提示](https://static.bdocx.com/images/bang_tan.gif)