我真的应该关心 servlet Action 类中的线程安全问题吗

发布于 2024-12-16 16:23:02 字数 1699 浏览 0 评论 0原文

servlet 类处理传入的请求对象、获取数据并获取数据。存储到 StringBuilder/StringBuffer 并将数据传递给另一个类以写入文件。

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;
         }
    }

正如您在上面的示例中看到的,操作类将数据传递给 FileWrite 类,后者将数据写入新文件。有数百个客户端可能同时发送请求以将数据存储在新文件中。
所以我的问题是,在servlet类中我应该使用什么来存储数据。String还是stringBuffer还是StringBuilder?是线程安全的问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薄暮涼年 2024-12-23 16:23:02

是的,你应该关心线程安全问题,但是线程安全的问题并不在于你选择了 String 或 stringBuffer 或 StringBuilder。

您需要(可能)注意线程安全的地方是,如果您的 FileWriter 类收到对同一文件名的两个请求。

另外,我想指出,从安全角度来看,直接从 get 参数获取原始文件位置是非常危险的 - 因为用户可以覆盖其他用户文件,或者(可能,取决于权限)甚至操作系统文件。

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.

忘东忘西忘不掉你 2024-12-23 16:23:02

只要它发生在 servlet 的 doGet 或 doPost 内部,那么您就只处理一个线程。因此,不需要线程安全,这意味着您应该使用比 StringBuffer 更快的 StringBuilder。

As long as it is happening inside the doGet or doPost of your servlet then you are only dealing with one thread. Thus, no need for thread safety which means you should use StringBuilder which is faster than StringBuffer.

枉心 2024-12-23 16:23:02

对于每个新请求,都会生成一个新的 servlet 线程,该线程拥有自己正确包装的唯一上下文。因此 doPost doGet 中的数据会保存在该上下文中,因此不会受到其他请求的影响。

就String、StringBuffer和StringBuilder而言,由于上下文本身是线程安全的,因此不存在同步问题,因此您可以选择springbuilder。

附带说明:如果不会发生任何变化,为什么不选择简单的字符串呢?

顺便说一句..请注意针对同一文件的两个请求!

For every new request, a new servlet thread is spawned having its own properly wrapped unique context. So the data inside doPost doGet gets saved in that context and is thus safe from other requests.

As far as String vs StringBuffer vs StringBuilder is considered, since the context itself is thread safe, there are no synchronization issues and so you can go for springbuilder.

On a side note: if there are no changes going to happen, why not go for simple string ??

By the way.. look out for two requests coming for the same file!!

过气美图社 2024-12-23 16:23:02

来自 Java 文档。

StringBuilder 的实例对于多线程使用是不安全的。如果需要这样的同步,那么建议使用 StringBuffer。

http://download.oracle.com/javase /1.5.0/docs/api/java/lang/StringBuilder.html

首先,字符串是内部化的。

http://download.oracle.com /javase/1,5.0/docs/api/java/lang/String.html#intern()

这意味着如果您有相同的字符串,这不是问题。

如果您的页眉和页脚是静态的,则它可能位于静态变量中。

From Java doc.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html

First of all Strings are internalized.

http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#intern()

Which means if you have identical strings, it is not an issue.

If your header and footer are static, it could be in a static variable.

耶耶耶 2024-12-23 16:23:02

如果您在方法内声明变量,它将被放置在仅当前线程可以访问的调用线程堆栈的位置。因此,只要它是局部变量,您就不必担心线程安全
在你的情况下,你应该使用 StringBuider 因为它更快,无需担心线程安全

if you declare variable inside a method it will be put in a place call thread-stack that is accessible to only the current thread .so you dont have to worry about thread safety as long as it a local variable
in your case you should use StringBuider because it's faster no need to worry about thread safety here

執念 2024-12-23 16:23:02

你的编码器是线程安全的,这在多线程环境中没有问题。因为你的 ServleClass 没有类级别或实例级别变量,所以你的 FileWrite 在 post 方法中是实例。服务器请求将有一个新的 FileWrite..

your coder is Thread safe , this has not problem in Multi threaded environment .Because your ServleClass does not have Class level or Instance level variables, your FileWrite is Instance in the method of post . erver request will have a new FileWrite..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文