⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainservlet.java

📁 JAVA Servlet2.3外文书籍源码
💻 JAVA
字号:
package store;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

// JAXP packages
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;

public class MainServlet extends HttpServlet {


  public void init() throws ServletException {

    //  Load the products from XML file provided by init parameter
    ServletContext context = getServletContext();
    InputStream productsFile = context.getResourceAsStream((String) context.getInitParameter("productsFile"));


    DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance();

 	DocumentBuilder db = null;

    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException pce) {
    	throw new ServletException (pce.getMessage());
    }

    Document doc = null;
    try {
        doc = db.parse(productsFile);
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } catch (SAXException se) {
        throw new ServletException(se.getMessage());
	}

    NodeList productsList = doc.getElementsByTagName("product");


    HashMap products = new HashMap();
    Node product;
    for (int ctr = 0; ctr < productsList.getLength(); ctr ++ ) {

		product = productsList.item(ctr);
		NamedNodeMap attribs = product.getAttributes();
		Node attrib = attribs.getNamedItem("name");
		String name = attrib.getNodeValue();

		attrib = attribs.getNamedItem("price");
		String price = attrib.getNodeValue();

	    Product p = new Product(ctr,name,price);

		products.put(new Integer(ctr),p);
	}


    //  Store products in the ServletContext
    context.setAttribute("products",products);


  }

  public void doPost(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {

    doGetOrPost(req,res);

  }
  public void doGet(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {

    doGetOrPost(req,res);

  }

  private void doGetOrPost(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {

    PrintWriter out = res.getWriter();

    // Include standard header
    RequestDispatcher dispatcher = req.getRequestDispatcher("/header.html");
    dispatcher.include(req,res);

    HashMap products = (HashMap) getServletContext().getAttribute("products");

    //  List the products, clickable to add to cart
    Iterator it = products.values().iterator();
    out.println("<table>");
    while (it.hasNext()) {
      out.println("<tr>");
      Product product = (Product) it.next();
      out.println("<td><a href='Cart?add=true&id=" + product.getId() +"'>" + product.getName() + "</a></td><td>" + product.getPrice() +"</td>");
      out.println("</tr>");
    }
    out.println("</table>");

    // Include standard footer
    dispatcher = req.getRequestDispatcher("/footer.html");
    dispatcher.include(req,res);
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -