Web开发基础_Servlet学习_0025_项目练习(八)

项目名称:中国电信运营支持系统-网络版(七)


1. NETCTOSS
 NET    网络
 C        China
 T        Telecom        电信
 O        Operation    运营
 S        Support        支持
 S        System        系统
 中国电信运营支持系统-网络版
 
2.开发步骤
1)创建项目
2)导包
 - javaee    tomcat
 - jstl        jstl
 - jdbc        ojdbc
 - dbcp        commons-dbcp

3.Servlet路径规范
 1)查询资费: /findCost.do
 2)打开增加资费:/toAddCost.do
 3)增加保存资费:/addCost.do
 4)打开修改资费:/toUpdateCost.do
 5)修改保存资费:/updateCost.do
 6)删除资费:/deleteCost.do
 7)打开登录页:/toLogin.do
 8)打开主页:/toIndex.do
 9)登录:/login.do
 
4.JSP命名规范
 1)查询资费: /WEB-INF/cost/find.jsp
 2)增加资费:/WEB-INF/cost/add.jsp
 3)修改资费:/WEB-INF/cost/update.jsp
 4)登录页:/WEB-INF/main/login.jsp
 5)主页:/WEB-INF/main/index.jsp

项目登记部分 

jstl:import标签 <c:import>说明  (include)

案例演示:

工程案例目录结构

 

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启动案例工程,

浏览器录入http://localhost:8080/netctoss/findCost.do 即可:如果没有错误,最终页面显示效果应如下图:

录入账户与密码进行登记:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值