java 简单购物车+登陆

==========================dl.html=====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
  </head>
  <body bgcolor="#FFFFFF">
      <center> 欢迎登陆系统</center></br></br>
      <center>
      <form name="login" method="post" action="main">
          <label>用户名:</label>
          <input type=text name="userID" value="">
          <label>密码:</label>
          <input type=password name="password" value="">
          <input type="submit" name="tj" value="提交"></input>
          <input type="reset" name="reset"></input>
      </form></center>
  </body>
</html>

 

=============================main.java====================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class main extends HttpServlet {
  
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
          String userID=request.getParameter("userID");
          if(userID==null)userID="";
          String password=request.getParameter("password");
          if(password==null)password="";
          if((userID.equals("guest")&&password.equals("123"))){

                        RequestDispatcher dispatcher=
                            request.getRequestDispatcher("LoginSucess");
                            dispatcher.forward(request,response);}
          else{
                        RequestDispatcher dispatcher=
                               request.getRequestDispatcher("LoginFail");
                               dispatcher.forward(request,response);
               }
       }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

 

=================================LoginSucess.java=========================


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class LoginSucess extends HttpServlet {
  
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


        response.setContentType("text/html;charset=GB2312");
        PrintWriter out = response.getWriter();
        String name=request.getParameter("userID");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>登陆成功</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>欢迎!"+name+  "您已成功登录系统。。。。</h1>");
              RequestDispatcher dispatcher=request.getRequestDispatcher("test1.html");
               dispatcher.include(request,response);
            out.println("</body>");
            out.println("</html>");

           out.close();
     
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

 

==================================LoginFail.java===========================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class LoginFail extends HttpServlet {
  
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=GB2312");
        PrintWriter out = response.getWriter();

            out.println("<html>");
            out.println("<head>");
            out.println("<title>登录失败</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>登录失败,请重新登录。。。</h1>");
                RequestDispatcher dispatcher=request.getRequestDispatcher("dl.html");
               dispatcher.include(request,response);
            out.println("</body>");
            out.println("</html>");
            
            out.close();
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

 

 

====================================CatalogServlet.java=============

import java.io.*;
import java.util.HashMap;
import javax.servlet.*;
import javax.servlet.http.*;


/**
 *
 * @author Administrator
 */
public class CatalogServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
 
        HttpSession session=req.getSession();
        int itemCount=0;
        HashMap cart=(HashMap)session.getAttribute("cart");
        if(cart!=null){
            itemCount=cart.size();
        }
        res.setContentType("text/html;charset=GB2312");
        PrintWriter out = res.getWriter();


            out.println("<html><head><title>简单购物车"+"Example </title></head>");


            out.println("<body><table border=/"0/"width =/"100%/"><tr>");
            out.println("<td align=/"left/" valign=/"botom/">");
            out.println("<h1>欢迎来到DOO水果商店挑选水果</h1>");
            out.println("<form action=/"");
            out.println(res.encodeURL("cart"));

            out.println("/"method=/"POST/">");
            out.println("<table cellspacing=/"5/" cellpadding=/"5/"><tr>");
            out.println("<td align=/"center/"><b>种类</b></td>");
            out.println("<td align=/"center/"><B>单价</b></td>");
            out.println("<td align=/"center/"><B>数量</b></td></tr><tr>");
            out.println("<td align=/"center/">"+"苹果"+"</td>");
            out.println("<td align=/"center/">"+"5.5"+"</td>");
            out.println("<td align=/"center/">");
            out.println("<input name=/"apple_amount/""+"></td></tr><tr>");
            out.println("<td align=/"center/">"+"香蕉"+"</td>");
            out.println("<td align=/"center/">"+"4.5"+"</td>");
            out.println("<td align=/"center/">");
            out.println("<input name=/"banana_amount/""+"></td></tr><tr>");
            out.println("<td align=/"center/">"+"葡萄"+"</td>");
            out.println("<td align=/"center/">"+"3.6"+"</td>");
            out.println("<td align=/"center/">");
            out.println("<input name=/"grape_amount/""+"></td></tr><tr>");
            out.println("<table><hr>");
            out.println("<input type=/"Submit/" name=/"btn_submit/" "+"value=/"放入购物车/">");
            out.println("</form></body>");
            out.println("</html>");

      
            out.close();


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        processRequest(req, res);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        processRequest(req, res);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}


==============================CartServlet.java=====================


import java.io.*;
import java.util.HashMap;
import javax.servlet.*;
import javax.servlet.http.*;


/**
 *
 * @author Administrator
 */
public class CartServlet extends HttpServlet {
  
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        HttpSession session=req.getSession(true);
        HashMap cart=(HashMap)session.getAttribute("cart");
        if(cart==null){
             cart=new HashMap();
             cart.put("apple","0");
             cart.put("banana","0");
             cart.put("grap","0");
             session.setAttribute("cart",cart);

        }
        res.setContentType("text/html;charset=GB2312");
        PrintWriter out = res.getWriter();
            req.setCharacterEncoding("GB2312");
            String apple_amount=req.getParameter("apple_amount");
            String banana_amount=req.getParameter("banana_amount");
            String grape_amount=req.getParameter("grape_amount");
            String appleAmount=(String)cart.get("apple");
            String bananaAmount=(String)cart.get("banana");
            String grapeAmount=(String)cart.get("grape");
            int new_apple_amount=Integer.parseInt(appleAmount,10)+Integer.parseInt(apple_amount,10);
            int new_banana_amount=Integer.parseInt(bananaAmount,10)+Integer.parseInt(banana_amount,10);
            int new_grape_amount=Integer.parseInt(grapeAmount,10)+Integer.parseInt(grape_amount,10);
            cart.put("apple",String.valueOf(new_apple_amount));
            cart.put("banana",String.valueOf(new_banana_amount));
            cart.put("grape",String.valueOf(new_grape_amount));
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>购物车内容/title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>你的购物车里有</h1>"+new_apple_amount+"斤苹果 "+new_banana_amount+"斤香蕉"+new_grape_amount+"斤葡萄");
            out.println("<hr>");
            out.println("<hr>

<a href=/"");
            out.println(res.encodeURL("catalog"));
            out.println("/">回到水果店</a>

");
            out.println("</body>");
            out.println("</html>");
          
            out.close();
  
    }

 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        processRequest(req, res);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        processRequest(req, res);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
   
}


/*1)在进行代码编写时,首先遇到的就是Servlet 中HTML 的繁琐
   2)在进行登录系统的编写时,原本希望连接数据库,在课本最后的练习中发现了有源码,但是又遇到了,EJB 编程还不了解。特别是JSP, JAVABEAN,EJB 的感念还有些模糊,没办法分清其感念。   现在有些了解,JSP 实现与用户的表象逻辑,JAVABEAN 实现GUI的 小控件,
EJB是业务逻辑层的实现
  3)发现WEB.XML是对整体的配置,包括Servlet   java程序  URL
  4)对CartServlet.java这段代码不能独立显示,一定要由CatalogServlet.java传递参数才能正常显示?
  5)发现NETBEAN  右下对各段程序有一个概括显示,对调试参数特别有效
  6)为了实现与数据库的链接,在网上查了很多,在服务器调试的窗口处添加MYSQL驱动,链接了数据库,但是在命令参数行,没有实现构建基      本表,对数据库链接池还没有搞懂,在登陆页面预存GUST 123 实现了用户的识别
  7)在登录失败页面中,添加  RequestDispatcher dispatcher=request.getRequestDispatcher("dl.html");
                  dispatcher.include(request,response)实现了插入页面*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值