Example usage for javax.servlet.http HttpServletRequest setCharacterEncoding

List of usage examples for javax.servlet.http HttpServletRequest setCharacterEncoding

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest setCharacterEncoding.

Prototype

public void setCharacterEncoding(String env) throws UnsupportedEncodingException;

Source Link

Document

Overrides the name of the character encoding used in the body of this request.

Usage

From source file:org.mhi.servlets.ImageUpload.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");

    // Service Class for DB - Actions
    ServiceQuery query = new ServiceQuery();
    ServiceUpdate update = new ServiceUpdate();

    String imgName = request.getParameter("name");
    String imgDesc = request.getParameter("desc");
    String imgCat = request.getParameter("category");
    String fileSize = null;//  w  w  w  . j  a va  2 s  . co  m
    String fileName = null;
    String contentType = null;
    String[] parts = null;

    InputStream inputStream = null; // input stream of the upload file

    // obtains the upload file part in this multipart request
    Part filePart = request.getPart("files");

    if (filePart != null) {
        // Fill up Information extract from File
        fileSize = "" + filePart.getSize();
        parts = filePart.getSubmittedFileName().split("\\.");
        contentType = filePart.getContentType();

        // obtains input stream of the upload file
        inputStream = filePart.getInputStream();

        // new Image
        Images img = new Images();
        // Set Parameters
        img.setFileName(imgName + "." + parts[1]);
        img.setName(imgName);
        img.setDescription(imgDesc);
        img.setFileBlob(IOUtils.toByteArray(inputStream));
        img.setFileSize(fileSize);
        img.setcTyp(contentType);
        // Relationship to Category
        ImgCat cat = query.getCategoryByID(Long.valueOf(imgCat));
        img.setCategory(cat);
        // Persist to Database
        update.insertImage(img);
    }
    response.sendRedirect(request.getServletContext().getContextPath() + "/admin/upload");

}

From source file:org.jbpm.designer.web.server.FileStoreServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    String fname = req.getParameter("fname");
    String fext = req.getParameter("fext");
    String data = req.getParameter("data");
    String dataEncoded = req.getParameter("data_encoded");

    String storeInRepo = req.getParameter("storeinrepo");
    String profileName = req.getParameter("profile") != null ? req.getParameter("profile") : "jbpm";
    String uuid = Utils.getUUID(req);
    String processid = req.getParameter("processid");

    IDiagramProfile profile = _profileService.findProfile(req, profileName);
    Repository repository = profile.getRepository();

    String retData;/*  w w  w.  j a v  a2s.c o m*/
    if (dataEncoded != null && dataEncoded.length() > 0) {
        retData = new String(Base64.decodeBase64(dataEncoded));
    } else {
        retData = data;
    }

    if (fext != null && (fext.equals("bpmn2") || fext.equals("svg"))) {
        try {
            if (fext.equals("bpmn2")) {
                resp.setContentType("application/xml; charset=UTF-8");
            } else if (fext.equals("svg")) {
                resp.setContentType("image/svg+xml; charset=UTF-8");
            }

            if (processid != null) {
                resp.setHeader("Content-Disposition",
                        "attachment; filename=\"" + processid + "." + fext + "\"");
            } else if (uuid != null) {
                resp.setHeader("Content-Disposition", "attachment; filename=\"" + uuid + "." + fext + "\"");
            } else {
                resp.setHeader("Content-Disposition", "attachment; filename=\"" + fname + "." + fext + "\"");
            }
            resp.getWriter().write(retData);
        } catch (Exception e) {
            resp.sendError(500, e.getMessage());
        }

        if (storeInRepo != null && storeInRepo.equals("true")) {
            storeInRepository(uuid, retData, fext, processid, repository);
        }
    }
}

From source file:ua.aits.oblenergo_site.controller.AjaxAndFormController.java

@RequestMapping(value = "/system/users/insertdata.do", method = RequestMethod.POST)
public ModelAndView doAddUser(HttpServletRequest request) throws SQLException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, UnsupportedEncodingException {
    request.setCharacterEncoding("UTF-8");
    String user_name = request.getParameter("user_name");
    String user_password = request.getParameter("user_password");
    String user_role = request.getParameter("user_role");
    String user_enabled = request.getParameter("user_enabled");
    String user_sections = request.getParameter("user_sections");
    Users.addUser(user_name, user_password, user_role, user_enabled, user_sections);
    return new ModelAndView("redirect:" + "/system/users");
}

