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