Friday 15 May 2015

Connecting the Servlet to the jsp page table -



Connecting the Servlet to the jsp page table -

how connect servlet:

@webservlet(urlpatterns = {"/accountlookupservlet"}) public class accountlookupservlet extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); seek (printwriter out = response.getwriter()) { /* todo output page here. may utilize next sample code. */ string id = request.getparameter("id"); string pw = request.getparameter("pw"); system.out.println(id); system.out.println(pw); business relationship a1=new account(); a1.finddb(id); string an=a1.getacctno(); system.out.println(an); string ln=a1.getcustid(); system.out.println(ln); string at=a1.gettype(); system.out.println(at); double bal=a1.getbalance(); system.out.println(bal); httpsession ses1=request.getsession(); ses1.setattribute("a1", a1); } } /** * handles http <code>get</code> method. */ @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request,response); } /** * handles http <code>post</code> method. */ @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string action = request.getparameter("action"); if (action.equals(null)) { request.getrequestdispatcher("/index.jsp").forward(request, response); } /* gets single searched business relationship accountlookup.jsp */ else if (action.equals("getacct")) { string acctno = request.getparameter("accountno"); seek { /* instantiates new business relationship class , sets request attribute utilize in accountview.jsp */ business relationship account = new account(acctno); request.setattribute("account", account); request.getrequestdispatcher("/accountview.jsp").forward(request, response); } grab (sqlexception ex) { logger.getlogger(accountlookupservlet.class.getname()).log(level.severe, null, ex); } } } /** * returns short description of servlet. * * @return string containing servlet description */ @override public string getservletinfo() { homecoming "short description"; } }

to connect jsp page pull out client id, business relationship number, business relationship type, , balance appear in table created? servlet working, database connected, need way jsp page read in info , print out.

<%@page import="business.account"%> <%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>your account</title> <link rel="stylesheet" href="chattbank.css" type="text/css"> <style> #image{ text-align: center; } #acctform{ text-align: center; } #accttable{ margin-left: 645px; } #submittable{ margin-left: 755px; } table{ margin-left: auto; margin-right: auto; } h4{ text-align: center; } </style> </head> <body> <div id = "image"> <img src="bankpic.jpg" /> </div> <h4>here business relationship requested</h4> <table border="1" width="75%" cellspacing="5" cellpadding="2"> <thead> <tr> <th colspan="10">your chatt accounts</th> </tr> </thead> <tbody> <tr> <td colspan="2"></td> <td colspan="2">customer id</td> <td colspan="2">account number</td> <td colspan="2">account type</td> <td colspan="2">account balance</td> </tr> <tr> <td colspan="2">account: </td> <td colspan="2">${account.custid}</td> <td colspan="2">${account.acctno}</td> <td colspan="2">${account.type}</td> <td colspan="2">${account.balance}</td> </tr> </tbody> </table> <table width="50%" cellspacing="5" cellpadding="2"> <thead> <tr> <td></td> <td>click here navigate new page</td> <td></td> </tr> </thead> <tbody> <tr> <td><a href="accountservlet?action=view&id=${customer.custid}">view accounts</a></td> <td><a href="manageaccounts?action=manage&id=${customer.custid}">manage accounts</a></td> <td><a href="logout?action=logout">log out</a></td> </tr> </tbody> </table> </body> </html>

here business relationship business object: want create sure right hooking accountview.jsp connect business relationship pull out db business relationship information.

