Web開發基礎_Servlet學習_0025_專案練習(八)
阿新 • • 發佈:2018-12-10
專案名稱:中國電信運營支援系統-網路版(七)
專案登記部分
jstl:import標籤 <c:import>說明
案例演示:
工程案例目錄結構
MainServlet.java
package web; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.AdminDao; import dao.CostDao; import entity.Admin; import entity.Cost; public class MainServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String path = req.getServletPath(); if("/findCost.do".equals(path)){ //查詢資費 System.out.println(path); findCost(req,res); }else if("/toAddCost.do".equals(path)){ //開啟增加資費頁 toAddCost(req,res); }else if("/addCost.do".equals(path)){ //增加儲存資費 addCost(req,res); }else if("/toUpdateCost.do".equals(path)){ //開啟修改資費頁 toUpdateCost(req,res); }else if("/updateCost.do".equals(path)){ //修改資費 updateCost(req,res); }else if("/deleteCost.do".equals(path)){ //刪除資費 deleteCost(req,res); }else if("/toLogin.do".equals(path)){ //開啟登入頁 toLogin(req,res); }else if("/toIndex.do".equals(path)){ //開啟主頁 toIndex(req,res); }else if("/login.do".equals(path)){ //登入驗證 login(req,res); }else{ //錯誤的路徑 throw new RuntimeException("沒有這個頁面"); } } protected void login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //接收表單資料 String adminCode = req.getParameter("adminCode"); String password = req.getParameter("password"); //校驗 AdminDao dao = new AdminDao(); Admin admin = dao.findByCode(adminCode); if(admin == null){ //賬號不存在 req.setAttribute("error", "賬號不存在"); req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res); }else if(!admin.getPassword().equals(password)){ //密碼錯誤 req.setAttribute("error", "密碼錯誤"); req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res); }else{ //校驗通過 //當前:/netctoss/login.do //目標:/netctoss/toIndex.do res.sendRedirect("toIndex.do"); } } protected void toIndex(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("toIndex......"); req.getRequestDispatcher("WEB-INF/main/index.jsp").forward(req, res); } protected void toLogin(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //當前:/netctoss/toLogin.do //目標:/netctoss/WEB-INF/main/login.jsp System.out.println("toLogin.do"); req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res); } protected void deleteCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String costId = req.getParameter("costId"); CostDao dao = new CostDao(); dao.delete(new Integer(costId)); System.out.println("deleteCost......"); //3.重定向到查詢 //當前:/netctoss/deleteCost.do //目標:/netctoss/findCost.do res.sendRedirect("findCost.do"); } protected void updateCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //1.接收表單資料 String costId = req.getParameter("costId"); String name = req.getParameter("name"); String costType = req.getParameter("costType"); String descr = req.getParameter("descr"); String baseDuration = req.getParameter("baseDuration"); String baseCost = req.getParameter("baseCost"); String unitCost = req.getParameter("unitCost"); //2.儲存這些資料 Cost c = new Cost(); c.setCostId(new Integer(costId)); c.setName(name); c.setCostType(costType); c.setDescr(descr); if(baseDuration !=null && !baseDuration.equals("")){// c.setBaseDuration(new Integer(baseDuration)); } if(baseCost !=null && !baseCost.equals("")){ c.setBaseCost(new Double(baseCost)); } if(unitCost !=null && !unitCost.equals("")){ c.setUnitCost(new Double(unitCost)); } CostDao dao = new CostDao(); System.out.println(c); dao.update(c); //3.重定向到查詢 //當前:/netctoss/addCost.do //目標:/netctoss/findCost.do res.sendRedirect("findCost.do"); } protected void toUpdateCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("toUpdateCost......."); //接收引數 String id = req.getParameter("id"); //查詢要修改的資費 CostDao dao = new CostDao(); Cost cost = dao.findById(new Integer(id)); //轉發到修改頁 req.setAttribute("cost", cost); req.getRequestDispatcher("WEB-INF/cost/update.jsp").forward(req, res); } protected void addCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //1.接收表單資料 String name = req.getParameter("name"); String costType = req.getParameter("costType"); String descr = req.getParameter("descr"); String baseDuration = req.getParameter("baseDuration"); String baseCost = req.getParameter("baseCost"); String unitCost = req.getParameter("unitCost"); //2.儲存這些資料 Cost c = new Cost(); c.setName(name); c.setCostType(costType); c.setDescr(descr); if(baseDuration !=null && !baseDuration.equals("")){// c.setBaseDuration(new Integer(baseDuration)); } if(baseCost !=null && !baseCost.equals("")){ c.setBaseCost(new Double(baseCost)); } if(unitCost !=null && !unitCost.equals("")){ c.setUnitCost(new Double(unitCost)); } CostDao dao = new CostDao(); dao.save(c); //3.重定向到查詢 //當前:/netctoss/addCost.do //目標:/netctoss/findCost.do res.sendRedirect("findCost.do"); } protected void toAddCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //當前:/netctoss/toAddCost.do //目標:/netctoss/WEB-INF/cost/add.jsp req.getRequestDispatcher("WEB-INF/cost/add.jsp").forward(req, res); } protected void findCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //v1 // //查詢資費 // CostDao dao = new CostDao(); // List<Cost> list = dao.findAll(); // //轉發到查詢頁面 // req.setAttribute("costs", list); // //當前:/netctoss/findCost.dao // //目標:/netctoss/WEB-INF/cost/find.jsp // System.out.println("into--findCost"); // req.getRequestDispatcher("WEB-INF/cost/find.jsp").forward(req, res); //v2 分頁 //獲取請求引數 String page = req.getParameter("page"); if(page == null || page.equals("")){ page = "1"; } System.out.println(page); //獲取常量 String size = this.getServletContext().getInitParameter("size"); //查詢資費 CostDao dao = new CostDao(); List<Cost> list= dao.findByPage(new Integer(page), new Integer(size)); //查詢總行數,計算出總頁數 int rows = dao.findRows(); int total =rows/(new Integer(size)); if(rows%new Integer(size) != 0){ total++; } //轉發到查詢頁面 req.setAttribute("costs", list); req.setAttribute("total", total); req.setAttribute("page", page); //當前:/netctoss/findCost.do //目標:/netctoss/WEB-INF/cost/find.jsp req.getRequestDispatcher("WEB-INF/cost/find.jsp").forward(req, res); } }
login.jsp
<%@page pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>案例-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css"/> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css"/> </head> <body class="index"> <div class="login_box"> <form action="login.do" method="post"> <table> <tr> <td class="login_info">賬號:</td> <td colspan="2"><input name="adminCode" type="text" class="width150" /></td> <td class="login_error_info"><span class="required">30長度的字母、數字和下劃線</span></td> </tr> <tr> <td class="login_info">密碼:</td> <td colspan="2"><input name="password" type="password" class="width150"/></td> <td><span class="required">30長度的字母、數字和下劃線</span></td> </tr> <tr> <td class="login_info">驗證碼:</td> <td class="width70"><input name="" type="text" class="width70"/></td> <td><img src="images/valicode.jpg" alt="驗證碼" title="點選更換"/></td> <td><span class="required"></span></td> </tr> <tr> <td></td> <td class="login_button" colspan="2"> <a href="javascript:document.forms[0].submit();"><img src="images/login_btn.png" alt="" /></a> </td> <td><span class="required">${error }</span></td> </tr> </table> </form> </div> </body> </html>
index.jsp
<%@page pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>案例-NetCTOSS</title> <link type="text/css" rel="stylesheet" media="all" href="styles/global.css"/> <link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" /> </head> <body class="index"> <!-- 導航區域開始 --> <div id="index_navi"> <!-- <ul id="menu"> <li><a href="index.html" class="index_off"></a></li> <li><a href="role/role_list.html" class="role_off"></a></li> <li><a href="admin/admin_list.html" class="admin_off"></a></li> <li><a href="fee/fee_list.html" class="fee_off"></a></li> <li><a href="account/account_list.html" class="account_off"></a></li> <li><a href="service/service_list.html" class="service_off"></a></li> <li><a href="bill/bill_list.html" class="bill_off"></a></li> <li><a href="report/report_list.html" class="report_off"></a></li> <li><a href="user/user_info.html" class="information_off"></a></li> <li><a href="user/user_modi_pwd.html" class="password_off"></a></li> </ul> --> <c:import url="../nav.jsp"></c:import> </div> </body> </html>
nav.jsp
<%@page pageEncoding="utf-8"%>
<ul id="menu">
<li><a href="/netctoss/toIndex.do" class="index_off"></a></li>
<li><a href="role/role_list.html" class="role_off"></a></li>
<li><a href="admin/admin_list.html" class="admin_off"></a></li>
<li><a href="/netctoss/findCost.do" class="fee_off"></a></li>
<li><a href="account/account_list.html" class="account_off"></a></li>
<li><a href="service/service_list.html" class="service_off"></a></li>
<li><a href="bill/bill_list.html" class="bill_off"></a></li>
<li><a href="report/report_list.html" class="report_off"></a></li>
<li><a href="user/user_info.html" class="information_off"></a></li>
<li><a href="user/user_modi_pwd.html" class="password_off"></a></li>
</ul>
將netctoss工程部署到Tomcat上,執行Tomcat啟動案例工程,
錄入賬戶與密碼進行登記: