ImageVerifierCode 换一换
格式:DOCX , 页数:27 ,大小:20.01KB ,
资源ID:10519087      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10519087.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Servlet学习笔记六Servlet之间的通信.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Servlet学习笔记六Servlet之间的通信.docx

1、Servlet学习笔记六Servlet之间的通信Servlet学习笔记(六)-Servlet之间的通信 Servlet学习笔记(六)-Servlet之间的通信 这是4个Servlet之间的通信2. 代码如下:1) 首先是test.htm文件,它作为客户端访问的页面<!DOCTYPE HTML PUBLIC -/W3C/DTD HTML 4.0 Transitional/EN><HTML><HEAD><META http-equiv=Content-Type content=text/html; charset=gb2312><META co

2、ntent=Microsoft FrontPage 4.0 name=GENERATOR></HEAD><BODY><CENTER><H1>Earnest Bank </H1></CENTER><FORM action=http:/127.0.0.1:8000/servlet/firstservlet method=post>enter your account Number here <INPUT name=accnum size=21> <BR><BR><BR

3、>enter your pin Number here&nbsp;&nbsp;&nbsp;&nbsp; <INPUT name=pinnum size=21> <BR> <p><CENTER><INPUT type=submit value=submit></CENTER></p></FORM></BODY></HTML> 2) 第一个Servelt代码:(作用是检验用户输入account Number 和pin Number是否合法)i

4、mport javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.sql.*;/import javax.sql.*; public class FirstServlet extends HttpServlet static Connection dbcon; static String resultToSecond=NA; protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletExce

5、ption, java.io.IOException try   Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); dbcon=DriverManager.getConnection(jdbc:odbc:test,sa,); System.out.println(Connection established); catch(Exception e) System.out.println(Database not found!); /Create a share attribute创建一个共享属性 ServletContext context=t

6、his.getServletContext(); context.setAttribute(accountNumber,); /得到从客户端发送过来的两个值 String accnum=req.getParameter(accnum); String pinnum=req.getParameter(pinnum); /Check whether the accountnum and pinnum are valid try PreparedStatement stat=dbcon.prepareStatement( select * from Login where cAccount_id=?

7、 and cPin_no=?); stat.setString(1,accnum); stat.setString(2,pinnum); ResultSet result=stat.executeQuery(); if(result.next() resultToSecond=result.getString(1); context.setAttribute(accountNumber,resultToSecond); /the account i valid call the second servlet通过验证后使用RequestDispatcher接口 /得到以URL为参数的Servle

8、t的引用 RequestDispatcher dispatcher=context.getRequestDispatcher(/secondservlet); if(dispatcher=null) resp.sendError(resp.SC_NO_CONTENT); /把请求提交或者委派给第二个Servlet dispatcher.forward(req,resp); try dbcon.close(); catch(Exception e) System.out.println(e.toString(); else resp.setContentType(text/html); Prin

9、tWriter out=resp.getWriter();   out.println(<html><body bgcolor=pink>); out.println( The account is invalid); out.println(<br><br>); out.println(</body></html>); resultToSecond=NA; out.close(); catch(Exception e)System.out.println(e); 3) 第二个Servelt代码:(作用是显示Ac

10、count Number,并允许用户输入Cheque number和支票金额amount)import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.sql.*;/import javax.sql.*;/通过用getAttribute()函数得到帐号,显示用户账单号。点击deposit按钮,调用第三个Servlet public class SecondServlet extends HttpServlet Connection dbcon; public void service(Servle

11、tRequest req,ServletResponse resp) throws ServletException,java.io.IOException resp.setContentType(text/html); PrintWriter out=resp.getWriter(); /Accessing the account number from the servlet context ServletContext context=this.getServletContext(); String value=(String)context.getAttribute(accountNu

12、mber); out.println(<html><head>); out.println(<META http-equiv=Content-Type content=text/html; charset=gb2312>);   out.println(<META content=MSHTML 6.00.2600.0 name=GENERATOR></HEAD>); out.println(<body><CENTER><H1>Earnest Bank </H1></CEN

13、TER>); out.println(<form action=http:/127.0.0.1:8000/servlet/thirdservlet method=post>); out.println(<b>Click the deposit button to deposit your money</b>); out.println(<table>); out.println(<tr><td>Account number: </td><td>+value+</td></tr

14、>); out.println(<tr><td>Cheque number: </td><td><input type=text name=checknum></td></tr>); out.println(<tr><td>Enter the amount to be deposited: </td><td><input type=text name=amount value=0></td></tr>); out.prin

15、tln(</table>); out.println(<input type=submit value=deposit>); out.println(<BR></form>); out.println(</body></html>); 4) 第三个Servelt代码:(作用是显示数据插入成功,并允于用户查看交易报告)import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.sql.*;/import javax.sql.*

