Package fr.iut.montreuil.servlet

Source Code of fr.iut.montreuil.servlet.WelcomeHttpServlet

package fr.iut.montreuil.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.iut.montreuil.dao.MediaDao;
import fr.iut.montreuil.model.Media;

public class WelcomeHttpServlet extends HttpServlet {

  private static final long serialVersionUID = -7911420701920105657L;

  private MediaDao dao;
 
  @Override
  public void init() throws ServletException {
   
    dao  = new MediaDao();
    dao.add(new Media("Fight Club"));
    dao.add(new Media("Blade Runner"));
    dao.add(new Media("Dark City"));
   
    super.init();
  }
 
 
  public void doGet(HttpServletRequest  req, HttpServletResponse res)
      throws ServletException, IOException {
   
    String name = req.getParameter("name");

    name = getCookieValue(req.getCookies(),"name",name);
   
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>JMedia</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");
    out.println("<H1>Bienvenue "+ (name != null ? name : "") + " dans JMedia</H1>");
    out.println("<ul>");
    for (Media media : dao.findAll()) {
      out.println("<li>"+media.getName()+"</li>");     
    }
    out.println("</ul>");
    out.println("</BODY>");
    out.println("</HTML>");
    out.close();
  }

  public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
    for (int i = 0; i < cookies.length; i++) {
      Cookie cookie = cookies[i];
      if (cookieName.equals(cookie.getName()))
        return (cookie.getValue());
    }
    return (defaultValue);
  }
}
TOP

Related Classes of fr.iut.montreuil.servlet.WelcomeHttpServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.