Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cServ; import clients.CuentasAdmin_Service; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.WebServiceRef; import cAddOns.Validator; import java.util.Iterator; import java.util.List; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * * @author Seven */ @WebServlet(name = "AltaDoc", urlPatterns = { "/AltaDoc" }) public class AltaDoc extends HttpServlet { @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/proyectoiii/DocsAdmin.wsdl") private adminDocs.DocsAdmin_Service service_3; @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/proyectoiii/CuentasAdmin.wsdl") private CuentasAdmin_Service service; /** * 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=UTF-8"); try (PrintWriter out = response.getWriter()) { System.out.println("hit altadoc"); //begin database operation String nomArch = ""; String nombre = ""; String contra = ""; int id = 0; System.out.println(request.getParameter("idprofe")); String displayString = "Contrasea incorrecta"; String divColor = "danger"; String rdrUrl = "<meta http-equiv=\"refresh\" content=\"2;url=pages/profesor/adminDocs.jsp\" >"; //start file up FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List items = null; try { items = upload.parseRequest(request); } catch (Exception e) { e.printStackTrace(); } for (Iterator it = items.iterator(); it.hasNext();) { DiskFileItem diskFileItem = (DiskFileItem) it.next(); if (diskFileItem.isFormField()) { String fieldname = diskFileItem.getFieldName(); String fieldvalue = diskFileItem.getString(); System.out.println("fn: " + fieldname + " fv " + fieldvalue); if (fieldname.equals("nombre")) { nombre = fieldvalue; } else if (fieldname.equals("contra")) { contra = fieldvalue; } else if (fieldname.equals("idprofe")) { id = Integer.parseInt(fieldvalue); } } else { //start getpath String relativeWebPath = "/../../web/pages/profesor/adminDocsPanels/docDump/"; System.out.println("relative thing " + getServletContext().getRealPath(relativeWebPath)); System.out.println(relativeWebPath); String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); System.out.println("complete path " + absoluteDiskPath + "\\" + nomArch); absoluteDiskPath += "\\" + nomArch; //end getpath byte[] fileBytes = diskFileItem.get(); nomArch = diskFileItem.getName(); File file = new File(absoluteDiskPath + diskFileItem.getName()); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(fileBytes); fileOutputStream.flush(); } } //end file up //start db insertion Validator valid = new Validator(); valid.ValidatePDF(nomArch); if (valid.isValid() == true) { String adString = altaDoc(id, nomArch); if (adString.equals("operacion realizada")) { displayString = "Documento dado de alta"; divColor = "success"; rdrUrl = "<meta http-equiv=\"refresh\" content=\"2;url=pages/profesor/adminDocs.jsp\" >"; } } else { displayString = "Archivo no vlido"; divColor = "danger"; rdrUrl = "<meta http-equiv=\"refresh\" content=\"2;url=pages/profesor/adminDocs.jsp\" >"; } //end db insertion out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<link rel=\"stylesheet\" href=\"css/style.css\">"); out.println("<title>Servlet AltaGrupo</title>"); out.println("</head>"); out.println("<body>"); out.println("<div class=\"container\">\n" + " <div class=\"row\">\n" + " <br><br>\n" + " <div class=\"panel panel-" + divColor + "\">\n" + " <div class=\"panel-heading\">\n" + " <h3 class=\"panel-title\">Espera</h3>\n" + " </div>\n" + " <div class=\"panel-body\">\n" + " " + displayString + " \n" + " </div>\n" + " </div>\n" + " </div>\n" + " </div>"); out.println(rdrUrl); out.println("</body>"); out.println("</html>"); //end database operation } catch (Exception ex) { ex.printStackTrace(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * 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> private String login(java.lang.String nombre, java.lang.String contra) { // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe. // If the calling of port operations may lead to race condition some synchronization is required. clients.CuentasAdmin port = service.getCuentasAdminPort(); return port.login(nombre, contra); } private String altaDoc(int idprofe, java.lang.String nomArch) { // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe. // If the calling of port operations may lead to race condition some synchronization is required. adminDocs.DocsAdmin port = service_3.getDocsAdminPort(); return port.altaDoc(idprofe, nomArch); } }