Wednesday, January 24, 2007

How can you retrieve data from the ResultSet?

JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.

ResultSet rs = stmt.executeQuery(”SELECT COF_NAME, PRICE FROM COFFEES”);
String s = rs.getString(”COF_NAME”);

SOURCE : www.referjava.com

How can you load the drivers?

Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:

Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);

Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ, you would load the driver with the following line of code:

Class.forName(”jdbc.DriverXYZ”);


SOURCE : www.referjava.com

What is Action Servlet?

The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.

SOURCE : www.referjava.com

code of any Action Class?

Here is the code of Action Class that returns the ActionForward object.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
return mapping.findForward("testAction");
}
}



SOURCE : www.referjava.com

What is ActionForm?

An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.


SOURCE : www.referjava.com

What is Struts Validator Framework?

Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class. The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. Now the Validator framework is a part of Jakarta Commons project and it can be used with or without Struts. The Validator framework comes integrated with the Struts Framework and can be used without doing any extra settings.

SOURCE : www.referjava.com

What is Jakarta Struts Framework?

Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.

SOURCE : www.referjava.com

how can we communicate from struts to ejb?

strut frame work have Action class. This Action class contain execute() method.this method interact with model......your model code maybe in EJB/JDBC/Hibernate.......u can call that methods in execute().

with in the execute() u can call business delegate---> service loactor--->jndi--->to SessionFacade.


In Struts architecture Action class contain execute() method, within execute() method you can access database through jdbc or EJB. You can call the reference of entity bean class by jndi call and easily fetch data from database.


SOURCE : www.referjava.com

Is struts thread safe? Give an example?

Struts is not only thread-safe but thread-dependant. The response to a request is handled by a light-weight Action object, rather than an individual servlet. Struts instantiates each Action class once, and allows other requests to be threaded through the original object. This core strategy conserves resources and provides the best possible throughput. A properly-designed application will exploit this further by routing related operations through a single Action.

SOURCE : www.referjava.com

difference between perform() and execute() method ?

Perform method is the method which was deprecated in the Struts Version 1.1. In Struts 1.x, Action.perform() is the method called by the ActionServlet. This is typically where your business logic resides, or at least the flow control to your JavaBeans and EJBs that handle your business logic. As we already mentioned, to support declarative exception handling, the method signature changed in perform. Now execute just throws Exception. Action.perform() is now deprecated; however, the Struts v1.1 ActionServlet is smart enough to know whether or not it should call perform or execute in the Action, depending on which one is available.

SOURCE : www.referjava.com

What are the various Struts tag libraries?

Struts is very rich framework and it provides very good and user friendly way to develop web application forms. Struts provide many tag libraries to ease the development of web applications. These tag libraries are:

* Bean tag library - Tags for accessing JavaBeans and their properties.
* HTML tag library - Tags to output standard HTML, including forms, text boxes, checkboxes, radio buttons etc..
* Logic tag library - Tags for generating conditional output, iteration capabilities and flow management
* Tiles or Template tag library - For the application using tiles
* Nested tag library - For using the nested beans in the application.

SOURCE : www.referjava.com

How Struts relates to J2EE?

Struts framework is built on J2EE technologies (JSP, Servlet, Taglibs), but it is itself not part of the J2EE standard.

SOURCE : www.referjava.com

difference between and

: This tag is used to output locale-specific text (from the properties files) from a MessageResources bundle.

: This tag is used to output property values from a bean. is a commonly used tag which enables the programmers to easily present the data.

SOURCE : www.referjava.com

What are the components of Struts?

Struts is based on the MVC design pattern. Struts components can be categories into Model, View and Controller.
Model: Components like business logic / business processes and data are the part of Model.
View: JSP, HTML etc. are part of View
Controller: Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.


SOURCE : www.referjava.com

How you will handle exceptions in Struts?

In Struts you can handle the exceptions in two ways:
a) Declarative Exception Handling: You can either define global exception handling tags in your struts-config.xml or define the exception handling tags within .. tag.
Example:

key="database.error.duplicate"

path="/UserExists.jsp"

type="mybank.account.DuplicateUserException"/>

b) Programmatic Exception Handling: Here you can use try{}catch{} block to handle the exception.


SOURCE : www.referjava.com

Perfect definition for Servlet

A Java application that runs in a Web server or application server and provides server-side processing such as accessing a database and e-commerce transactions. Widely used for Web processing, servlets are designed to handle HTTP requests (get, post, etc.) and are the standard Java replacement for a variety of other methods, including CGI scripts, Active Server Pages (ASPs) and proprietary C/C++ plug-ins for specific Web servers (ISAPI, NSAPI).

SOURCE : www.referjava.com

Lifecycle of servlet

Lifecycle of servlet

1. init()
2. service()
3. destroy().


SOURCE : www.referjava.com

Difference between ServletContext and PageContext?

ServletContext gives the info about the container while PageContext gives info about the requests.
ServletContext is generated one per application while PageContext is generated for every page in ur application.

SOURCE : www.referjava.com

Define JSP?

JSP - JAVA SERVER PAGES

A server side technology. Have dynamic scripting capabilities that works along with HTML (Hypertext Markup Language) .

