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

项目登记部分 

Web开发基础_Servlet学习_0025_项目练习(八)_第1张图片

jstl:import标签 说明  (include)

Web开发基础_Servlet学习_0025_项目练习(八)_第2张图片

案例演示:

工程案例目录结构

Web开发基础_Servlet学习_0025_项目练习(八)_第3张图片

 

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 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 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"%>


	
		
		案例-NetCTOSS
		
		
	
	
	  
	

 index.jsp

<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


	
		
		案例-NetCTOSS
		
		 
	
	
		
		

 

nav.jsp

<%@page pageEncoding="utf-8"%>

将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,

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

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

Web开发基础_Servlet学习_0025_项目练习(八)_第4张图片

Web开发基础_Servlet学习_0025_项目练习(八)_第5张图片

你可能感兴趣的:(Web开发,Servlet)