首先小编总结一下javaweb学习的一些经验和一些开发的收获!

小编的学习资料来自尚硅谷的javaweb微博视频,资料来自其官方的的官网,小编就不发了,需要的友友可以去官方下载!下面是小编的一些经验分享,首先来说小编自学java差不多有5个月了,从javase,mysql,jdbc,到html,css,js(juery,ajax).javaweb基础(servlet+tomcat),然后是跟着视频做了书城项目,收获很多,也可以说是为以后开发积累基础,接下来小编将会进入的开发框架的学习同时也会继续写博客分享自己的收获!

进入到javaee的初级阶段对小编来说可谓是困难重重,在这段时间小编有时候会改好几天的bug,尽管是跟着视频做也可能会报错,因为一些版本不同的原因,但是友友们也不要害怕出错,慢慢改在网上找资源自己解决也是可以丰富自己的经验的,同时在看视频的时候一定要跟着做,写代码,小编在最开始的时候敲代码的速度很慢,但是到现在小编的基本代码速度有了很大的提高,甚至还可以不看键盘敲代码,这是基础打字速度的提高!

然后是代码思想的提升,跟着视频做项目,说实话对于小编来说这个项目是第一个算是比较完整的BS架构的项目,小编也学到了一些开发的知识和技巧的东西,也学会了众多jar的使用,但是它不会对整体的开发代码的水平提高多少,因为很多东西始终不是小编自己独立写的而是跟着视频做的,视频中的讲解是非常细致的,这让学习它的人有更好的立即,同时也是体会到了分层分模块写代码的好处,知道了接口,工具类,封装,继承,多态在一个项目中的实际使用,算的上是小编对开发的一次入门接触,小编在此次开发中学会了自己写封装使用工具类,以及javaweb的三层结构和之间的关系,小编也是自己把订单模块给自己完成了,理解了浏览器和服务器之间的交互过程,以及遇见了很多的网页错误提示和解决办法,熟悉了断点调试,充分的锻炼了小编的改bug的能力,以及对开发工具的使用,整体代码不算太多,项目也是比较low的但是对于初学者来说是很好的开发入门继承。所有若是有和小编一样的友友推荐跟着做一做,相信你也会收获很大!但是做完了也不要太深究代码,主要是学的其开发的思想,而不是其中的实际代码!

下面是书城项目的实际展示

      1.用户模块


Java设计论坛页面 javaweb论坛_验证码

 

Java设计论坛页面 javaweb论坛_验证码_02

  

