家庭记账本三

根据自己设计的一些功能,今天还差的就是修改密码和查看账本,一些分类查看。

按月份查找:

按收支查找:

 

 修改密码:

  1 package Servlet;
  2 
  3 import java.io.IOException;
  4 import java.util.List;
  5 
  6 import javax.servlet.ServletException;
  7 import javax.servlet.annotation.WebServlet;
  8 import javax.servlet.http.HttpServlet;
  9 import javax.servlet.http.HttpServletRequest;
 10 import javax.servlet.http.HttpServletResponse;
 11 
 12 
 13 
 14 import Dao.Dao;
 15 import bean.Usertable;
 16 import bean.Account;
 17 
 18 
 19 
 20 /**
 21  * Servlet implementation class Servlet
 22  */
 23 @WebServlet("/Servlet")
 24 public class Servlet extends HttpServlet {
 25     Dao dao = new Dao();
 26     Usertable name = new Usertable();
 27     private static final long serialVersionUID = 1L;
 28     
 29     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 30            req.setCharacterEncoding("utf-8");
 31            String method = req.getParameter("method");
 32            System.out.println(method);
 33            if ("denglu".equals(method)) {//登录
 34                System.out.println("ok");
 35                denglucheck(req, resp);
 36            }
 37            else if("load".equals(method)) {//查看账单
 38                System.out.println(method);
 39                listall(req,resp);
 40            }
 41            else if("getuserbyid".equals(method)) {
 42                System.out.println(method);
 43                getUserById(req,resp);
 44            }
 45            else if("update1".equals(method)) {//修改密码
 46                System.out.println(method);
 47                Update1(req,resp);
 48            }
 49            else if("update2".equals(method)) {//修改密码
 50                System.out.println(method);
 51                Update2(req,resp);
 52            }
 53            else if("zhuce".equals(method)) {//注册
 54                System.out.println(method);
 55                Adduser(req,resp);
 56            }
 57            else if("addaccount".equals(method))//记账
 58            {
 59                Addaccount(req,resp);
 60            }
 61            else if("time".equals(method))
 62            {
 63                time(req,resp);
 64            }
 65            else if("mtype".equals(method))
 66            {
 67                mtype(req,resp);
 68            }
 69        }
 70        
 71     private void mtype(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
 72         // TODO Auto-generated method stub
 73         response.setContentType("text/html;charset=utf8");
 74         request.setCharacterEncoding("utf-8");
 75         response.getWriter().append("Served at: ").append(request.getContextPath());
 76         String time="";
 77         String mtype = request.getParameter("mtype");
 78         System.out.println(mtype);
 79         List<Account> account = dao.search(time, mtype);
 80         request.setAttribute("account", account);
 81         request.getRequestDispatcher("list.jsp").forward(request, response);
 82     }
 83 
 84     private void time(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
 85         // TODO Auto-generated method stub
 86         response.setContentType("text/html;charset=utf8");
 87         request.setCharacterEncoding("utf-8");
 88         response.getWriter().append("Served at: ").append(request.getContextPath());
 89         String mtype="";
 90         String time = request.getParameter("time");
 91         time = "2019-" + time;
 92         System.out.println(time);
 93         List<Account> account = dao.search(time, mtype);
 94         request.setAttribute("account", account);
 95         request.getRequestDispatcher("list.jsp").forward(request, response);
 96     }
 97 
 98     private void Update2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
 99         // TODO Auto-generated method stub
100         response.setContentType("text/html;charset=utf8");
101         request.setCharacterEncoding("utf-8");
102         response.getWriter().append("Served at: ").append(request.getContextPath());
103         String username = request.getParameter("name");
104         String pwd1 = request.getParameter("pwd1");
105         String pwd2 = request.getParameter("pwd2");
106         Usertable bean1 = new Usertable(username,pwd1);
107         Usertable bean = new Usertable(username,pwd2);
108         int a = 0;
109         if(dao.getpwdByuser(username).equals(bean1)) {
110         if(dao.update(bean)) 
111         {
112             a = 1;
113         }
114         }
115         request.setAttribute("a", a);
116         request.getRequestDispatcher("change_res.jsp").forward(request, response);
117     }
118 
119     private void Addaccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
120         // TODO Auto-generated method stub
121         response.setContentType("text/html;charset=utf8");
122         request.setCharacterEncoding("utf-8");
123         response.getWriter().append("Served at: ").append(request.getContextPath());
124         String thing = request.getParameter("thing");
125         int money = Integer.valueOf(request.getParameter("money"));
126         String mtype = request.getParameter("mtype");
127         System.out.println(thing + mtype+money );
128         String time = Dao.getNowTime();
129         if(mtype.equals("支出"))
130             {
131                 money =0-money;
132                 mtype = "支出";
133             }
134         else {mtype = "收入";}
135         Account acc = new Account(thing,money,mtype,time);
136         Dao.addNote(acc);
137         int a = 0;
138         //if(Dao.addNote(acc))
139         //UUser = Integer.valueOf(request.getParameter("logname"));
140         
141         request.setAttribute("a", a);
142         request.getRequestDispatcher("addacc_res.jsp").forward(request, response);
143     }
144 
145     private void Adduser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
146         // TODO Auto-generated method stub
147         response.setContentType("text/html;charset=utf8");
148         request.setCharacterEncoding("utf-8");
149         response.getWriter().append("Served at: ").append(request.getContextPath());
150         String username = request.getParameter("logname");
151         String pwd = request.getParameter("logpass1");
152         String mail = request.getParameter("mail");
153         Usertable bean = new Usertable(username,pwd,mail);
154         int a = 0;
155         if(dao.checkusername(username))
156         {
157             Dao.addusers(bean);
158             a = 1;
159         }
160         //UUser = Integer.valueOf(request.getParameter("logname"));
161         
162         request.setAttribute("a", a);
163         request.getRequestDispatcher("zhuce_res.jsp").forward(request, response);
164         
165     }
166 
167     private void Update1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
168         // TODO Auto-generated method stub
169         response.setContentType("text/html;charset=utf8");
170         request.setCharacterEncoding("utf-8");
171         response.getWriter().append("Served at: ").append(request.getContextPath());
172         String username = name.getUsername();
173         System.out.println(username);
174         request.setAttribute("username", username);
175         request.getRequestDispatcher("change.jsp").forward(request, response);
176     }
177 
178     private void getUserById(HttpServletRequest req, HttpServletResponse resp) {
179         // TODO Auto-generated method stub
180         
181     }
182 
183     private void listall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
184         // TODO Auto-generated method stub
185         req.setCharacterEncoding("utf-8");
186         List<Account> account = dao.list();
187         req.setAttribute("account", account);
188         req.getRequestDispatcher("list.jsp").forward(req,resp);
189     }
190 
191     private void denglucheck(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException  {
192         // TODO Auto-generated method stub
193         resp.setContentType("text/html;charset=utf8");
194         req.setCharacterEncoding("utf-8");
195         resp.getWriter().append("Served at: ").append(req.getContextPath());
196         String username = req.getParameter("logname");
197         String pwd = req.getParameter("logpass");
198         String t = Dao.getNowTime();
199         Usertable bean = new Usertable(username,pwd);
200         System.out.println("用户名:"+username+"密码:"+pwd +"时间:" + t);
201         int a = 0;
202         if(!dao.checkusername(username))
203         {
204         Usertable p=dao.getpwdByuser(username);
205         if(p.getPwd().equals(pwd))
206         {
207             a = 1;
208             name = bean;
209         }
210         //UUser = Integer.valueOf(request.getParameter("logname"));
211         }
212         req.setAttribute("a", a);
213         req.setAttribute("username", username);
214         req.getRequestDispatcher("panduan.jsp").forward(req, resp);
215         req.getRequestDispatcher("top.jsp").forward(req, resp);
216     }
217 
218     /**
219      * @see HttpServlet#HttpServlet()
220      */
221     public Servlet() {
222         super();
223         // TODO Auto-generated constructor stub
224     }
225 
226     /**
227      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
228      */
229     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
230         // TODO Auto-generated method stub
231         response.getWriter().append("Served at: ").append(request.getContextPath());
232     }
233 
234     /**
235      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
236      */
237     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
238         // TODO Auto-generated method stub
239         doGet(request, response);
240     }
241 
242 }
servlet代码
原文地址:https://www.cnblogs.com/flw0322/p/10422705.html