Jsp+servlet+mysql enables simple bank login and transfer functions
"Pre-Prep"
HTML (Login interface), servlet (processing business logic), JSP (main implementation interface), MySQL (Implement simple interaction with database)
Start with a login page (using form submission, action= "JSP implementation page")
The first step: Create a table in the (MySQL) database:
Set up a user's basic information table (user name, password, account balance), user transfer (user name, transfer amount) of the action sheet
Step Two:
Login page: Enter the user name and password to verify the user information (login.jsp) in the database (QQ browser has a bookmark inside the login success verification, login successfully verify that the jump page can be changed to another page (it contains can see their account balance in the amount of money, there is a link to transfer) (Login.js P,back.jsp (Login failed), Database processing Class (Db1.java) (mainly authentication user name and password, there is a method Boolean loginsucess (String username,string password)), Servlet class (There are Dopost methods, which mainly constructs the database class object, the call inside the Loginsucess method, if the user name and password are not wrong, using response.sendredirect jump to zhuangzhang.jsp))
Post-login page (the function of transfer):
Mainly the function of transfer:
{transfer: After entering the account and password, can determine the user according to the password is legal, and then enter the other's transfer account, and determine the other's transfer account is legitimate, if lawful, and then enter the amount of the transfer, and can determine whether the transfer amount exceeds the balance of the account, if the transfer amount is legal, Update the account balance information and the account balance information of the other party.
{} Part is a paragraph from the Internet, the second transfer part of the database processing class, you can write two methods (1. The method of judging (transfer out account, transfer amount, whether the transfer account is legal, the return type of this method can be used Boolean) 2. Update method (to update the balance information of the account and the account balance information of the other party)
Specific code
Boolean Puanduan (String outuser,double cash,string inuser);
void Update ();
}
These two methods are then called in the servlet.
The transfer link to (Transfer page, (zhuanzhang.jsp) This page contains three input boxes: Transfer out account (Outuser), transfer Amount (cash), to account (Inuser)), and then write a database processing class (Db2.java): Write a method Dozz is used to deal with the transfer of the amount of the transfer account by transferring funds to reduce cash, to the account amount increased cash, specific statements refer to the JDBC statement and MySQL statement. And then we're going to write a servlet class, or through the Dopost method to get the value of the zhuanzhang.jsp page input box, and then by creating a method of the database class to invoke the Dozz method to implement its function, and then response.sendredirect jump to a page (their own user-reduced page done.jsp ??? )
" Critical Code "
Loginservlet.java
Package servlet;
Import java.io.IOException;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import javax.websocket.Session;
Import Org.apache.jasper.tagplugins.jstl.core.Out;
Import Javax.servlet.http.HttpServlet;
Import JavaBean. DB01;
public class Loginservlet extends httpservlet{
Static String userId;
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
UserId = Request.getparameter ("id");
HttpSession session=request.getsession ();
Session.setattribute ("User", userId);
String Password = request.getparameter ("password");
DB01 db1 = new DB01 ();
Boolean canlogin = Db1.loginsuccess (Userid,password);
if (Canlogin) {
Response.sendredirect ("transfer.jsp");
}
else if (!canlogin) {
Response.sendredirect ("back.jsp");
}
}
Public ServletConfig Getservletconfig () {
return null;
}
Public String Getservletinfo () {
return null;
}
public void Service (ServletRequest request,servletresponse response) throws Servletexception, ioexception{
HttpServletRequest RQ = (httpservletrequest) request;
HttpServletResponse rs = (httpservletresponse) response;
DoPost (RQ,RS);
}
public void Destroy () {
System.out.println ("DESTROY");
}
public void init (ServletConfig args0) throws servletexception{
System.out.println ("Init");
}
}
Checkservlet.java
Package servlet;
Import JavaBean. DB02;
Import java.io.IOException;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import javax.websocket.Session;
Import Org.apache.jasper.tagplugins.jstl.core.Out;
Import Org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
Import Javax.servlet.http.HttpServlet;
Import JavaBean. DB01;
Import sun.text.resources.cldr.es.FormatData_es_419;
public class Checkservlet extends httpservlet{
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
HttpSession session=request.getsession ();
String Outid = (string) session.getattribute ("User");
String inId = Request.getparameter ("InId");
String Cash0 = Request.getparameter ("cash");
DB02 DB2 = new DB02 ();
Boolean checksuccess = Db2.checkandchangeuser (Outid,cash0,inid);
if (checksuccess) {
System.out.println (Outid);
System.out.println (INID);
Response.sendredirect ("success.jsp");
}
else if (!checksuccess) {
Response.sendredirect ("transfer.jsp");
}
}
Public ServletConfig Getservletconfig () {
return null;
}
Public String Getservletinfo () {
return null;
}
public void Service (ServletRequest request,servletresponse response) throws Servletexception, ioexception{
HttpServletRequest RQ = (httpservletrequest) request;
HttpServletResponse rs = (httpservletresponse) response;
DoPost (RQ,RS);
}
public void Destroy () {
System.out.println ("DESTROY");
}
public void init (ServletConfig args0) throws servletexception{
System.out.println ("Init");
}
}
" post-impression and summary "
Summary of the Bank login page
After two weeks, no, maybe only for a week or so, because there are more things in the previous week. Also part of the reason is that they are not too understanding the overall knowledge of the structure, and the seniors are just a rough talk about some basic knowledge. Then you have to start writing, just to achieve a simple login to achieve the user's transfer function.
Prophase:
1. Oneself also on the Internet to find some information, about Jsp+servlet+mysql, to achieve simple login verification, also, is also blame oneself before summer vacation study Java did not learn and used java.sql.* inside some interface, Used to connect with the database, there is not how to use the idea before, and then the previous week, how to introduce the database driver in idea, but also asked the seniors and other talent to solve it, but in the time before the settlement, this small bank login transfer, I began to write with Eclipse, Fortunately, because most of the time before the eclipse, and then use it more smoothly, after some small test with the database, or to prove that the introduction of the database driver package introduced smoothly.
2. I also wrote a pre-to do before you can be said to be a small demand document, but feel not how to write this before, it is also a bit difficult to write, so-called everything at the beginning of the difficult, it is probably so!
3. Then according to their own requirements of the document, one step to realize the function, the middle or met a few more nerve-racking problems, troubled me for two days, Baidu did not come out, and finally asked the classmate, compared to the progress of their own learning is a bit slow.
4. Another problem is that before learning JSP, forget that there is a thing, is the session object, is generally used to store the user login user name and password, and then use each other in a number of pages, before this point forget, the problem is relatively large, not the user logged in to the user , can be any user between the transfer function, so, this is a very serious error, so, the Internet to check the information, read a book, and then I also understand some, then is through the login, jump to the Transfer page, this page only, enter the transfer amount and transfer to the user.
5. And then I think about it, a user must want to know how much money in his account, so, I later thought about it, made a link to check the balance of my account, and then measure the amount of my own transfer.
Summed up, is that if you just want to do, and do not do, there is no harvest, so, I am not only reading a day, to more knock code, even if the book knocks, their own slowly understand, and then knock again, so much to consolidate, to make their hands-on ability to enhance.
Through this, a simple bank login, let me still learned a lot of knowledge, JSP inside a few common built-in objects, as well as the Java inside how to connect the database code, and then there is a servlet inside some Dopost method, service method, Init method, Destroy method, and so on a lot of knowledge, and then their own harvest: to summarize from the practice, the bug to learn how to tune, Port really, if a bug fix bad, really will affect the back of a lot of things, there is, do not let their code directory looks too messy, the code is still to be streamlined and tidy, If there is duplicate code, try to write a tool class to achieve, it is important to distinguish the level.
Jsp+servlet+mysql enables simple bank login and transfer functions