Created
October 21, 2011 10:43
-
-
Save lukaspili/1303545 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supinfo.supcommerce.servlet; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.supinfo.sun.supcommerce.bo.SupProduct; | |
import com.supinfo.sun.supcommerce.doa.SupProductDao; | |
public class InsertSomeProductServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
SupProduct product = new SupProduct(); | |
product.setName("foobar"); | |
product.setContent("hello world"); | |
product.setPrice(10.5F); | |
SupProductDao.addProduct(product); | |
resp.setContentType("text/html"); | |
resp.getWriter().println("Produit ajouté!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supinfo.supcommerce.servlet; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.List; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.supinfo.sun.supcommerce.bo.SupProduct; | |
import com.supinfo.sun.supcommerce.doa.SupProductDao; | |
public class ListProductServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
resp.setContentType("text/html"); | |
PrintWriter writer = resp.getWriter(); | |
writer.println("<html><body>"); | |
List<SupProduct> products = SupProductDao.getAllProducts(); | |
writer.println("<p>Il y'a " + products.size() + " produits dans la boutique.</p>"); | |
for(SupProduct product : products) { | |
writer.println("<p>"); | |
writer.println("<a href=\"showProduct?id=" + product.getId() + "\">"); | |
writer.println("Product name: " + product.getName() + "</a><br />"); | |
writer.println("Product content: " + product.getContent() + "<br />"); | |
writer.println("Product price: " + product.getPrice() + "<br />"); | |
writer.println("</p>"); | |
} | |
writer.println("</body></html>"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.supinfo.supcommerce.servlet; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.supinfo.sun.supcommerce.bo.SupProduct; | |
import com.supinfo.sun.supcommerce.doa.SupProductDao; | |
import com.supinfo.sun.supcommerce.exception.UnknownProductException; | |
public class ShowProductServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
String param = req.getParameter("id"); | |
// id n'existe pas | |
if (null == param) { | |
resp.sendError(404); | |
return; | |
} | |
Long id; | |
try { | |
id = Long.parseLong(param); | |
} catch (NumberFormatException e) { | |
// id n'est pas un chiffre | |
resp.sendError(404); | |
return; | |
} | |
SupProduct product; | |
try { | |
product = SupProductDao.findProductById(id); | |
} catch (UnknownProductException e) { | |
// produit pour l'id donné n'existe pas | |
resp.sendError(404); | |
return; | |
} | |
resp.setContentType("text/html"); | |
PrintWriter writer = resp.getWriter(); | |
writer.println("<p>"); | |
writer.println("Product name: " + product.getName() + "<br />"); | |
writer.println("Product content: " + product.getContent() + "<br />"); | |
writer.println("Product price: " + product.getPrice() + "<br />"); | |
writer.println("</p>"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment