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.siphon.d2js.DispatchServlet.java

protected void doPost(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();
    String method = StringUtils.defaultIfEmpty(request.getParameter("_m"), "create");
    try {/* w w w .j a v  a2  s.  co m*/
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), method);
    } finally {
        asyncContext.complete();
    }
}

From source file:ru.org.linux.auth.SecurityFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    HttpServletRequest request = (HttpServletRequest) req;
    request.setAttribute("configuration", ctx.getBean(Configuration.class));
    request.setAttribute("template", new Template(ctx));
    request.setCharacterEncoding("utf-8"); // ?? tomcat
    CSRFManipulation(request, (HttpServletResponse) res);
    forWikiManipulation(request, (HttpServletResponse) res);
    chain.doFilter(req, res);//from  w  ww  .  j a v a  2 s . com
}

From source file:ua.aits.sdolyna.controller.MainController.java

@RequestMapping(value = { "/sendmail/", "/sendmail" }, method = RequestMethod.GET)
public @ResponseBody String sendMail(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String text = request.getParameter("text");
    final String username = "office@crc.org.ua";
    final String password = "crossroad2000";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication(username, password);
        }//from   w  w  w.ja va2s .  c  om
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("sonyachna-dolyna@ukr.net"));
        message.setSubject("E-mail ? ?:");
        message.setText("?: " + name + "\nEmail: " + email + "\n\n" + text);
        Transport.send(message);
        return "done";
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

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

protected void doGet(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();
    String method = StringUtils.defaultIfEmpty(request.getParameter("_m"), "fetch");

    try {/*  w w  w  . ja  v a  2s . c  o m*/
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), method);
    } finally {
        asyncContext.complete();
    }
}

From source file:com.happyuno.controller.EventController.java

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 *//*from   www.  j  a  v  a2s.  co  m*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
    response.setHeader("Cache-Control", "no-cache");

    int playerId = Integer.parseInt(request.getParameter("playerId").trim());
    int gameId = Integer.parseInt(request.getParameter("tableId").trim());

    ServletContext application = request.getSession().getServletContext();
    HashMap<Integer, GameTable> gameTableMap = (HashMap<Integer, GameTable>) application
            .getAttribute("gameTableMap");
    //?
    Game game = gameTableMap.get(gameId).getCurrentGame();

    ArrayList<JSONObject> eventsAL = game.getEventCenter().getEvents(game.getPlayerFromId(playerId));

    JSONArray events = new JSONArray();
    for (int i = 0; i < eventsAL.size(); ++i) {
        events.put(eventsAL.get(i));
    }

    response.getWriter().write(events.toString());
}

From source file:com.liusoft.dlog4j.servlet.DLOG_ActionServlet.java

