jsp/servlet/javaBean三层架构小示例(1)

本文介绍了一个简单的用户登录功能实现案例,使用MVC模式通过JSP、Servlet和JavaBean完成前后端分离。展示了如何利用数据库操作类进行用户验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java代码中写文档(html),这是servlet;在文档中写java代码,这是jsp;而如何将两者分开,这就是MVC模式了。虽然是java的一小步,却是程序员们生活的一大步哦。
下面给出一个简单的用户登陆功能,采用jsp/servelt/javaBean来实现MVC模式(即经典的模式2)。

模型层:
None.gifpackagecom.vitamin.DataAccess;
None.gif
None.gif
None.gif
importjava.sql.*;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
publicclassDBbasedot.gif{
InBlock.gifStringsDBDriver
="sun.jdbc.odbc.JdbcOdbcDriver";
InBlock.gifStringsConnstr
="jdbc:odbc:myDB";
InBlock.gifConnectionconnect
=null;
InBlock.gifResultSetrs
=null;
InBlock.gifStatementstmt
=null;
InBlock.gif
InBlock.gif
InBlock.gif
publicDBbase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifClass.forName(sDBDriver);
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(ClassNotFoundExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
publicResultSetexecuteQuery(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifrs
=stmt.executeQuery(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrs;
ExpandedSubBlockEnd.gif}

InBlock.gif
publicintexecuteUpdate(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intresult=0;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifresult
=stmt.executeUpdate(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

控制层:
None.gifpackagecom.vitamin.servlet;
None.gif
None.gif
importjava.io.IOException;
None.gif
importjava.io.PrintWriter;
None.gif
None.gif
importjavax.servlet.ServletException;
None.gif
importjavax.servlet.http.HttpServlet;
None.gif
importjavax.servlet.http.HttpServletRequest;
None.gif
importjavax.servlet.http.HttpServletResponse;
None.gif
importcom.vitamin.DataAccess.*;
None.gif
importjava.sql.*;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
publicclassHelloextendsHttpServletdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/***//**
InBlock.gif*Constructoroftheobject.
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicHello()dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/***//**
InBlock.gif*Destructionoftheservlet.<br>
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoiddestroy()dot.gif{
InBlock.gif
super.destroy();//Justputs"destroy"stringinlog
InBlock.gif
//Putyourcodehere
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/***//**
InBlock.gif*ThedoGetmethodoftheservlet.<br>
InBlock.gif*
InBlock.gif*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
InBlock.gif*
InBlock.gif*
@paramrequesttherequestsendbytheclienttotheserver
InBlock.gif*
@paramresponsetheresponsesendbytheservertotheclient
InBlock.gif*
@throwsServletExceptionifanerroroccurred
InBlock.gif*
@throwsIOExceptionifanerroroccurred
ExpandedSubBlockEnd.gif
*/

InBlock.gif
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
throwsServletException,IOExceptiondot.gif{
InBlock.gif
InBlock.gifresponse.setContentType(
"text/html");
InBlock.gif
InBlock.gifPrintWriterout
=response.getWriter();
InBlock.gifout.println(
"<!DOCTYPEHTMLPUBLIC/"-//W3C//DTDHTML4.01Transitional//EN/">");
InBlock.gif
out.println("<HTML>");
InBlock.gifout.println(
"<HEAD><TITLE>AServlet</TITLE></HEAD>");
InBlock.gifout.println(
"<BODY>");
InBlock.gifout.print(
"Thisis");
InBlock.gifout.print(
this.getClass());
InBlock.gifout.println(
",usingtheGETmethod");
InBlock.gifout.println(
"</BODY>");
InBlock.gifout.println(
"</HTML>");
InBlock.gifout.flush();
InBlock.gifout.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/***//**
InBlock.gif*ThedoPostmethodoftheservlet.<br>
InBlock.gif*
InBlock.gif*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
InBlock.gif*
InBlock.gif*
@paramrequesttherequestsendbytheclienttotheserver
InBlock.gif*
@paramresponsetheresponsesendbytheservertotheclient
InBlock.gif*
@throwsServletExceptionifanerroroccurred
InBlock.gif*
@throwsIOExceptionifanerroroccurred
ExpandedSubBlockEnd.gif
*/

InBlock.gif
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
throwsServletException,IOExceptiondot.gif{
InBlock.gif
InBlock.gifresponse.setContentType(
"text/html");
InBlock.gifresponse.setCharacterEncoding(
"GBK");
InBlock.gifPrintWriterout
=response.getWriter();
InBlock.gif
InBlock.gifrequest.setCharacterEncoding(
"GBK");
InBlock.gifStringname
=request.getParameter("name");
InBlock.gifStringpwd
=request.getParameter("password");
InBlock.gifDBbasemyDb
=newDBbase();
InBlock.gifResultSetrs
=null;
InBlock.gif
intresult=0;
InBlock.gif
InBlock.gifStringsql
="selectcount(*)ascountfromuserswhereusername='"+name+"'andpassword='"+pwd+"'";
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifrs
=myDb.executeQuery(sql);
InBlock.gif
if(rs.next())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifresult
=rs.getInt("count");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifex.printStackTrace();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(result>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifout.println(
"合法用户");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifout.println(
"非法用户");
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/***//**
InBlock.gif*Initializationoftheservlet.<br>
InBlock.gif*
InBlock.gif*
@throwsServletExceptionifanerroroccure
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidinit()throwsServletExceptiondot.gif{
InBlock.gif
//Putyourcodehere
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

Web表示层:
None.gif
None.gif
<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
None.gif
None.gif
<%
None.gifStringpath
=request.getContextPath();
None.gifStringbasePath
=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
None.gif
%>
None.gif
None.gif
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
None.gif
<html>
None.gif
<head>
None.gif
<basehref="<%=basePath%>">
None.gif
None.gif
<title>MyJSP'index.jsp'startingpage</title>
None.gif
None.gif
<metahttp-equiv="pragma"content="no-cache">
None.gif
<metahttp-equiv="cache-control"content="no-cache">
None.gif
<metahttp-equiv="expires"content="0">
None.gif
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
None.gif
<metahttp-equiv="description"content="Thisismypage">
None.gif
None.gif
<!--
None.gif
<linkrel="stylesheet"type="text/css"href="styles.css">
None.gif
-->
None.gif
</head>
None.gif
None.gif
<body>
None.gif
<FORMname="form1"method="POST"action="/servletTest/servlet/Hello">
None.gif
<P>&nbsp;用户名:&nbsp;&nbsp;<INPUTtype="text"name="name"maxlength="20"/></P>
None.gif
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密码&nbsp;<INPUTtype="password"name="password"maxlength="20"/></P>
None.gif
<P>
None.gif
&nbsp;
None.gif
</P>
None.gif
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUTtype="Submit"name="button4"value="确定"/>&nbsp;&nbsp;<INPUTtype="Reset"name="button5"value="重置"></P>
None.gif
<P>
None.gif
&nbsp;
None.gif
</P>
None.gif
<P>
None.gif
&nbsp;
None.gif
</P>
None.gif
</FORM>
None.gif
</body>
None.gif
</html>
None.gif

看过一篇资料说,servlet能够与多个客户机连接,接受多个客户的输入,并将结果广播到多个客户机上,例如可以作为一个多人参与的游戏服务器,这点倒是让我挺吃惊的,不知道如何能够实现哪?如果是真的,那servlet是如何得知客户的详细信息的?难道可以象socket那样?不解。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值