JSP程序设计第10章习题答案Word格式文档下载.docx

上传人:b****2 文档编号:1357704 上传时间:2023-04-30 格式:DOCX 页数:18 大小:32.32KB
下载 相关 举报
JSP程序设计第10章习题答案Word格式文档下载.docx_第1页
第1页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第2页
第2页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第3页
第3页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第4页
第4页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第5页
第5页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第6页
第6页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第7页
第7页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第8页
第8页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第9页
第9页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第10页
第10页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第11页
第11页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第12页
第12页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第13页
第13页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第14页
第14页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第15页
第15页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第16页
第16页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第17页
第17页 / 共18页
JSP程序设计第10章习题答案Word格式文档下载.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JSP程序设计第10章习题答案Word格式文档下载.docx

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

JSP程序设计第10章习题答案Word格式文档下载.docx

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

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

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2