package com.java.web;import com.google.gson.Gson;import com.java.polo.User;import com.java.service.impl.Userservice;import com.java.service.impl.Userserviceimpl;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class Userservlet extends Baseservlet {    private Userservice userservice=new Userserviceimpl();    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //处理登录业务        // 1、获取请求的参数        String username = req.getParameter("username");        String password = req.getParameter("password");        // 调用 userService.login()登录处理业务        User loginUser = userservice.login(new User(null, username, password, null));        // 如果等于 null,说明登录 失败!        if (loginUser == null) {            //把错误的信息存放到域之中            req.setAttribute("msg","用户名或密码错误!");            req.setAttribute("username",username);            // 跳回登录页面            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);        } else {            // 登录 成功            //保存用户登录的信息             req.getSession().setAttribute("user",loginUser);            //跳到成功页面 login_success.jsp            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);        }    }    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //处理注册业务        // 1、获取请求的参数        String username= req.getParameter("username");        String password=req.getParameter("password");        String email=req.getParameter("email");        String code=req.getParameter("code");//验证码        //获取session的验证码        String toke=(String) req.getSession().getAttribute("KAPTCHA_SESSION_KEY");        //马上删除        req.getSession().removeAttribute("KAPTCHA_SESSION_KEY");        // 2、检查 验证码是否正确 === 写死,要求验证码为:abcde        if( toke!=null&&toke.equalsIgnoreCase(code )){            // 3、检查 用户名是否可用            if(userservice.existsusername(username)){                //把错误的信息保存在域之中                req.setAttribute("msg","用户名以存在");                req.setAttribute("username",username);                req.setAttribute("email",email);                //不可用                System.out.println("用户名"+username+"已存在");                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);            }else{                // 可用                // 调用 Sservice 保存到数据库                userservice.registUser(new User(null,username,password,email));                // 跳到注册成功页面 regist_success.jsp                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);            }        }else{            //把错误的信息保存在域之中            req.setAttribute("msg","验证码错误");            req.setAttribute("username",username);            req.setAttribute("email",email);            // 跳回注册页面            System.out.println("验证码错误!验证码是:"+code);            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);        }    }    //注销    protected void Logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         //销毁Session        req.getSession().invalidate();        resp.sendRedirect(req.getContextPath());    }    //用户名验证    protected void Ajaxusernameexits(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        System.out.println("ajax请求开始");         //获取参数      String username= req.getParameter("username");      //查看是否可以使用       boolean falg= userservice.existsusername(username);       //返回得结果        Map<String,Object> result=new HashMap<>();        result.put("usernameextis",falg);        //封装成json对象        Gson gson=new Gson();        String json= gson.toJson(result);        resp.getWriter().write(json);    }}

2.后台模块

 

Java设计论坛页面 javaweb论坛_验证码_03

 

Java设计论坛页面 javaweb论坛_tomcat_04

package com.java.web;import com.java.polo.Page;import com.java.polo.impl.Book;import com.java.service.impl.BookServiceimpl;import com.java.service.impl.Bookservice;import com.java.util.Webutil;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.List;public class Bookservlet extends Baseservlet{    private Bookservice bookservice=new BookServiceimpl();    //添加    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取请求数据        Book book= Webutil.copyparamTobean(req.getParameterMap(),new Book());        //添加方法        bookservice.addBook(book);        int pageNo=Webutil.praseInt(req.getParameter("pageNo"),0);        pageNo+=1;        //返回查询页面//        req.getRequestDispatcher("").forward(req,resp);        resp.sendRedirect(req.getContextPath()+"/manager/bookservlet?action=page&pageNo="+pageNo);    }    //删除    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取请求数据       String id=req.getParameter("id");        //删除方法        int i= Integer.parseInt(id);        bookservice.deleteBookById(i);        //返回查询页面        resp.sendRedirect(req.getContextPath()+"/manager/bookservlet?action=page&pageNo="+req.getParameter("pageNo"));    }    //修改    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取请求数据        Book book= Webutil.copyparamTobean(req.getParameterMap(),new Book());        //修改方法        bookservice.upDateBook(book);        //返回查询页面        resp.sendRedirect(req.getContextPath()+"/manager/bookservlet?action=page&pageNo="+req.getParameter("pageNo"));    }    //列表    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         //查询全部数据        List<Book> books = bookservice.queryBooks();        //保存到域中        req.setAttribute("books",books);        //请求转发        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);    }    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {             //获取请求编号        String id= req.getParameter("id");               //查询          int i=Integer.parseInt(id);         Book book= bookservice.queryBookById(i);            //保存信息        req.setAttribute("book",book);             //转发        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);    }    //分页    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {             //获取请求参数       /* if(req.getParameter("pageNo")==null){            int pageNo=Integer.parseInt("1");            int pageSize=Integer.parseInt("4");            //调用分页方法            Page<Book> page=bookservice.page(pageNo,pageSize);            //保存page对象            req.setAttribute("page",page);            //请求转发            req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);        }else{            int pageNo=Integer.parseInt(req.getParameter("pageNo"));            int pageSize=Integer.parseInt(req.getParameter("pageSize"));            //调用分页方法            Page<Book> page=bookservice.page(pageNo,pageSize);            //保存page对象            req.setAttribute("page",page);            //请求转发            req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);        }*/        int pageNo=Webutil.praseInt(req.getParameter("pageNo"),1);        int pageSize=Webutil.praseInt(req.getParameter("pageSize"),4);        //调用分页方法        Page<Book> page=bookservice.page(pageNo,pageSize);        page.setUrl("manager/bookservlet?action=page");        //保存page对象        req.setAttribute("page",page);        //请求转发        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);    }}

 

package com.java.web;import com.java.polo.Cart;import com.java.polo.Oder;import com.java.polo.OderItem;import com.java.polo.User;import com.java.service.impl.Oderservice;import com.java.service.impl.Oderserviceimpl;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.List;public class Odersevlet extends Baseservlet{    private Oderservice oderservice=new Oderserviceimpl();    protected void saveOder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {           //获取购物车       Cart cart= (Cart) req.getSession().getAttribute("cart");            //获取用户id        User user=(User)  req.getSession().getAttribute("user");       if(user==null){           req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);           return;       }        Integer userid= user.getId();        //生成订单            String oderId=oderservice.createOder(cart,userid);            req.getSession().setAttribute("orderid",oderId);            //请求转发            resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");    }    protected void showmyoder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取购物车        Cart cart= (Cart) req.getSession().getAttribute("cart");        //获取用户id        User user=(User)  req.getSession().getAttribute("user");        if(user==null){            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);            return;        }        Integer userid= user.getId();        //获取订单信息        List<Oder>oder=oderservice.showalloders(userid);        req.getSession().setAttribute("oders",oder);        resp.sendRedirect(req.getContextPath()+"/pages/order/order.jsp");    }    //查到用户点击的订单    protected void reciworder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {           String orderid= req.getParameter("orderid");           Oder oder = oderservice.showoneoder(orderid);           req.getSession().setAttribute("oder",oder);        resp.sendRedirect(req.getContextPath()+"/pages/user/showorder.jsp");    }    protected void jieshouoder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          //获取订单号           String orderid= req.getParameter("orderid");            //调用方法             oderservice.chengoder(2,orderid);             Oder oder = oderservice.showoneoder(orderid);             //回到原来得页面        req.getSession().setAttribute("oder",oder);        resp.sendRedirect(req.getContextPath()+"/pages/user/showorder.jsp");    }    //manager模块    //查看所有得订单    protected void Managershowalloder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取所有得订单        List<Oder> oders=oderservice.showallOder();        //保存在域之中         req.setAttribute("allorder",oders);         req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);    }    //查看个别订单    protected void Managershowoneeoder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {       String oderid=req.getParameter("id");        //获取单个得订单项        OderItem oder=oderservice.showOderItem(oderid);        //获取单个得订单        Oder oder1=oderservice.showoneoder(oderid);        //保存在域之中        req.setAttribute("allorder",oder);        req.setAttribute("oder1",oder1);        req.getRequestDispatcher("/pages/manager/showalloder.jsp").forward(req,resp);    }    //发货    protected void sendthisoder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取订单号        String orderid= req.getParameter("id");        //调用方法        oderservice.chengoder(1,orderid);        Oder oder = oderservice.showoneoder(orderid);        //回到原来得页面        req.setAttribute("oder",oder);        req.getRequestDispatcher("/pages/manager/sendorderseccss.jsp").forward(req,resp);    }}

3.购物车模块

Java设计论坛页面 javaweb论坛_验证码_05

  

Java设计论坛页面 javaweb论坛_tomcat_06

 

Java设计论坛页面 javaweb论坛_tomcat_07

 

package com.java.web;import com.java.polo.Cart;import com.java.polo.CartItem;import com.java.polo.impl.Book;import com.java.service.impl.BookServiceimpl;import com.java.service.impl.Bookservice;import com.java.util.Webutil;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class Cartservlet extends Baseservlet{    private Bookservice bookservice=new BookServiceimpl();    protected void addItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {       //获取编号        int id= Webutil.praseInt(req.getParameter("id"),0);        //调用方法        Book book=bookservice.queryBookById(id);        CartItem cartItem=new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());      //将购物车放入Session中 如果有就直接使用 如果没有就创建购物车        Cart cart=(Cart)req.getSession().getAttribute("cart");       if(cart==null){           cart=new Cart();           req.getSession().setAttribute("cart",cart);       }        cart.addItem(cartItem);        req.getSession().setAttribute("lastname",cartItem.getName());        //获取原来的地址        String referer = req.getHeader("Referer");        //重定向到原来的商品页面        resp.sendRedirect(referer);    }    //删除商品    protected void deleteItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取编号        int id=Webutil.praseInt(req.getParameter("id"),0);        //获取购物车        Cart cart =(Cart)req.getSession().getAttribute("cart");        if(cart!=null){            cart.deleteItem(id);            //获取原来的地址            String referer = req.getHeader("Referer");            //重定向到原来的商品页面            resp.sendRedirect(referer);        }    }   //清空    protected void clearcart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取购物车        Cart cart =(Cart)req.getSession().getAttribute("cart");        if(cart!=null){           cart.Clear();            //获取原来的地址            String referer = req.getHeader("Referer");            //重定向到原来的商品页面            resp.sendRedirect(referer);        }    }    //修改商品数量    protected void updatecount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {         int id=Webutil.praseInt(req.getParameter("id"),0);         int count=Webutil.praseInt(req.getParameter("count"),0);        //获取购物车        Cart cart =(Cart)req.getSession().getAttribute("cart");        if(cart!=null){            cart.updateCount(id,count);            //获取原来的地址            String referer = req.getHeader("Referer");            //重定向到原来的商品页面            resp.sendRedirect(referer);        }    }}
package com.java.web;import com.java.polo.Page;import com.java.polo.impl.Book;import com.java.service.impl.BookServiceimpl;import com.java.service.impl.Bookservice;import com.java.util.Webutil;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class ClientBookServlet extends Baseservlet {    private Bookservice bookservice=new BookServiceimpl();    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取请求参数        int pageNo= Webutil.praseInt(req.getParameter("pageNo"),1);        int pageSize=Webutil.praseInt(req.getParameter("pageSize"),4);        //调用分页方法        Page<Book> page=bookservice.page(pageNo,pageSize);        //保存page对象        req.setAttribute("page",page);        page.setUrl("client/bookservlet?action=page");        //请求转发        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);    }    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取请求参数        int pageNo= Webutil.praseInt(req.getParameter("pageNo"),1);        int pageSize=Webutil.praseInt(req.getParameter("pageSize"),4);        int min=Webutil.praseInt(req.getParameter("min"),0);        int max=Webutil.praseInt(req.getParameter("max"),100000);        //调用分页方法        Page<Book> page=bookservice.pageByPrice(pageNo,pageSize,min,max);       //带上价格区间        StringBuilder sb= new StringBuilder("client/bookservlet?action=pageByPrice");        //如果有价格参数就追加在后面         if(req.getParameter("min")!=null){             sb.append("&min=").append(req.getParameter("min"));         }        if(req.getParameter("max")!=null){            sb.append("&max=").append(req.getParameter("max"));        }        //保存page对象        req.setAttribute("page",page);        page.setUrl(sb.toString());        //请求转发        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);    }}

整体实现了图书的增删改,用户的登录注册,图书的分页,管理员对订单的发送用户对图示的购买生成订单以及用户对订单的签收