16、; public class ThirdServlet extends HttpServlet Connection dbcon; String accountNumber; protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException try   Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); dbcon=DriverManager.getConnection(jdbc:

17、odbc:test,sa,); System.out.println(Connection established); catch(Exception e) System.out.println(Database not found!); /Accessing the account number from the servlet context ServletContext context=this.getServletContext(); String accnum=(String)context.getAttribute(accountNumber); resp.setContentTy

18、pe(text/html); PrintWriter out=resp.getWriter(); String amount=req.getParameter(amount); String checknum=req.getParameter(checknum); out.println(<html><body bgcolor=pink>); boolean checkcorrect=true; boolean amountcorrect=true; if(checknum.length()=0) checkcorrect=false; out.println(Pls

19、enter the cheque number properly); double mdeposit=Double.parseDouble(amount); if(mdeposit<=0) amountcorrect=false; out.println(The deposit amount must be valid); out.println(<br>Pls reenter the amount); if(checkcorrect && amountcorrect)   try PreparedStatement stat=dbcon.prepa

20、reStatement( insert Account_Holder_Transaction values(?,getDate(),?,?,?); String vcparticulars=new String(Cheque Deposit); stat.setString(1,accnum); stat.setString(2,vcparticulars); stat.setString(3,checknum); stat.setDouble(4,mdeposit); int rows=stat.executeUpdate(); try dbcon.close(); catch(Except

21、ion e) System.out.println(e); if(rows=0) System.out.println(Error insert);   else out.println(Your transaction details have been recorded); out.println(<br><br>); out.println(<form method=post action=http:/127.0.0.1:8000/servlet/fourthservlet>); out.println(<input type=subm

22、it value=report>); out.println(</form>); out.println(</body>); out.println(</html>); catch(Exception e) System.out.println(e); 4) 第四个Servelt代码:(显示该Account Number报告详细细节)import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import java.util.Calenda

23、r;import javax.sql.*;  import java.sql.*; public class FourthServlet extends HttpServlet/ String accountnumber; Connection dbcon; public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); dbcon=DriverM

24、anager.getConnection(jdbc:odbc:test,sa,); catch(ClassNotFoundException e) System.out.println(Database driver not found); System.out.println(e.toString(); throw new UnavailableException(this,Cannot connect to the database); catch(Exception e) System.out.println(UNKNOWN!?); ServletContext context=getS

25、ervletContext(); Object obj=context.getAttribute(accountNumber); String accnum=obj.toString(); double amount=0.0; PrintWriter out=res.getWriter(); res.setContentType(text/html); try   PreparedStatement s=dbcon.prepareStatement(select mBalance from Account_Holder Where cAccount_id=?); s.setStrin

26、g(1,accnum); ResultSet result=s.executeQuery(); result.next(); if(result=null) System.out.println(Error executing the query); else amount=result.getDouble(1); catch(SQLException e) System.out.println(e.toString(); out.println(<html>); out.println(<body bgcolor=pink>); out.println(<hea

27、d>); out.println(<title>); out.println(Balance);   out.println(</title>); out.println(</head>); out.println(<b><u>Your balance details</u></b>); out.println(<br>); out.println(Account number: +accnum); out.println(<br>); out.println(<b

28、><font size=5 color=green> Balance statement</font></b>); boolean rowfound=true; out.println(<hr>); out.println(<table cellpadding=20>); out.println(<tr>); out.println(<td>); out.println(Account Number ); out.println(</td>); out.println(<td>);

29、 out.println(Date ); out.println(</td>); out.println(<td>); out.println(Amount Deposited(Rs) ); out.println(</td>); out.println(</tr>); out.println(</table>); out.println(<hr>); &nbsp;   int totalrows=0; try PreparedStatement s1=dbcon.prepareStatement(select count(*) from account_holder_transaction where cAccount_id=?); s1.setString(1,accnum); ResultSet r=s1.executeQuery(); r.next(); totalrows=r.getInt(1); totalrows-=20; catch(Exception e) try

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

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