/**
 * ???//from w w w  . jav a 2  s  .  com
 * @param req
 * @param res
 * @throws ServletException
 * @throws IOException
 * @see org.apache.struts.action.ActionServlet#process(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    HttpServletRequest request;
    if (RequestUtils.isMultipart(req)) {
        //???
        request = req;
        request.setCharacterEncoding(encoding);
    } else {
        //??
        String enc = req.getCharacterEncoding();
        if (req instanceof RequestProxy)
            request = req;
        else if (encoding.equalsIgnoreCase(enc))
            request = req;
        else
            request = new RequestProxy(req, encoding);
    }
    super.process(request, res);
}

From source file:org.seasar.cadhelin.ControllerServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding(encoding);
    RedirectSession.move(request.getSession());
    try {// w  w  w .ja v a  2 s .c  om
        request = createHttpRequest(request);
        ActionMetadata metadata = actionMetadataFactory.getActionMetadata(request);
        if (metadata != null) {
            InternalControllerContext controllerContext = new ControllerContextImpl(container,
                    actionMetadataFactory, request, response, viewUrlPattern, metadata.getControllerName(),
                    metadata.getName());
            ControllerContext.setContext(controllerContext);
            request.setAttribute(CONTROLLER_CONTEXT_NAME, controllerContext);
            try {
                FilterContextImpl filter = new FilterContextImpl(filters, controllerContext, metadata);
                filter.doFilter(request, response);
            } catch (Throwable e) {
                if (exceptionHandlerMetadata != null) {
                    exceptionHandlerMetadata.service(e, controllerContext, request, response);
                }
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
        }
    } catch (FileUploadException e) {
        LOG.error("", e);
        throw new RuntimeException(e);
    } finally {
        if (request instanceof MultipartRequestWrapper) {
            MultipartRequestWrapper wrapper = (MultipartRequestWrapper) request;
            wrapper.closeFileItems();
        }
    }
}

From source file:com.shyshlav.functions.filework.download_image.java

public String download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    request.setCharacterEncoding("UTF-8"); //  
    response.setCharacterEncoding("UTF-8");
    filePath = request.getSession().getServletContext().getInitParameter("avathars");
    System.out.println(filePath);
    isMultipart = ServletFileUpload.isMultipartContent(request);
    System.out.println(isMultipart);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    if (!isMultipart) {
        return "  ";
    }//from w w w . j av a2  s .  co  m
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // maximum size that will be stored in memory
    factory.setSizeThreshold(maxMemSize);
    // Location to save data that is larger than maxMemSize.
    factory.setRepository(new File("c:\test"));
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding("UTF-8");
    // maximum file size to be uploaded.
    upload.setSizeMax(maxFileSize);
    try {
        // Parse the request to get file items.
        List<FileItem> fileItems = upload.parseRequest(request);
        // Process the uploaded file items
        Iterator i = fileItems.iterator();
        //String name,String password,String email,String surname,String link_to_image,String about_me,String id
        String name = null;
        String password = null;
        String re_password = null;
        String surname = null;
        String about_me = null;
        String id = null;
        String link_to_server = null;
        String email = null;
        while (i.hasNext()) {
            FileItem fi = (FileItem) i.next();
            if (fi.isFormField()) {
                String fieldname = fi.getFieldName();
                String fieldvalue = fi.getString();
                if (fieldname.equals("name")) {
                    name = fi.getString("UTF-8");
                } else if (fieldname.equals("surname")) {
                    surname = fi.getString("UTF-8");
                } else if (fieldname.equals("password")) {
                    password = fi.getString("UTF-8");
                } else if (fieldname.equals("re_password")) {
                    re_password = fi.getString("UTF-8");
                } else if (fieldname.equals("about_me")) {
                    about_me = fi.getString("UTF-8");
                } else if (fieldname.equals("id")) {
                    id = fi.getString("UTF-8");
                } else if (fieldname.equals("email")) {
                    email = fi.getString("UTF-8");
                }
                System.out.println(fieldname + fieldvalue);
                if (fieldname == null || fieldvalue == null) {
                    return "? ? ? ";
                }
            }
            if (!fi.isFormField()) {
                if (!password.equals(re_password)) {
                    System.out.println(password + " - " + re_password);
                    return "  ?";
                }
                // Get the uploaded file parameters
                String fileName = email + ".png";
                link_to_server = "/musicbox/avathars/" + email + ".png".trim();
                // Write the file
                if (fileName.lastIndexOf("\\") >= 0) {
                    file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\")));
                } else {
                    file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1));
                }
                fi.write(file);
                System.out.println("Uploaded Filename: " + filePath + fileName);
            }
        }
        System.out.println(link_to_server);
        updateUser um = new updateUser();
        um.updateUser(name, password, surname, link_to_server, about_me, id);
    } catch (Exception ex) {
        System.out.println(ex);
        return "    1 ";
    }
    return "ok";
}

From source file:com.openkm.extension.servlet.StaplingDownloadServlet.java

/**
 * //  w w w .jav a  2  s. c  o  m
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
    int sgId = WebUtils.getInt(request, "sgId");
    File tmpZip = File.createTempFile("okm", ".zip");

    try {
        String archive = "stapling.zip";
        response.setHeader("Content-disposition", "attachment; filename=\"" + archive + "\"");
        response.setContentType("application/zip");
        OutputStream out = response.getOutputStream();
        exportZip(sgId, out);
        out.flush();
        out.close();
    } catch (RepositoryException e) {
        log.warn(e.getMessage(), e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "RepositoryException: " + e.getMessage());
    } catch (Exception e) {
        log.warn(e.getMessage(), e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        FileUtils.deleteQuietly(tmpZip);
    }
}

From source file:com.twinsoft.convertigo.engine.util.HttpServletRequestTwsWrapper.java

public HttpServletRequestTwsWrapper(HttpServletRequest request) {
    super(request);
    try {//  w  w w .  ja  v a2 s .  co  m
        if (request.getCharacterEncoding() == null) {
            request.setCharacterEncoding("UTF-8");
        }

        if (request.getMethod().equalsIgnoreCase("PUT")
                && MimeType.WwwForm.is(HeaderName.ContentType.getHeader(request))) {
            try {
                String content = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
                addQuery(content);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // parse GET parameters
        addQuery(request.getQueryString());

        // retrieve POST parameters ( == not defined in GET )
        for (Entry<String, String[]> entry : GenericUtils.<Map<String, String[]>>cast(request.getParameterMap())
                .entrySet()) {
            if (!parameters.containsKey(entry.getKey())) {
                parameters.put(entry.getKey(), entry.getValue());
            }
        }
    } catch (UnsupportedEncodingException e) {
        parameters.clear();
        parameters.putAll(GenericUtils.<Map<String, String[]>>cast(request.getParameterMap()));
    }
}