JSP程序设计第10章习题答案Word格式文档下载.docx
《JSP程序设计第10章习题答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《JSP程序设计第10章习题答案Word格式文档下载.docx(18页珍藏版)》请在冰点文库上搜索。
CommonUpload.do"
enctype="
multipart/form-data"
method="
post"
>
上传文件:
br>
第一个文件:
inputtype="
file"
name="
Upload"
/>
第二个文件:
第三个文件:
<
submit"
value="
上传"
/form>
/body>
/html>
(2)web.xml文件中配置CommonUpload.do请求,代码如下:
?
xmlversion="
1.0"
encoding="
UTF-8"
web-appversion="
2.4"
xmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
servlet>
servlet-name>
CommonUpload<
/servlet-name>
servlet-class>
sunyang.CommonUpload<
/servlet-class>
/servlet>
servlet-mapping>
url-pattern>
/CommonUpload.do<
/url-pattern>
/servlet-mapping>
welcome-file-list>
welcome-file>
CommonUpload.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
(3)定义Servlet类,处理上传请求,并对上传文件类型进行限制,限制为jpg格式,代码如下:
packagesunyang;
publicclassCommonUploadextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,
DiskFileItemFactoryfactory=newDiskFileItemFactory();
factory.setRepository(newFile(request.getRealPath("
/"
)));
factory.setSizeThreshold(1024*1024*20);
ServletFileUploadupload=newServletFileUpload(factory);
Listitems=null;
try{
items=upload.parseRequest(request);
}catch(FileUploadExceptione){
e.printStackTrace();
}
for(inti=0;
i<
items.size();
i++){
FileItemitem=(FileItem)items.get(i);
if(!
item.isFormField()){
StringfieldName=item.getFieldName();
StringfileName=item.getName();
StringcontentType=item.getContentType();
System.out.println(contentType);
if(!
contentType.equals("
image/pjpeg"
)){//判断文件类型是否符合
Stringmessage="
"
;
if(request.getAttribute("
message"
)!
=null){
message=(String)request.getAttribute("
);
}
request.setAttribute("
message+fileName
+"
文件类型不符导致上传失败<
continue;
}
FileOutputStreamfos=newFileOutputStream(request.getRealPath("
)
+System.currentTimeMillis()
+fileName.substring(fileName.lastIndexOf("
."
),
fileName.length()));
InputStreamis=item.getInputStream();
byte[]buffer=newbyte[1024];
intlen;
while((len=is.read(buffer))>
0){
fos.write(buffer,0,len);
is.close();
fos.close();
}
if(request.getAttribute("
)==null){
request.setAttribute("
"
文件上传成功"
RequestDispatcherrq=request
.getRequestDispatcher("
resultCommonUpload.jsp"
rq.forward(request,response);
}
(4)创建resultCommonUpload.jsp页面,该页面显示是否文件上传成功,代码如下:
%=request.getAttribute("
)%>
7.实例2
使用JFreeChart组件生成一个3D柱形图。
(1)创建类Bar3D.java,该类用于生成3D柱形图,代码如下:
publicclassBar3D{
intwidth;
intheight;
StringchartTitle;
Stringsubtitle;
StringxTitle;
StringyTitle;
Stringlegend[];
Stringcategory[];
Integer[][]data;
StringservletURI="
/DisplayChart"
publicBar3D(){
width=600;
height=400;
chartTitle="
每月平均温度"
subtitle="
——统计时间:
2008年"
xTitle="
月份"
yTitle="
气温单位:
摄氏度"
legend=newString[]{"
吉林长春"
湖南长沙"
};
category=newString[]{"
1月"
2月"
3月"
4月"
5月"
6月"
7月"
"
8月"
9月"
10月"
11月"
12月"
data=newInteger[][]{
{-21,-3,12,19,22,28,30,29,23,18,5,-10},
{3,12,17,20,25,32,41,38,30,27,15,10}};
publicStringdraw(HttpSessionsession,StringcontextPath){
DefaultCategoryDatasetdataset=newDefaultCategoryDataset();
for(intm=0;
m<
legend.length;
m++){
for(intn=0;
n<
category.length;
n++)
dataset.addValue(data[m][n],legend[m],category[n]);
Fontfont=newFont("
SimSun"
10,15);
JFreeChartchart=ChartFactory.createBarChart3D(chartTitle,xTitle,
yTitle,dataset,PlotOrientation.VERTICAL,true,true,false);
//生成3D柱形图
chart.setTitle(newTextTitle(chartTitle,font));
if(subtitle.length()>
chart.addSubtitle(newTextTitle(subtitle));
chart.setBackgroundPaint(newColor(200,200,200));
CategoryPlotplot=chart.getCategoryPlot();
plot.getRangeAxis().setLabelFont(font);
plot.getDomainAxis().setLabelFont(font);
plot.getRangeAxis().setTickLabelFont(font);
plot.getDomainAxis().setTickLabelFont(font);
plot.setBackgroundPaint(newColor(241,219,127));
plot.setRangeGridlinePaint(Color.BLACK);
plot.setRangeGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.RED);
plot.setDomainGridlinesVisible(true);
BarRendererrenderer=(BarRenderer)plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setBaseLegendTextFont(font);
ChartRenderingInfoinfo=newChartRenderingInfo(
newStandardEntityCollection());
StringfileName="
fileName+=ServletUtilities.saveChartAsPNG(chart,width,height,
info,session);
}catch(IOExceptione){
StringgraphURL=contextPath+servletURI+"
filename="
+fileName;
returngraphURL;
(2)创建index.jsp页面,该页面用于显示所生成3D柱形图,代码如下:
gbk"
import="
sunyang.*"
%Bar3Db3D=newBar3D();
3D柱形图<
imgsrc="
%=b3D.draw(session,request.getContextPath())%>
border="
1"
8.实例3
使用JFreeChart组件生成一个3D饼图。
(1)创建类Pie3D.java,该类用于生成3D饼图,代码如下:
publicclassPie3D{
String[]cutline;
Double[]data;
publicPie3D(){
每月平均降水量"
——统计城市:
长春"
cutline=newString[]{"
Jan"
Feb"
Mar"
Apr"
May"
Jun"
Jul"
Aug"
Sep"
Oct"
Nov"
Dec"
data=newDouble[]{3.5,4.6,9.1,21.9,42.3,90.7,183.5,127.5,
61.4,33.5,11.5,4.4};
20,13);
DefaultPieDatasetdataset=newDefaultPieDataset();
cutline.length;
dataset.setValue(cutline[i],data[i]);
JFreeChartchart=ChartFactory.createPieChart3D(chartTitle,dataset,
false,false,false);
chart.addSubtitle(newTextTitle(subtitle,font));
TextTitletitle=chart.getTitle();
title.setFont(font);
title.setPaint(Color.RED);
PiePlotplot=(PiePlot)chart.getPlot();
plot.setBackgroundPaint(newColor(255,255,0));
plot.setLabelFont(font);
plot.setLabelBackgroundPaint(Color.YELLOW);
plot.setBaseSectionOutlinePaint(newColor(0,0,0));
plot.setBaseSectionOutlineStroke(newBasicStroke(1.0f));
fileName=ServletUtilities.saveChartAsPNG(chart,width,height,
(2)创建index.jsp页面,该页面用于显示所生成3D饼图,代码如下:
%Pie3Dp3d=newPie3D();
饼图<
%=p3d.draw(session,request.getContextPath())%>
9.实例4
使用JFreeChart组件生成一个与数据库关联的柱形图。
(1)数据库使用MySQLServer5.0,首先创建数据库和数据表,数据库中字段如下:
(2)在数据表中填写数据,如图所示:
(3)创建数据库连接类DBConnector.java,其中方法getConnection()用于连接数据库,代码如下:
publicclassDBConnector{
publicstaticConnectiongetConnection(){
Stringuser="
root"
Stringpsw="
111"
Stringurl="
jdbc:
mysql:
//localhost:
3306/temperature?
user="
+user
+"
&
password="
+psw
useUnicode=true&
characterEncoding=GBK"
Connectionconn=null;
Class.forName("
org.gjt.mm.mysql.Driver"
).newInstance();
conn=DriverManager.getConnection(url);
returnconn;
}catch(Exceptione){
System.out.println("
连接数据库失败"
returnnull;
(4)创建数据表实体类,代码如下:
publicclassChangchunTpt{
privateIntegerid;
privateIntegermonth;
privateIntegertpt;
publicIntegergetId(){
returnid;
publicvoidsetId(Integerid){
this.id=id;
publicIntegergetMonth(){
returnmonth;
publicvoidsetMonth(Integermonth){
this.month=month;
publicIntegergetTpt(){
retur