How does JSP process concurrent requests?

南笙酒味 提交于 2019-12-12 01:53:40

问题


I guess my question is, a JSP is compiled into a single servlet instance that serve multiple requests. How do I make it threadsafe?


回答1:


Servlets are meant to be immutable. Either no state exists outside of method calls (the servlet is stateless), or any such state will never change (so the state that each thread sees is always the same).

It's extremely simple to write a threadsafe servlet: never use instance variables. Use method-local variables.




回答2:


Just don't assign request/session specific data as global/static variables. So as long as you don't use scriptlet declarations <%! %> which you assign with request/session specific data and you don't put request/session data in application scope (i.e. as attribute of ServletContext), then you're safe.

See also:

  • How to avoid Java code in JSP?
  • Servlet instantiation, session variables and threadsafety



回答3:


Try this: <%@ page isThreadSafe="true" %>




回答4:


JSPs are compiled into servlets. All JSP variables are method local (stack) variables hence they are thread safe.

If you directly add a thread-unsafe attribute to a servlet class, it will cease to be thread safe.



来源:https://stackoverflow.com/questions/5397285/how-does-jsp-process-concurrent-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!