SOURCE : www.referjava.com

Lifecycle of JSP

There are two life cycle phases in JSP:
1) Translation phase:
Here the jsp page is converted to a servlet page and compiled to .class file

2)Execution phase:
Compiled code is executed.

The methods used for the life cycle of JSP are:
1.jspInit()
2.jspService()
3.jspDestroy().

SOURCE : www.referjava.com

Why do I need JSP technology if I already have servlets

JSP pages are compiled into servlets, so theoretically you could write servlets to support your web-based applications. However, JSP technology was designed to simplify the process of creating pages by separating web presentation from web content. In many applications, the response sent to the client is a combination of template data and dynamically-generated data. In this situation, it is much easier to work with JSP pages than to do everything with servlets.

SOURCE : www.referjava.com

How is JSP technology different from other products?

JSP technology is the result of industry collaboration and is designed to be an open, industry-standard method supporting numerous servers, browsers and tools. JSP technology speeds development with reusable components and tags, instead of relying heavily on scripting within the page itself. All JSP implementations support a Java programming language-based scripting language, which provides inherent scalability and support for complex operations.

SOURCE : www.referjava.com

What is a JSP page?

A JSP page is a page created by the web developer that includes JSP technology-specific and custom tags, in combination with other static (HTML or XML) tags. A JSP page has the extension .jsp or .jspx; this signals to the web server that the JSP engine will process elements on this page. Using the web.xml deployment descriptor, additional extensions can be associated with the JSP engine.

SOURCE : www.referjava.com

How do JSP pages work?

A JSP engine interprets tags, and generates the content required - for example, by calling a bean, accessing a database with the JDBC API or including a file. It then sends the results back in the form of an HTML (or XML) page to the browser. The logic that generates the content is encapsulated in tags and beans processed on the server.

SOURCE : www.referjava.com

Does JSP technology require the use of other Java platform APIs?

JSP pages are typically compiled into Java platform servlet classes. As a result, JSP pages require a Java virtual machine that supports the Java platform servlet specification.

SOURCE : www.referjava.com

How is a JSP page invoked and compiled?

Pages built using JSP technology are typically implemented using a translation phase that is performed once, the first time the page is called. The page is compiled into a Java Servlet class and remains in server memory, so subsequent calls to the page have very fast response times.

SOURCE : www.referjava.com

Can I create XML pages using JSP technology?

Yes, the JSP specification does support creation of XML documents. For simple XML generation, the XML tags may be included as static template portions of the JSP page. Dynamic generation of XML tags occurs through bean components or custom tags that generate XML output.

SOURCE : www.referjava.com

How do I use JavaBeans components (beans) from a JSP page

The JSP specification includes standard tags for bean use and manipulation. The useBean tag creates an instance of a specific JavaBeans class. If the instance already exists, it is retrieved. Otherwise, it is created. The setProperty and getProperty tags let you manipulate properties of the given object.

SOURCE : www.referjava.com

How can I delete a cookie from within a JSP page?

A cookie, mycookie, can be deleted using the following scriptlet:

<%

//creating a cookie
Cookie mycookie = new Cookie("aName","aValue");
response.addCookie(mycookie);

//delete a cookie
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);

%>


SOURCE : www.referjava.com

How can I prevent the word "null"

You could make a simple wrapper function, like this

<%!


String blanknull(String s) {

return (s == null) ? "" : s;

}

%>

then use it inside your JSP form, like





SOURCE : www.referjava.com

can I declare methods within my JSP page?

You can declare methods for use within your JSP page as declarations. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions. Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare.


for example:

<%!
public String whereFrom(HttpServletRequest req) {
HttpSession ses = req.getSession();
...
return req.getRemoteHost();
}
%>
<%
out.print("Hi there, I see that you are coming in from ");
%>
<%= whereFrom(request) %>
Another Example
file1.jsp:
<%@page contentType="text/html"%>
<%!
public void test(JspWriter writer) throws IOException{
writer.println("Hello!");
}
%>
file2.jsp
<%@include file="file1.jsp"%>


<%test(out);% >




SOURCE : www.referjava.com

Keywords in Java

There are 50 keywords in Java. The keyword assert is added in version Java1.4 and enum in Java1.5.

SOURCE : www.referjava.com

Access Specifiers in Java ?

There are four access specifiers in java
1. public
2. private
3. protected
4. default

A class with public access can be seen by all classes from all packages.
A class with private access can be visible in the same class.
A class with protected access can be seen by classes in the same package and by a subclass, even if the subclass is in a different package.
A class with default access can be visible only by classes within the same package.

SOURCE : www.referjava.com

Difference between protected and default ?

A default member may be accessed only if the class belongs to the same package, whereas a protected member can be accessed by a subclass even if the subclass is in a different package.

SOURCE : www.referjava.com

List Primitive variables ?

1. byte
2. short
3. int
4. long
5. float
6. double
7. boolean
8. char.

SOURCE : www.referjava.com

Instance Variables

Instance variables are defined inside the class, but outside of any method. They are only initialized when the class is instantiated. Instance variables are the fields that belong to each unique object.


SOURCE : www.referjava.com