Mvc的应用实例Word文档格式.docx
《Mvc的应用实例Word文档格式.docx》由会员分享,可在线阅读,更多相关《Mvc的应用实例Word文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
xmlversion="
1.0"
encoding="
ISO-8859-1"
>
web-app>
servlet>
<
servlet-name>
computerSum<
/servlet-name>
servlet-class>
sun.yourservlet.HandleSum<
/servlet-class>
/servlet>
servlet-mapping>
url-pattern>
/lookSum<
/url-pattern>
/servlet-mapping>
/web-app>
WEB.XML要保存到WEB-INF目录下。
一、模型
Series.java
packagemoon.yourbean;
publicclassSeries
{doublefirstItem;
//数列首项
doublevar;
//公差或公比
intnumber;
//求和项数
doublesum;
//求和结果
Stringname="
"
;
//数列类别
publicvoidsetFirstItem(doublea)
{firstItem=a;
}
publicdoublegetFirstItem()
{returnfirstItem;
publicvoidsetVar(doubleb)
{var=b;
publicdoublegetVar()
{returnvar;
publicvoidsetNumber(intn)
{number=n;
publicdoublegetNumber()
{returnnumber;
publicvoidsetSum(doubles)
{sum=s;
publicdoublegetSum()
{returnsum;
publicvoidsetName(Stringna)
{name=na;
publicStringgetName()
{returnname;
}
二、视图
inputMess.jsp
%@pagecontentType="
text/html;
Charset=GB2312"
%>
HTML>
BODY>
Fontsize=2>
FORMaction="
lookSum"
Method="
post"
>
等差数列求和:
BR>
输入首项:
Inputtype=textname="
firstItem"
size=4>
输入公差:
var"
求和项数:
number"
Inputtype=submitvalue="
提交"
/FORM>
get"
等比数列求和:
输入公比:
/Font>
/BODY>
/HTML>
showResult.jsp
charset=GB2312"
%@pageimport="
moon.yourbean.*"
%>
jsp:
useBeanid="
lie"
type="
moon.yourbean.Series"
scope="
request"
/>
BODY>
Tableborder=1>
tr>
th>
数列的首项<
/th>
getPropertyname="
property="
name"
所求项数<
求和结果<
/tr>
td>
/td>
sum"
/Table>
三、控制器
HandleSum.java
packagesun.yourservlet;
importmoon.yourbean.*;
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassHandleSumextendsHttpServlet
{publicvoidinit(ServletConfigconfig)throwsServletException
{super.init(config);
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException
{Seriesshulie=newSeries();
//创建Javabean对象
request.setAttribute("
shulie);
//将shulie存储到HttpServletRequest对象中
doublea=Double.parseDouble(request.getParameter("
));
doubled=Double.parseDouble(request.getParameter("
intn=Integer.parseInt(request.getParameter("
shulie.setFirstItem(a);
//将数据存储在shulie中
shulie.setVar(d);
shulie.setNumber(n);
//计算和:
doublesum=0,item=a;
inti=1;
shulie.setName("
等差数列的公差"
);
while(i<
=n)//计算等差数列的和
{sum=sum+item;
i++;
item=item+d;
shulie.setSum(sum);
RequestDispatcherdispatcher=request.getRequestDispatcher("
showResult.jsp"
dispatcher.forward(request,response);
//请求showResult.jsp显示shulie中的数据
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
等比数列的公比"
=n)//计算等比数列的和
item=item*d;
实例二、数据库查询操作
ShowRecordByPage.java
importcom.sun.rowset.*;
publicclassShowRecordByPage
{CachedRowSetImplrowSet=null;
//存储表中全部记录的行集对象
intpageSize=10;
//每页显示的记录数
intpageAllCount=0;
//分页后的总页数
intshowPage=1;
//当前显示页
StringBufferpresentPageResult;
//显示当前页内容
StringdatabaseName="
//数据库名称
StringtableName="
//表的名字
StringBufferformTitle=null;
//表头
publicvoidsetRowSet(CachedRowSetImplset)
{rowSet=set;
publicCachedRowSetImplgetRowSet()
{returnrowSet;
publicvoidsetPageSize(intsize)
{pageSize=size;
publicintgetPageSize()
{returnpageSize;
publicintgetPageAllCount()
{returnpageAllCount;
publicvoidsetPageAllCount(intn)
{pageAllCount=n;
publicvoidsetShowPage(intn)
{showPage=n;
publicintgetShowPage()
{returnshowPage;
publicvoidsetPresentPageResult(StringBufferp)
{presentPageResult=p;
publicStringBuffergetPresentPageResult()
{returnpresentPageResult;
publicvoidsetDatabaseName(Strings)
{databaseName=s.trim();
publicStringgetDatabaseName()
{returndatabaseName;
publicvoidsetTableName(Strings)
{tableName=s.trim();
publicStringgetTableName()
{returntableName;
publicvoidsetFormTitle(StringBuffers)
{formTitle=s;
publicStringBuffergetFormTitle()
{returnformTitle;
choiceDatabase.jsp
helpReadRecord"
method="
name="
form"
输入SQlServer2000所管理的数据库的名字(例如:
pubs、factory):
INPUTtype="
text"
databaseName"
输入相应数据库中表的名字(例如:
jobs、employee):
tableName"
输入每页显示的记录数:
value="
2"
pageSize"
size=6>
submit"
showRecord.jsp
database"
moon.yourbean.ShowRecordByPage"
session"
您查询的数据库:
查询的表:
。
记录分<
pageAllCount"
页,
每页最多显示<
条记录,
目前显示第<
showPage"
页。
tableborder=1>
formTitle"
presentPageResult"
/table>
table>
hidden"
previousPage"
whichPage"
上一页"
nextPage"
下一页"
HandleDatabase.java
importjava.sql.*;
publicclassHandleDatabaseextendsHttpServlet
{int字段个数;
CachedRowSetImplrowSet=null;
publicvoidinit(ServletConfigconfig)throwsServletException
{super.init(config);
try{Class.forName("
com.microsoft.sqlserver.jdbc.SQLServerDriver"
catch(Exceptione){}
{Connectioncon;
StringBufferpresentPageResult=newStringBuffer();
ShowRecordByPagedatabaseBean=null;
HttpSessionsession=request.getSession(true);
try{databaseBean=(ShowRecordByPage)session.getAttribute("
if(databaseBean==null)
{databaseBean=newShowRecordByPage();
session.setAttribute("
databaseBean);
catch(Exceptionexp)
StringdatabaseName=request.getParameter("
StringtableName=request.getParameter("
Stringps=request.getParameter("
if(ps!
=null)
{try{intmm=Integer.parseInt(ps);
databaseBean.setPageSize(mm);
catch(NumberFormatExceptionexp)
{databaseBean.setPageSize
(1);
intshowPage=databaseBean.getShowPage();
intpageSize=databaseBean.getPageSize();
booleanboo=databaseName!
=null&
&
tableName!
databaseName.length()>
0&
tableName.length()>
0;
if(boo)
{databaseBean.setDatabaseName(databaseNam