JSP程序设计第10章习题答案.docx

上传人:b****5 文档编号:12674420 上传时间:2023-04-21 格式:DOCX 页数:18 大小:32.32KB
下载 相关 举报
JSP程序设计第10章习题答案.docx_第1页
第1页 / 共18页
JSP程序设计第10章习题答案.docx_第2页
第2页 / 共18页
JSP程序设计第10章习题答案.docx_第3页
第3页 / 共18页
JSP程序设计第10章习题答案.docx_第4页
第4页 / 共18页
JSP程序设计第10章习题答案.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

JSP程序设计第10章习题答案.docx

《JSP程序设计第10章习题答案.docx》由会员分享,可在线阅读,更多相关《JSP程序设计第10章习题答案.docx(18页珍藏版)》请在冰豆网上搜索。

JSP程序设计第10章习题答案.docx

JSP程序设计第10章习题答案

第10章JSP实用组件技术

习题答案

1.Commons-FileUpload组件是否支持多文件上传?

答案:

Commons-FileUpload组件是由Apache公司开发的功能强大的文件上传组件,我们可以通过该组件实现一次上传一个或多个文件。

2.JavaMail组件支持哪些邮件传输协议?

答案:

JavaMail组件支持三种常用邮件传输协议,包括SMTP、POP和IMAP。

3.简述使用JavaMail组件发送邮件流程。

答案:

JavaMail组件通过javax.mail.Session类定义一个基本邮件会话;

发送邮件时使用javax.mail.Message类储存邮件信息;

通过javax.mail.Transport类指定的邮件传输协议将邮件发送到javax.mail.Address类指定的邮件地址。

4.JFreeChart组件可以生成何种类型的文件?

答案:

JFreeChart组件用于生成统计图表,所生成的图表可以保存为PNG和JPEG格式文件。

5.JFreeChart组件可以生成何类统计图表?

答案:

JFreeChart是JAVA平台上的一个开源图表绘制组件。

通过该组件可生成诸如饼图(piecharts)、柱形图(barcharts)、散点图(scatterplots)、时序图(timeseries)、甘特图(Ganttcharts)等多种图表。

6.实例1

实例内容:

使用Commons-FileUpload组件编写一个多文件上传应用并且对所上传文件的格式进行限制。

答案:

(1)创建名为CommonUpload.jsp的页面文件,该页面中完成三个文件的上传代码如下:

<%@pagelanguage="java"pageEncoding="utf-8"%>

多文件上传

上传文件:


第一个文件:


第二个文件:


第三个文件:



(2)web.xml文件中配置CommonUpload.do请求,代码如下:

xmlversion="1.0"encoding="UTF-8"?

>

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

CommonUpload

sunyang.CommonUpload

CommonUpload

/CommonUpload.do

CommonUpload.jsp

(3)定义Servlet类,处理上传请求,并对上传文件类型进行限制,限制为jpg格式,代码如下:

packagesunyang;

publicclassCommonUploadextendsHttpServlet{

protectedvoiddoGet(HttpServletRequestrequest,

HttpServletResponseresponse)throwsServletException,IOException{

doPost(request,response);

}

protectedvoiddoPost(HttpServletRequestrequest,

HttpServletResponseresponse)throwsServletException,IOException{

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

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("message");

}

request.setAttribute("message",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("message")==null){

request.setAttribute("message","文件上传成功");

}

RequestDispatcherrq=request

.getRequestDispatcher("resultCommonUpload.jsp");

rq.forward(request,response);

}

}

(4)创建resultCommonUpload.jsp页面,该页面显示是否文件上传成功,代码如下:

<%@pagelanguage="java"pageEncoding="utf-8"%>

<%=request.getAttribute("message")%>

7.实例2

实例内容:

使用JFreeChart组件生成一个3D柱形图。

答案:

(1)创建类Bar3D.java,该类用于生成3D柱形图,代码如下:

packagesunyang;

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

for(intn=0;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()>0){

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="";

try{

fileName+=ServletUtilities.saveChartAsPNG(chart,width,height,

info,session);

}catch(IOExceptione){

e.printStackTrace();

}

StringgraphURL=contextPath+servletURI+"?

filename="+fileName;

returngraphURL;

}

}

(2)创建index.jsp页面,该页面用于显示所生成3D柱形图,代码如下:

<%@pagelanguage="java"pageEncoding="gbk"import="sunyang.*"%>

<%Bar3Db3D=newBar3D();%>

3D柱形图

"border="1">

8.实例3

实例内容:

使用JFreeChart组件生成一个3D饼图。

答案:

(1)创建类Pie3D.java,该类用于生成3D饼图,代码如下:

packagesunyang;

publicclassPie3D{

intwidth;

intheight;

StringchartTitle;

Stringsubtitle;

String[]cutline;

Double[]data;

StringservletURI="/DisplayChart";

publicPie3D(){

width=600;

height=400;

chartTitle="每月平均降水量";

subtitle="——统计城市:

长春";

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};

}

publicStringdraw(HttpSessionsession,StringcontextPath){

Fontfont=newFont("SimSun",20,13);

DefaultPieDatasetdataset=newDefaultPieDataset();

for(inti=0;i

dataset.setValue(cutline[i],data[i]);

}

JFreeChartchart=ChartFactory.createPieChart3D(chartTitle,dataset,

false,false,false);

chart.addSubtitle(newTextTitle(subtitle,font));

chart.setBackgroundPaint(newColor(200,200,200));

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));

ChartRenderingInfoinfo=newChartRenderingInfo(

newStandardEntityCollection());

StringfileName="";

try{

fileName=ServletUtilities.saveChartAsPNG(chart,width,height,

info,session);

}catch(IOExceptione){

e.printStackTrace();

}

StringgraphURL=contextPath+servletURI+"?

filename="+fileName;

returngraphURL;

}

}

(2)创建index.jsp页面,该页面用于显示所生成3D饼图,代码如下:

<%@pagelanguage="java"pageEncoding="gbk"import="sunyang.*"%>

<%Pie3Dp3d=newPie3D();%>

饼图

"border="1">

9.实例4

实例内容:

使用JFreeChart组件生成一个与数据库关联的柱形图。

答案:

(1)数据库使用MySQLServer5.0,首先创建数据库和数据表,数据库中字段如下:

(2)在数据表中填写数据,如图所示:

(3)创建数据库连接类DBConnector.java,其中方法getConnection()用于连接数据库,代码如下:

packagesunyang;

publicclassDBConnector{

publicstaticConnectiongetConnection(){

Stringuser="root";

Stringpsw="111";

Stringurl="jdbc:

mysql:

//localhost:

3306/temperature?

user="+user

+"&password="+psw

+"&useUnicode=true&characterEncoding=GBK";

Connectionconn=null;

try{

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

conn=DriverManager.getConnection(url);

returnconn;

}catch(Exceptione){

System.out.println("连接数据库失败");

e.printStackTrace();

}

returnnull;

}

}

(4)创建数据表实体类,代码如下:

packagesunyang;

publicclassChangchunTpt{

privateIntegerid;

privateIntegermonth;

privateIntegertpt;

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicIntegergetMonth(){

returnmonth;

}

publicvoidsetMonth(Integermonth){

this.month=month;

}

publicIntegergetTpt(){

retur

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

当前位置:首页 > 初中教育 > 语文

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

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