-1

I am new to JSP. I created a web application using JSP.

I written the java code inside the JSP scriptlets(<%.....%>).
For database connection also I fallowed the same manner.
Example:

   html code;
  <%
    database connectivity code;

  %>

     some jquery code;

 <%

    again database connectivity code;
 %>

But some people told to me that it has a big disadvantage that is "By using scriptlet declarations that only one person can use your site at a time without conflict"
Am I doing right? Building web application in this manner is right way?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 3
    No, this is a terrible way to write web applications. Go review our various wiki sites, starting with [`jsp`](http://stackoverflow.com/tags/jsp/info). – Sotirios Delimanolis Oct 04 '13 at 18:38
  • http://stackoverflow.com/questions/5818101/why-business-logic-should-be-moved-out-of-jsp – jmj Oct 04 '13 at 18:42
  • forget the JSP, start using with Servlet, JSP is usually used for simple constant pages. –  Oct 04 '13 at 18:42
  • Its certainly NOT that only 1 person will be able to use your site at a time. The biggest problem is whatever errors your code encounters might be shown to the user. If you put the database connectivity string in the JSP and the db becomes inaccessible, the error thrown might show the user your db password for example. If you do continue to use scriptlets, at least create a function in a class to make the database connection and handle the possible errors, and then use that class in the scriptlet. – developerwjk Oct 04 '13 at 18:56

3 Answers3

1

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.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

Just like servlet , JSPs are also translated into servlet and then compiled, so whatever you declare in JSP Declaration block i.e. <%! ... %> 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

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Vijay
  • 1,024
  • 6
  • 18
  • *whatever you declare in JSP Declaration block will treated as properties of that translated Servlet* this is **false**, the variables declared in scriptlets will be treated as local variables in `_jspService` method. – Luiggi Mendoza Oct 04 '13 at 19:13
  • I have mentioned declaration block which means <!% %> not scriplet block <% %> – Vijay Oct 04 '13 at 19:17
  • I can't see any part in your answer where you **explicitly** showed usage of `<%! ... %>`. – Luiggi Mendoza Oct 04 '13 at 19:24
  • JSP Declaration block will treated as properties of that translated Servlet . JSP declaration block is declared as <!% %> – Vijay Oct 04 '13 at 19:27
  • By they way, it is `<%! ... %>` not `<!% ... %>`. Refer to http://docs.oracle.com/javaee/5/tutorial/doc/bnaos.html#bnaot – Luiggi Mendoza Oct 04 '13 at 19:28
  • yes same as you mentioned i used to do it long time ago so small mistakes happens. apologies for that – Vijay Oct 04 '13 at 19:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38639/discussion-between-vijay-and-luiggi-mendoza) – Vijay Oct 04 '13 at 19:31
0

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.