Separate HTML code from the Java code: Combining HTML and Java code in the same source code can
make the code less readable. Mixing HTML and scriptlet will make the code extremely difficult to read and maintain. The display or behavior logic can be implemented as a custom tags by the Java developers and Web designers can use these tags as the ordinary XHTML tags. Refer Q36 in Enterprise section.
Place data access logic in JavaBeans: The code within the JavaBean is readily accessible to other JSPs
and Servlets.
Factor shared behavior out of Custom Tags into common JavaBeans classes: The custom tags are not
used outside JSPs. To avoid duplication of behavior or business logic, move the logic into JavaBeans and get the custom tags to utilize the beans.
Choose the right “include” mechanism: What are the differences between static and a dynamic include?
Using includes will improve code reuse and maintenance through modular design. Which one to use? Refer
Q31 in Enterprise section.
Use style sheets (e.g. css), template mechanism (e.g. struts tiles etc) and appropriate comments (both
hidden and output comments).
Q. Why use style sheets? The traditional HTML approach was to "hardcode" all of the appearance
information about a page. Say you want all your headings in Arial, and you have hard coded that in more than 50 pages? That is a lot of editing, and a lot of re-editing if you decide to modify the headings to courier. With all of that editing there are plenty of possibility for introducing errors. With CSS, you can decide how headings should appear, and enter that information once. Every heading in every page that is linked to this style sheet now has that appearance. Example:
h1
{
font-family : arial; font-weight : normal; }
Use pagination for large resultsets: If you display long lists (i.e. resultsets) in the browser, it is difficult for the user to find what he or she wants and also can prove impractical due to memory limitation, response-time limitation, page design limitation (i.e long scrollable pages are not desirable) etc. Pagination is the most common way to break up large amount of data into manageable chunks.
Q. How do you paginate your results?
1. Results can be read at once from the database and cached in middle-tier (e.g. HTTP session or home
grown cache) for fast access in subsequent pages. This approach is memory intensive and suitable only for small-to-medium sized recurring queries.
2. Results are fetched from the database on demand as the user pages. This divide and conquer approach
is suitable for medium-to-large resultsets where it delivers pages on demand, direct from the database. Limiting the size of the resultsets is SQL specific. For example in MySQL/Oracle you could limit your resultsets as follows:
//can be user selected values or constant values
String strPageNum = request.getParameter(“pageNum”); int pageNum = 0;
if(strPageNum != null){
pageNum = new Integer(strPageNum).intValue(); }
int maxRowsPerPage = new Integer(request.getParameter(“rowsPerPage”)).intValue();
//calculate
int rowEnd = pageNum * maxRowsPerPage;
int rowStart = (rowEnd - maxRowsPerPage) + 1;
“SELECT * FROM Products p where p.category=’Consumables’ LIMIT ” + rowStart + “,” + rowEnd
In Oracle:
“SELECT p.*, rownum as rowcount FROM Products p where p.category=’Consumables’ order by p.productNo where rowcount >= “ + rowStart + “ and rowcount < ” + rowEnd ” ;
Q 40:
How will you avoid scriptlet code in JSP? BP FAQA 40:
Use JavaBeans or custom tags instead.Q. If you have to develop a web site, which has say more than 200 static & dynamic pages, how would you make sure that in future if there is any requirement for a layout change, you have to change the layout in one page not 200 or more pages?
You could use the JSP include directives for page reuse but better approach to reduce redundant code is to use frameworks like Tiles for page composition using template mechanism or SiteMesh for page decoration. SiteMesh can be used by any Java Web framework since it is a Servlet filter. SiteMesh uses the decorator design pattern.
Q. How do you connect to the database from JSP/Servlet?
A. A connection can be established to a database as shown below via scriptlet. It is not the best practice to embed data
access logic in your JSP/Servlet and is shown only for illustration purpose and to create a lead up to the next section. The best practice should make use of a separate “Data Access Object (using DAO pattern)” , which gets invoked by JSP, Servlet, plain Java class, EJBs etc. The next section discusses basics and best practices relating to data access.
<%@ page language="java" contentTpe="text/html" import="java.sql.*"%> <html> <title>Simple JSP Page</title> <h1>Output to Browser</h1> <body> <%
//1. load the driver from specific vendor
Class.forName("oracle.jdbc.driver.OracleDriver");
//2. open connection to the databse by passing the URL to the database
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1526:myDB");
//3. create a statement object
Statement stmt = con.createStatement();
//4. Create a ResultSet
ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
//5. you can use the ResultSet Object to read data while(rs.next()){ rs.getString("firstname"); } %> </body> </html>
General Tip #4:
Every body is nervous for interviews and being a little nervous is natural. But if you are too nervous then you can overcome this by preparing for your interviews and by treating each interview as a free technical/behavioral training course. Have an attitude that even if you are not going to get the job, you are going to learn something good out of it. If you go with this attitude you will put yourself in a win/win situation and you might really get the offer. If you take this attitude you can learn a lot from your interviews. Also never think that you have to answer all the questions correctly. Do not get put off by a tricky or a difficult question. What really earns you a job is the combination of your knowledge +
Enterprise – JDBC & JTA