Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Thursday, August 28, 2008

DOJO - Ajax Technology

Dojo is an Open Source DHTML toolkit written in JavaScript. It builds on several contributed code bases (nWidgets, Burstlib, f(m)), which is why we refer to it sometimes as a "unified" toolkit. Dojo aims to solve some long-standing historical problems with DHTML which prevented mass adoption of dynamic web application development.

Dojo allows you to easily build dynamic capabilities into web pages and any other environment that supports JavaScript sanely. You can use the components that Dojo provides to make your web sites more usable, responsive, and functional. With Dojo you can build degradable user interfaces more easily, prototype interactive widgets quickly, and animate transitions. You can use the lower-level APIs and compatibility layers from Dojo to write portable JavaScript and simplify complex scripts. Dojo's event system, I/O APIs, and generic language enhancement form the basis of a powerful programming environment. You can use the Dojo build tools to write command-line unit-tests for your JavaScript code. The Dojo build process helps you optimize your JavaScript for deployment by grouping sets of files together and reuse those groups through "profiles".

Dojo does all of these things by layering capabilities onto a very small core which provides the package system and little else. When you write scripts with Dojo, you can include as little or as much of the available APIs as you need to suit your needs. Dojo provides multiple points of entry, interpreter independence, forward looking APIs, and focuses on reducing barriers to adoption.

Download Dojo Tool Kit 1.1.1

Integrate With Code :

1. First add the Dojo script file before body tag
script type="text/javascript" src="js/dojo1.0/dojo/dojo.js"
djConfig="parseOnLoad:true, isDebug:true"


Dojo has a mechanism for setting various configuration options at runtime. The two most common are parseOnLoad, which toggles page-load parsing of widgets and in-markup code, and isDebug, which enables or disables certain debugging messages.

We can set these configuration in another way also.

script type="text/javascript"
var djConfig = {
isDebug:true, parseOnLoad:true
};
/script
script type="text/javascript" src="js/dojo1.0/dojo/dojo.js"

Tuesday, July 8, 2008

JSP Content Type - MIME Type

Some JSP pages are designed so they can deliver content using different content types (and character sets) depending on request time input. These pages may be organized as custom actions or scriptlets that determine the response content type and provide glue into other code actually generating the content of the response.

The initial content type for the response (including the character set) is determined as shown in the "Output Content Type" column in Table JSP.3-1. In all cases, the container must call response.setContentType() with the initial content type before processing the page.

The content type (and character set) can then be changed dynamically by calling setContentType() or setLocale() on the response object. The most recent call takes precedence. Changing the content type can be done up until the point where the response is committed. Data is sent to the response stream on buffer flushes for buffered pages, or on encountering the first content (beware of whitespace) on unbuffered pages. Whitespace is notoriously tricky for JSP Pages in JSP syntax, but much more manageable for JSP Documents in XML syntax.

Default JSP Type :


language CDATA "java"
extends %ClassName; #IMPLIED
contentType %Content; text/xml; UTF-8
import CDATA #IMPLIED
session %Bool; true
buffer CDATA 8kb
autoFlush %Bool; true
isThreadSafe %Bool; true
info CDATA #IMPLIED
errorPage %URL; #IMPLIED
isErrorPage %Bool; false
>


1. is "text/html" for JSP Pages in standard syntax, or "text/xml" for JSP Documents in XML syntax.

2. is "ISO-8859-1" for JSP Pages in standard syntax, or "UTF-8" for JSP Documents in XML syntax.

3. is "ISO-8859-1" for JSP Pages in standard syntax, or "UTF-8" or "UTF-16" for JSP Documents in XML syntax (depending on the type detected as per the rules in the XML specification). Note that, in the case of include directives, the default input encoding is derived from the initial page, not from any of the included pages.


How set the Content Type in JSP :

Using the setContentType() of the response we can change the MIME type and encoding format.

EX :
response.setContentType("text/html");

To check Pre-defined Content Type : click Here

Dynamically Generate PDF

We can generate the PDF dynamically.Here i will explain about the PDF geneartion with the iText Library.

History Of iText:

iText is a library that allows you to generate PDF files on the fly.

iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won't use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you'll build iText into your own applications so that you can automate the PDF creation and manipulation process.

For instance in one or more of the following situations:
Due to time or size, the PDF documents can't be produced manually.
The content of the document must be calculated or based on user input.
The content needs to be customized or personalized.
The PDF content needs to be served in a web environment.
Documents are to be created in "batch process" mode.
You can use iText to:
Serve PDF to a browser
Generate dynamic documents from XML files or databases
Use PDF's many interactive features
Add bookmarks, page numbers, watermarks, etc.
Split, concatenate, and manipulate PDF pages
Automate filling out of PDF forms
Add digital signatures to a PDF file

