List of usage examples for javax.servlet.http HttpServletRequest getPart
public Part getPart(String name) throws IOException, ServletException;
From source file:com.izv.controlador.Controlador.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w. j a va 2 s. c om*/ * * @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 destino = "index.html"; boolean forward = false; String target, op, action, view; target = request.getParameter("target"); op = request.getParameter("op"); action = request.getParameter("action"); if (target.equals("inmobiliaria") && op.equals("select") && action.equals("view")) { forward = true; destino = "WEB-INF/inmobiliaria/index.jsp"; request.setAttribute("datos", ModeloInmueble.get()); } else { if (target.equals("inmobiliaria") && op.equals("delete") && action.equals("op")) { forward = false; ModeloInmueble.delete(request.getParameter("id")); destino = "control?target=inmobiliaria&op=select&action=view"; } else { if (target.equals("inmobiliaria") && op.equals("insert") && action.equals("view")) { forward = true; destino = "WEB-INF/inmobiliaria/insert.jsp"; } else { if (target.equals("inmobiliaria") && op.equals("insert") && action.equals("op")) { forward = false; destino = "control?target=inmobiliaria&op=select&action=view"; format = new SimpleDateFormat(); format.applyPattern("yyyy-dd-MM"); Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); format.format(date); Inmueble inmueble = new Inmueble(); inmueble.setLocalidad(request.getParameter("localidad")); inmueble.setDireccion(request.getParameter("direccion")); inmueble.setTipo(request.getParameter("tipo")); inmueble.setUsuario(request.getParameter("usuario")); inmueble.setPrecio(request.getParameter("precio")); inmueble.setFechaalta(date); ModeloInmueble.insert(inmueble); } else { if (target.equals("inmobiliaria") && op.equals("edit") && action.equals("view")) { forward = true; request.setAttribute("inmueble", ModeloInmueble.get(request.getParameter("id"))); destino = "WEB-INF/inmobiliaria/edit.jsp"; } else { if (target.equals("inmobiliaria") && op.equals("edit") && action.equals("op")) { forward = false; destino = "control?target=inmobiliaria&op=select&action=view"; Inmueble inmueble = new Inmueble(); inmueble.setLocalidad(request.getParameter("localidad")); inmueble.setDireccion(request.getParameter("direccion")); inmueble.setTipo(request.getParameter("tipo")); inmueble.setUsuario(request.getParameter("usuario")); inmueble.setId(Integer.parseInt(request.getParameter("id"))); inmueble.setPrecio(request.getParameter("precio")); String fecha = request.getParameter("fecha"); format = new SimpleDateFormat(); format.applyPattern("yyyy-dd-MM"); Date date = null; try { date = format.parse(fecha); } catch (ParseException ex) { Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex); } inmueble.setFechaalta(date); //String id=request.getParameter("id"); ModeloInmueble.update(inmueble); } else { if (target.equals("inmobiliaria") && op.equals("imagen") && action.equals("view")) { forward = true; request.setAttribute("inmueble", ModeloInmueble.get(request.getParameter("id"))); destino = "WEB-INF/inmobiliaria/addImagen.jsp"; } else { if (target.equals("inmobiliaria") && op.equals("imagen") && action.equals("op")) { forward = false; boolean error = false; String id = request.getParameter("id"); response.setContentType("text/html;charset=UTF-8"); Calendar calendario = new GregorianCalendar(); Date date = calendario.getTime(); SimpleDateFormat formatoFecha = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String fecha = formatoFecha.format(date); String titulo = "inmueble_" + id + "_" + fecha; Part archivoPost = request.getPart("archivo"); String carpeta = getServletContext().getRealPath("/") + "fotos/"; InputStream input = archivoPost.getInputStream(); try { OutputStream out = new FileOutputStream(carpeta + titulo + ".jpg"); byte[] b = new byte[2048]; int length; while ((length = input.read(b)) != -1) { out.write(b, 0, length); } } catch (Exception e) { error = true; } finally { input.close(); } Fotos fot = new Fotos(); fot.setIdinmueble(Integer.valueOf(id)); fot.setNombre(titulo + ".jpg"); ModeloFoto.insert(fot); destino = "control?target=inmobiliaria&op=select&action=view"; } else { if (target.equals("inmobiliaria") && op.equals("imagenes") && action.equals("view")) { forward = true; request.setAttribute("inmueble", ModeloInmueble.get(request.getParameter("id"))); destino = "WEB-INF/inmobiliaria/imageview.jsp"; } else { if (target.equals("inmobiliaria") && op.equals("deletefoto") && action.equals("op")) { forward = false; ModeloFoto.delete(request.getParameter("id")); destino = "control?target=inmobiliaria&op=select&action=view"; } } } } } } } } } } if (forward) { request.getRequestDispatcher(destino).forward(request, response); } else { response.sendRedirect(destino); } }
From source file:controllerClasses.StudyController.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// w ww . j a v a 2s. co m * * @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 { System.out.println("In StudyController"); HttpSession session = null; //String action = request.getParameter("action"); String userPath = request.getServletPath().substring(1, request.getServletPath().length()); RequestDispatcher dispatcher = null; String reDirect = null; String studyCode = null; String choice = null; StudyDB studyDB = null; List<Study> studies = null; User user = null; try { HttpSession session2 = request.getSession(); String email = (String) session2.getAttribute("email"); session = request.getSession(false); if (session != null && session.getAttribute("theUser") != null) { user = (User) session.getAttribute("theUser"); studyDB = new StudyDB(); if (userPath.equals("participate")) { studyCode = request.getParameter("studyCode"); String email1 = (String) session2.getAttribute("email"); System.out.println("user email in study controller from session" + email1); if (studyCode != null && !studyCode.isEmpty()) { System.out.println("studyCode in first block: " + studyCode); Study study = studyDB.getStudy(studyCode); request.setAttribute("study", study); reDirect = "question"; } else { List result = StudyDB.retrieveStudies1(email); Study study = new Study(); String s = study.getName(); System.out.println("Controller study object" + s); request.setAttribute("study", study); request.setAttribute("result", result); reDirect = "participate"; } } else if (userPath.equals("answer")) { studyCode = request.getParameter("studyCode"); choice = request.getParameter("choice"); System.out.println("studyCode: " + studyCode); System.out.println("choice: " + choice); List result = StudyDB.answer(email, choice, studyCode); int i = UserDB.updateParticipations(email); user.setParticipation(i); int j = UserDB.updateCoins(email); user.setCoins(j); int k = UserDB.updateParticipants(email, studyCode); user.setParticipants(k); int participants = UserDB.getAttribute(email); int coins = UserDB.coins(email); int participation = UserDB.participation(email); user.setParticipants(participants); user.setCoins(coins); user.setParticipation(participation); request.setAttribute("user", user); /** List result = studyDB.getStudies(); List<Study> sInStart = new ArrayList<Study>(); for(Study study: studies){ if(study.getStatus().equalsIgnoreCase("start")){ sInStart.add(study); } } System.out.println(sInStart.size());**/ session.setAttribute("theUser", user); request.setAttribute("openStudies", result); request.setAttribute("result", result); reDirect = "participate"; } else if (userPath.equals("myStudies")) { List result = studyDB.retrieveStudies(email); Study study = new Study(); request.setAttribute("study", study); request.setAttribute("result", result); session.setAttribute("result", result); session.setAttribute("myStudies", studies); reDirect = "studies"; } else if (userPath.equals("edit")) { studyCode = request.getParameter("studyCode"); String email3 = (String) session2.getAttribute("email"); Study study = new Study(); study = StudyDB.getStudy(studyCode); /** if(session.getAttribute("myStudies")!=null){ studies = (List<Study>) session.getAttribute("myStudies"); }else{ studies = studyDB.getStudies(user.getEmail()); } for(Study study: studies){ if(study.getCode() == Integer.parseInt(studyCode)){ request.setAttribute("study", studyDB.getStudy(Integer.parseInt(studyCode))); break; } }**/ request.setAttribute("study", study); reDirect = "editstudy"; /*Pending*/ } else if (userPath.equals("update")) { studyCode = request.getParameter("studyCode"); Study study = new Study(); study.setCode(Integer.parseInt(request.getParameter("studyCode"))); study.setName(request.getParameter("studyName")); study.setQuestion(request.getParameter("questionText")); study.setRequestedParticipants(Integer.parseInt(request.getParameter("participants"))); study.setDescription(request.getParameter("description")); Part part = request.getPart("imageFile"); String fileName = part.getSubmittedFileName(); String file1 = "images/" + fileName; study.setImageURL(file1); System.out.println("IMAGE NAME" + file1); System.out.println("IMAGE NAME FROM STUDY OBJECT" + study.getImageURL()); List result = StudyDB.updateStudy(study, email); /** if(session.getAttribute("myStudies")!=null){ studies = (List<Study>) session.getAttribute("myStudies"); }else{ studies = studyDB.getStudies(user.getEmail()); } for(Study study: studies){ if(study.getCode() == Integer.parseInt(studyCode)){ studies.remove(study); study.setCode(Integer.parseInt(request.getParameter("studyCode"))); study.setName(request.getParameter("studyName")); study.setQuestion(request.getParameter("questionText")); study.setRequestedParticipants(Integer.parseInt(request.getParameter("participants"))); study.setDescription(request.getParameter("description")); studies.add(study); break; } }**/ request.setAttribute("result", result); request.setAttribute("study", study); session.setAttribute("myStudies", studies); reDirect = "studies"; } else if (userPath.equals("add")) { Study study = new Study(); study.setCode((int) Math.random()); study.setName(request.getParameter("studyName")); study.setQuestion(request.getParameter("questionText")); study.setRequestedParticipants(Integer.parseInt(request.getParameter("participants"))); study.setDescription(request.getParameter("description")); Part part = request.getPart("imageFile"); String fileName = part.getSubmittedFileName(); String file1 = "images/" + fileName; study.setImageURL(file1); /** studies = (List<Study>) session.getAttribute("myStudies"); studies.add(study);**/ List result1 = StudyDB.addStudy(study, email); List result = StudyDB.retrieveStudies(email); Study study1 = new Study(); request.setAttribute("result", result); session.setAttribute("myStudies", studies); reDirect = "studies"; } else if (userPath.equals("start")) { studyCode = request.getParameter("studyCode"); List result = StudyDB.startStudy(studyCode, email); Study study = new Study(); session.setAttribute("result", result); study.setStatus("started"); session.setAttribute("choice", "started"); /** if(session.getAttribute("myStudies")!=null){ studies = (List<Study>) session.getAttribute("myStudies"); } for(Study study: studies){ if(study.getCode() == Integer.parseInt(studyCode)){ studies.remove(study); study.setStatus("start"); studies.add(study); System.out.println("studyCode: " + studyCode); System.out.println("New Status: " + study.getStatus()); break; } }**/ session.setAttribute("myStudies", studies); reDirect = "studies"; } else if (userPath.equals("stop")) { studyCode = request.getParameter("studyCode"); System.out.println("study code from DB" + studyCode); List result = StudyDB.stopStudy(studyCode, email); Study study = new Study(); study.setStatus("stopped"); session.setAttribute("result", result); System.out.println("studyCode: " + studyCode); /** if(session.getAttribute("myStudies")!=null){ studies = (List<Study>) session.getAttribute("myStudies"); } for(Study study1: studies){ if(study1.getCode() == Integer.parseInt(studyCode)){ studies.remove(study); study1.setStatus(null); studies.add(study); System.out.println("studyCode: " + studyCode); System.out.println("New Status: " + study.getStatus()); break; } }**/ session.setAttribute("myStudies", studies); reDirect = "studies"; } dispatcher = getServletContext().getRequestDispatcher("/" + reDirect + ".jsp"); dispatcher.forward(request, response); } else { dispatcher = getServletContext().getRequestDispatcher("/" + "login" + ".jsp"); dispatcher.forward(request, response); } } catch (Exception e) { e.printStackTrace(); dispatcher = getServletContext().getRequestDispatcher("/main.jsp"); dispatcher.forward(request, response); } }