Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:29+00:00 2026-05-26T20:47:29+00:00

A servlet class handles the incoming request object, fetch data & store into StringBuilder/StringBuffer

  • 0

A servlet class handles the incoming request object, fetch data & store into StringBuilder/StringBuffer and passes the data to another class to write into a file.

ActionClass
    public void doPost(HttpServletRequest request,HttpServletResponse response)
   throws ServletException,IOException {
        String fileName = request.getparameter("fileName");
        String body = request.getParameter("innerHTML");
        String head = request.getParameter("headContents");

        StringBuilder sbr = new StringBuilder();
        sbr.append(body);  sbr.append(head);
                        OR
        StringBuffer sbf = new StringBuffer();
         sbf.append(body);  sbf.append(head);

   FileWrite fw = new FileWrite(fileName, sbf/sbr); /* write the data into file*/
          }

FileWrite

    class FileWrite{
       public FileWrite(String fileName, StringBuilder sbf){
        boolean isExist = checkFileName(fileName);  /* return true or false */
         if(isExist){
           String name =  reName(fileName);  /* rename & return new name */
                 /* write the file in new file */
          }else{  /* write in same file name */  }
      }

   public String reName(String oldName){
             /* rename oldName as newName & checks via isExist(newName) */
            } 

   public boolean isExist(String filename) {
         // checks the file in directory, if found already
       return true;
           else return false;
         }
    }

as you can see in above example, action class passes the data to FileWrite class which writes the data to a new file. there are hundreds of client might sent the request at same time to store the data in new file.
so my question is, in servlet class what should i use to store the data. is it String or stringBuffer or StringBuilder?? is it issue of thread safe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:47:30+00:00Added an answer on May 26, 2026 at 8:47 pm

    Yes, you should care about thread safety issues, but the issue with thread safety isn’t in your choice of String or stringBuffer or StringBuilder.

    Where you need to watch out (potentially) for thread safety is if you get two requests for the same filename to your FileWriter class.

    Also, I would note that it is quite dangerous from a security standpoint to simply take raw file locations directly from a get parameter – as users can overwrite other user files, or (potentially, depending on permissions) even OS files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a servlet class which handles login. At the end of my login
web.xml fragment <!-- Handles all requests into the application --> <servlet> <servlet-name>Spring MVC Dispatcher
I wrote some code to read a file in my Java Servlet class. (I'm
<servlet> <servlet-name>myservlet</servlet-name> <servlet-class>workflow.WDispatcher</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>*NEXTEVENT*</url-pattern> </servlet-mapping> Above is the snippet from
I'm working on an application. I have a servlet (writeDataBase.class) that writes some information
To what is the class path of a Servlet container set? As per my
I'm having an issue using the Cookie class of the Servlet API 2.5 on
If my Servlet class uses a singleton (such as a manager class), where should
I have a servlet filter that handles errors for both vanilla servlets and JSF
My servlet application includes a number of library .jars, some of which contain embedded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.