Process :

1. Decide where store the PDF.In web Apllication it should be stored in server.

String obsPath = this.getRequest().getContextPath();
String RealPath = this.getRequest().getRealPath(obsPath);
File ptfFile=null;
String path = RealPath.substring(0,RealPath.lastIndexOf(File.separator));
ptfFile = new File(path + File.separator + "Generated PDF");
if(!ptfFile.exists()){
ptfFile.mkdir();
}
ptfFile = new File(path + File.separator +"Generated PDF"+
File.separator+"test"+".pdf");


The above code check once before create the pdf if already exists or not.If exists it will updat the existing PDF file.

2. Create the Document and PDFWriter Object.

Document document = null;
PdfWriter writer=null;
document = new Document(PageSize.A4);
writer=PdfWriter.getInstance(document, new FileOutputStream(ptfFile));

3. Open the Document to write the content of the PDF.

document.open();
document.setMargins(document.leftMargin()-40, document.rightMargin()-40,
document.topMargin()+30, document.bottomMargin() -
18);
document.newPage();
form.setFilePath("Generated PDF"+ File.separator+"test"+".pdf");
Phrase phrase = new Phrase(new Chunk(form.getFilePath(),font8));
document.add(phrase);
document.close();


You can set the margine of the PDF using setMargine() function of the document object.

Now the PDF has been generated and stotred in the Specified path.you can open the PDF when the user needs.

Saturday, October 6, 2007

JSP Interview Questions

What is the difference between JSP and Servlets ?

A JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP. But a servlet can support any protocol like HTTP, FTP, SMTP etc.


2 Q What is difference between custom JSP tags and beans?
A Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior ,the tag library descriptor file that maps the XML element names to the tag implementations and the JSP file that uses the tag library

JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags

Custom tags and beans accomplish the same goals -- encapsulating complex behavior into simple and accessible forms. There are several differences:

Custom tags can manipulate JSP content; beans cannot. Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans. Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page. Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.


3 Q What are the different ways for session tracking?
A Cookies, URL rewriting, HttpSession, Hidden form fields


4 Q What mechanisms are used by a Servlet Container to maintain session information?
A Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information


5 Q Difference between GET and POST
A In GET your entire form submission can be encapsulated in one URL, like a hyperlink. query length is limited to 255 characters, not secure, faster, quick and easy. The data is submitted as part of URL.

In POST data is submitted inside body of the HTTP request. The data is not visible on the URL and it is more secure.



6 Q What is session?
A The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests. The session is stored on the server.


7 Q What is servlet mapping?
A The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets.


8 Q What is servlet context ?
A The servlet context is an object that contains a information about the Web application and container. Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use.


9 Q What is a servlet ?
A servlet is a java program that runs inside a web container.


10 Q Can we use the constructor, instead of init(), to initialize servlet?
A Yes. But you will not get the servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.


12 Q How many JSP scripting elements are there and what are they?
A There are three scripting language elements: declarations, scriptlets, expressions.


13 Q How do I include static files within a JSP page?
A Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase.


14 Q How can I implement a thread-safe JSP page?
A You can make your JSPs thread-safe adding the directive <%@ page isThreadSafe="false" % > within your JSP page.


15 Q What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
A In request.getRequestDispatcher(path) in order to create it we need to give the relative path of the resource. But in resourcecontext.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.


16 Q What are the lifecycle of JSP?
A When presented with JSP page the JSP engine does the following 7 phases.

Page translation: -page is parsed, and a java file which is a servlet is created.

Page compilation: page is compiled into a class file

Page loading : This class file is loaded.

Create an instance :- Instance of servlet is created

jspInit() method is called

_jspService is called to handle service calls

_jspDestroy is called to destroy it when the servlet is not required.


17 Q What are context initialization parameters?
A Context initialization parameters are specified by the in the web.xml file, these are initialization parameter for the whole application.


18 Q What is a Expression?
A Expressions are act as place holders for language expression, expression is evaluated each time the page is accessed. This will be included in the service method of the generated servlet.


19 Q What is a Declaration?
A It declares one or more variables or methods for use later in the JSP source file. A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as semicolons separate them. The declaration must be valid in the scripting language used in the JSP file. This will be included in the declaration section of the generated servlet.


20 Q What is a Scriptlet?
A A scriptlet can contain any number of language statements, variable or expressions that are valid in the page scripting language. Within scriptlet tags, you can declare variables to use later in the file, write expressions valid in the page scripting language, use any of the JSP implicit objects or any object declared with a . Generally a scriptlet can contain any java code that are valid inside a normal java method. This will become the part of generated servlet's service method.