public class business relationship implements serializable{ private string acctno; private string custid; private string type; private double balance; private string message; /** * no-arg constructor */ public account() { } /** * single arg constructor accepts 1 parameter * connects database , builds customers * business relationship object * @param acct */ public account(string acct) throws sqlexception { connection connect = null; statement statement = null; resultset result = null; string sql = "select * accounts acctno = '" + acct + "';"; seek { /*establishes connection , creates statement */ connect = acctconnect(); statement = connect.createstatement(); result = statement.executequery(sql); /*loops through database row containing info , builds row*/ while (result.next()) { this.acctno = result.getstring("acctno"); this.custid = result.getstring("cid"); this.type = result.getstring("type"); this.balance = result.getdouble("balance"); } } grab (sqlexception e) { system.out.println("error: " + e); } { connect.close(); } } /** *sets business relationship number *@param acctno */ public void setacctno(string acctno) { this.acctno = acctno; } /** *sets client id * @param custid */ public void setcustid(string custid) { this.custid = custid; } /** * sets business relationship type * @param accttype */ public void type(string type) { this.type = type; } /** * sets balance business relationship * @param balance */ public void setbalance(double balance) { this.balance = balance; } /** * returns business relationship number * @return string */ public string getacctno() { homecoming acctno; } /** * returns client id * @return string */ public string getcustid() { homecoming custid; } /** * returns business relationship type * @return string */ public string gettype() { homecoming type; } /** * returns business relationship balance * @return double */ public double getbalance() { homecoming balance; } /** * returns business relationship info string form * @return string */ public string getacct() { homecoming this.acctno + " " + this.custid + " " + this.type + " " + this.balance; } /** * returns message set other methods in class * @return string */ public string getmessage(){ homecoming this.message; }; /** * establishes connection database * @return connection */ public static connection acctconnect() { seek { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); } grab (classnotfoundexception e) { system.out.println("errors: " + e); } /*declare new connection*/ connection con = null; seek { /*define connection*/ con = drivermanager.getconnection("jdbc:odbc:chattdb"); } grab (sqlexception e) { system.out.println("error: " + e); } /*returns connection con*/ homecoming con; } /** * uses object business relationship number info database * business relationship number valid */ public void finddb() { connection connect = null; statement statement = null; resultset result = null; string sql = "select * accounts acctno = '" + acctno + "';"; seek { connect = acctconnect(); statement = connect.createstatement(); result = statement.executequery(sql); /*loop through result gather of info row in database*/ while (result.next()) { this.acctno = result.getstring("acctno"); this.custid = result.getstring("cid"); this.type = result.getstring("type"); this.balance = result.getdouble("balance"); } connect.close(); } grab (sqlexception e) { system.out.println("error: " + e); } } /** * connects database accesses desired business relationship , deposits * account. updates database. , closes connection. * method throws sql exception. * @param acctno * @param depamount * @throws sqlexception */ public void deposit(string acctno, double depamount) throws sqlexception { connection connect = acctconnect(); statement statement = connect.createstatement(); resultset result = null; string sql = "select balance accounts acctno = '" + acctno + "';"; string update = null; int updateset = 0; double balance = 0.00; double newbalance; result = statement.executequery(sql); /* retrieves balance of current business relationship */ while (result.next()) { balance = result.getdouble("balance"); } /* updates balance in object , database */ newbalance = balance + depamount; update = "update accounts set balance = '" + newbalance + "' acctno = '" + acctno + "';"; updateset = statement.executeupdate(update); this.balance = newbalance; /* closes connection */ connect.close(); } /** * withdraws funds current account. throws exception * insufficient funds * @param acctno * @param withdrawal * @throws sqlexception * @throws exception */ public void withdraw(string acctno, double withdrawal) throws sqlexception, exception { connection connect = acctconnect(); statement statement = connect.createstatement(); resultset result = null; string sql = "select balance accounts acctno = '" + acctno + "';"; string update = null; int updateset = 0; double balance = 0.00; double newbalance; result = statement.executequery(sql); /* gets balance of current business relationship database */ while (result.next()) { balance = result.getdouble("balance"); } /* checks see if withdrawal amount more balance , if exception thrown , insufficient funds message set */ if (balance < withdrawal){ this.message = "insufficcient funds"; throw new exception(); }else{ /* sets new balance , updates current database business relationship balance */ newbalance = balance - withdrawal; update = "update accounts set balance = '" + newbalance + "' acctno = '" + acctno + "';"; updateset = statement.executeupdate(update); this.balance = newbalance; } /* closes connection database */ connect.close(); } /** * transfers funds 1 business relationship calling withdrawal * , deposit methods. * @param fromacct * @param toacct * @param transfer * @throws sqlexception * @throws exception */ public void transfer(string fromacct, string toacct, double transfer) throws sqlexception, exception { /* phone call withdraw method on business relationship */ withdraw(fromacct, transfer); /* deposit to business relationship */ deposit(toacct, transfer); /* both methods close own database connection not necessary here */ } /** * sets new business relationship existing customers * @param acctno * @param custid * @param type * @param balance * @throws sqlexception */ public void estabaccount(string custid, string type, double balance) throws sqlexception { system.out.println(custid + " " + type + " " + balance); /* found connection , update database new row containing new business relationship info */ connection connect = acctconnect(); statement statement = connect.createstatement(); string sql = "insert accounts (cid, type, balance) values ('" + custid + "', '" + type + "', " + balance + ");"; system.out.println(sql); /* execute query , close connection */ statement.executeupdate(sql); connect.close(); } public void finddb(string id) { throw new unsupportedoperationexception("not supported yet."); //to alter body of generated methods, take tools | templates. } }

account business relationship = new account(acctno); request.setattribute("account", account); request.getrequestdispatcher("/accountview.jsp").forward(request, response);

your passing object of account class lone . if need value of custid in jsp . need set values in request help of setters

account business relationship = new account(); account.setcustid("value");

and pass object in request ,

request.setattribute("account", account);

so available when trying print in jsp ,

<td colspan="2">${account.custid}</td>

i assume model class account has setters , getters

jsp servlets

No comments:

Post a Comment