about using scriplets in JSP

This question already has an answer here:

  • How to avoid Java code in JSP files? 28 answers

  • This is a bad idea, but it's not because only one person can use the site at a time. It's a bad idea because the code will become hard to maintain. Your database connectivity code should be separate from your view logic. Perhaps put it in a servlet (ideally it'd be abstracted away in some persistence layer). The servlet will get the data from the database and make it available for the jsp to render.


    The advice you got on scriptlets causing single threading for your site is only true if the code you put in the scriptlet causes that to happen, JSTL, Struts, etc. all generate java code that looks just like the code your JSP with scriptlets generates.

    That being said, using scriptlets is considered poor practice because it leads to convoluted code. Try using JSTL use beans and put your java logic in them instead. Your JSP will look much nicer and be easier to maintain.


    Just like servlet , JSPs are also translated into servlet and then compiled, so whatever you declare in JSP Declaration block ie <%! ... %> <%! ... %> will be directly declared in the translated Servlet. And As for as servlet are concerned there is only once instance of servlet throughout the server life cycle and only service method is called by newly created thread from that instance only. So if simultaneous request occurs they may alter same data member and may cause unexpected result

    链接地址: http://www.djcxy.com/p/76600.html

    上一篇: 如何添加一个servlet来替换JSP中的scriptlet?

    下一篇: 关于在JSP中使用脚本