3

I've some question about jsp,

I've some method let say the name of the method is getStaffDetail and were written in staffDetail.jsp file below code in staffDetail.jsp

<%!
    public StaffDetails getStaffDetail(int staffId) throws SQLException{
        //Request to db
        //process the result set
        //return StaffDetail
    }
%>

and this method were use in personPage.jsp file by calling it using include directive

personPage.jsp

<%@ include file = "/path/staffDetail.jsp">

<%
    StaffDetails sd = getStaffDetail(1234);
    String stafName = sd.getStfName();
    String stfAddress = sd.getStfAddress();
    //and the rest...
%>

Or should I write this code in java class for example StaffDetail.class

package packageName

import DBAccess;

public class StaffDetail{   

    //Request to db
    //process the result set
    //Setter n getter in this class 
}

And in the personPage.jsp

<@ page import = "package.StaffDetail">

<%
    StaffDetail sdInClass = new StaffDetail();//
    sdInClass.getStfName();
    sdInClass.getStfAddress();
%>

The code is quite same the only difference is instead of putting the method in the jsp file, I put in the class

I would like to ask which once better in performance.

p/s I know that we as a java web programmer are discourage to use scriplet in jsp file, but for some reason I can't convert all the scriplet to EL. The least I can do is convert the method into class file. Sorry I'm new in java programming

Zahary
  • 329
  • 8
  • 23
  • You have to include the whole JSP file (its contents) just to use the method. That alone should discourage you to even think about it even if it had (though it doesn't) any marginal performance benefits. – Ravi K Thapliyal May 26 '13 at 18:30
  • A JSP file *is* a Java file, in the end, so there can't be a performance difference. – user207421 May 26 '13 at 21:41
  • As already mentioned: [The use of scriptlets in JSP is highly discouraged](http://stackoverflow.com/a/3180202/814702) – informatik01 May 26 '13 at 22:00

4 Answers4

3

Well, there is no performance issue of writing scriptlets in jsp or importing java class in jsp!

In both the cases, your jsp will be automatically converted to a servlet (by jsp container) and then the servlet will eventually be compiled to byte code! And this will happen once while deploying the application and after that there is no jsp, no scriptlet, no el expression, only the compiled byte code will run in your JVM.

Scpritlets are discouraged for other reasons, mostly related to readability and maintainability of your application. There is nothing about the performance!

From my experience, I can tell you, mostly debugging is too much difficult in scriptlet jsp pages! If your application freaks out in scriptlet, you are dead! Most of the time, will see a blank page with no error message or no sufficient clue to drill through!!!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

The answer is in the question. You shouldn't have any Java code in JSPs. The corollary is that the less Java code you have in JSPs, the better it is.

This has nothing to do with performance though. A method is a method, and JSPs are compiled to classes that are just like your own classes, and run by the same JVM. The important things here are clean design, maintainability, separation of responsibilities, ease to document, reuse and debug code.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

When you write java in a jsp page, the JVM will compile the code into a servlet than into a byte code at run-time, then process the request, when it comes to writing code in a java class, the class is compiled into .class (byte) when it's deployed on the web server. The JVM then will process the class at run-time. When it comes to the JVM the processing of the ClassName.class is much faster than the .jsp. But it's not a performance issue since it's negligible. Plus its better to write all your code in the Java class since it's more secure when it comes to XSS or cross-site scripting.

mkazma
  • 572
  • 3
  • 11
  • 29
0

If you want measure how long some code takes to execute (in nanoseconds):

<% 
    long startTime = System.nanoTime(); 
%>

<%
    // ... the code being measured ...
%>

<%  
    long estimatedTime = System.nanoTime() - startTime;
    // out estimatedTime
%>

You may find some differences statistically, but are minimal to be considered. Regardless of the results, it is recommended have the Java code separately.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148