From source file:es.uma.inftel.blog.servlet.PerfilServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w  ww . ja v a 2  s  .  c o  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 {

    request.setCharacterEncoding("UTF-8");

    BaseView baseView = new BaseView();
    BaseViewFacade<BaseView> baseViewFacade = new BaseViewFacade<>(postFacade);
    baseViewFacade.initView(baseView);

    RequestDispatcher requestDispatcher = request.getRequestDispatcher("perfil.jsp");
    String password = request.getParameter("password");
    if (password == null) {
        request.setAttribute("perfilView", baseView);
        requestDispatcher.forward(request, response);
    }

    String email = request.getParameter("email");

    Part filePart = request.getPart("avatar");
    byte[] avatar = null;
    if (!filePart.getSubmittedFileName().isEmpty()) {
        InputStream inputStream = filePart.getInputStream();
        avatar = IOUtils.toByteArray(inputStream);
    }

    HttpSession session = request.getSession();
    Usuario usuario = (Usuario) session.getAttribute("usuario");

    modificarUsuario(usuario, password, email, avatar);
    response.sendRedirect("perfil");

}

From source file:ua.aits.oblenergo_site.controller.AjaxAndFormController.java

@RequestMapping(value = "/system/users/updatedata.do", method = RequestMethod.POST)
public ModelAndView doEditUser(HttpServletRequest request) throws SQLException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, UnsupportedEncodingException {
    request.setCharacterEncoding("UTF-8");
    String user_id = request.getParameter("user_id");
    String user_name = request.getParameter("user_name");
    String user_password = request.getParameter("user_password");
    String user_role = request.getParameter("user_role");
    String user_enabled = request.getParameter("user_enabled");
    String user_sections = request.getParameter("user_sections");
    Users.editUser(user_id, user_name, user_password, user_role, user_enabled, user_sections);
    return new ModelAndView("redirect:" + "/system/users");
}

From source file:es.uma.inftel.blog.servlet.RegistroServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request/*w  ww.j  a va  2  s.c o  m*/
 * @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 {

    request.setCharacterEncoding("UTF-8");

    String username = request.getParameter("username");
    String password = request.getParameter("password");

    RegistroViewFacade registroViewFacade = new RegistroViewFacade(postFacade);

    RequestDispatcher registroRequestDispatcher = request.getRequestDispatcher("registro.jsp");
    if (username == null && password == null) {
        request.setAttribute("registroView", registroViewFacade.createView(false));
        registroRequestDispatcher.forward(request, response);
        return;
    }

    String email = request.getParameter("email");

    Part filePart = request.getPart("avatar");
    InputStream inputStream = filePart.getInputStream();
    byte[] avatar = IOUtils.toByteArray(inputStream);

    Usuario usuario = registrarUsuario(username, password, email, avatar);
    if (usuario == null) {
        request.setAttribute("registroView", registroViewFacade.createView(true));
        registroRequestDispatcher.forward(request, response);
    } else {
        HttpSession session = request.getSession();
        session.setAttribute("usuario", usuario);
        response.sendRedirect("index");
    }
}

From source file:org.linagora.linshare.view.tapestry.services.impl.MyMultipartDecoderImpl.java

public HttpServletRequest decode(HttpServletRequest request) {

    try {/*from w  w w . j  a v a  2 s .  c  o  m*/
        request.setCharacterEncoding(requestEncoding);
    } catch (UnsupportedEncodingException ex) {
        logger.error("error while uploading the files", ex);
        throw new RuntimeException(ex);
    }
    /*
            if   (request.getContentLength() > maxRequestSize) {
    uploadException = new FileUploadException("Upload to big"); 
    return request;
                       
            }
      */

    List<FileItem> fileItems = parseRequest(request);

    return processFileItems(request, fileItems);
}

From source file:br.edu.ifpb.sislivros.actions.CadastrarLivro.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    Livro livro = null;// ww w.jav a  2 s  . co  m
    try {
        livro = montarLivro(request);
    } catch (FileUploadException ex) {
        Logger.getLogger(CadastrarLivro.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (livro != null) {
        CadastrarLivroBo cadastarBo = new CadastrarLivroBo();

        try {
            boolean cadastrou = cadastarBo.cadastrar(livro);
            if (cadastrou) {
                response.sendRedirect(request.getContextPath() + "/administrativo.jsp");
            }
        } catch (LivroExistenteException ex) {
            Logger.getLogger(CadastrarLivro.class.getName()).log(Level.SEVERE, null, ex);
            response.sendRedirect("errorPageISBN.html");
        }

    } else {
        response.sendRedirect(request.getContextPath() + "/home");
    }
}

From source file:org.siphon.d2js.DispatchServlet.java

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    try {// w  ww.  j a  va 2s . c  o m
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), "delete");
    } finally {
        asyncContext.complete();
    }
}

From source file:org.siphon.d2js.DispatchServlet.java

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    try {/*from w w w  .  j  a  v  a  2  s  . c  o  m*/
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), "modify");
    } finally {
        asyncContext.complete();
    }
}