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

Sunday, February 7, 2010

JSP Program to Delete a Cookie Values

JSP is a dynamic scripting capability for web pages that allows Java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix traditionally ends with .jsp to indicate to the web server that the file is a JSP files. JSP is a server side technology - you can’t do any client side validation with it. The JSP assists in making the HTML more functional. Servlets on the other hand allow outputting of HTML but it is a tedious process. It is easy to make a change and then let the JSP capability of the web server you are using deal with
compiling it into a servlet and running it.

Sample JSP Program to Delete a Cookie Value:

<%
Cookie killCookie = new Cookie(“Ck”, null);

KillCookie.setPath(“/”);

killCookie.setMaxAge(0);

response.addCookie(killCookie);

%>
JSP pages are focused around HTML (or XML) with Java codes and JSP tags inside them. When a web server that has JSP support is asked for a JSP page, it checks to see if it has already compiled the page into a servlet. Thus, JSP pages become servlets and are transformed into pure Java and then compiled, loaded into the server and executed.

Read more...

Sunday, July 5, 2009

Struts and JSP

Struts makes it possible for JSP pages to externalize flow control, rather than specify physical links to various JSP pages within the JSP file, the JSP file contains a Struts-defined logical URI. The Struts URI defines a logical page request mapped to actions that may return different physical JSP pages depending on the context of the HTTP request.

Struts simplifies the development of the actual JSP file content by limiting it to user interface generation only. Java that would otherwise appear inside the JSP files appears in separate servlet action classes that the JSP page invokes at runtime.

Struts helps to separate the development roles into user interface designer (HTML or tag library user) and JSP action-handler developer. For example, one person can write JSP page using only HTML or suitable tag libraries, while another person works independently to create the page action handling classes in Java.

Struts externalizes JSP actions that would otherwise appear inside all the JSP pages of your project into a single configuration file. This greatly simplifies debugging and promotes reuse. Struts consolidates String resources that would otherwise appear inside all the JSP pages of your project into a single file. This simplifies the task of localizing JSP applications.

Read more...

Thursday, June 18, 2009

JSP Tags

Tags in JSP

JSP is a serverside technology to make content generation a simple appear. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content.
Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development.

JSP contains the following tags

  • Declaration tag
  • Expression tag
  • Directive Tag
  • Scriptlet tag
Declaration tag ( <%! %> )

A declaration tag declares one or more variables or methods for use later in the JSP source file. A declaration tag must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration tag must be valid in the scripting language used in the JSP file.

This tag allows the developer to declare variables or methods. Before the declaration you must have
<%! At the end of the declaration, the developer must have %> Code placed in this tag must end in a semicolon ( ; ). Declarations do not generate output so are used with JSP expressions or scriptlets.
<%!
private int counter = 0 ;
private String get Account ( int accountNo) ;
%>
Expression tag ( <%= %>)

An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file.

This tag allows the developer to embed any Java expression and is short for out.println(). A semicolon ( ; ) does not appear at the end of the code inside the tag. Example to show the current date and time
Date : <%= new java.util.Date() %>
Directive tag ( <%@ directive … %>)

A JSP directive tag gives special information about the page to the JSP Engine.

There are three main types of directives:
  • page – processing information for this page.
  • Include – files to be included.
  • Tag library – tag library to be used in this page.
Directives do not produce any visible output when the page is requested but change the way the JSP Engine processes the page.

Scriptlet tag ( <% … %> )

A scriptlet tag can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. We can declare variables or methods 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 tag. You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet. Scriptlets are executed at request time, when the JSP engine processes the client request.

If the scriptlet produces output, the output is stored in the out object, from which you can display it. Between <% and %> tags, any valid Java code is called a Scriptlet. This code can access any variable or bean declared. For example, to print a variable.
<%
String username = “visualbuilder” ;
out.println ( username ) ;
%>

Read more...

Monday, June 15, 2009

Hybrid Server Pages beyond JSP

Hybrid Server Pages beyond the Java Server Pages

JSP combined HTML and Java in one source text but did not go far enough. Hybrid Java Language (HJL) is the next step in the same direction. HJL supports code reuse in the form of widgets written in same Hybrid Java Pages. Generated Java code gets built into a natural pages and components framework wrapped around Servlet API.

Hybrid Java Language incorporates Java and HTML operators into a single formal grammar.We may consider the language as consisting of three parts – subset of Java, subset of HTML and all the rest. The latter serves for code factorization and reuse as well as for ‘gluing’ Java and HTML. Widgets are the units of code reuse.

The Hybrid Java Pages code of an application is provided as a set of .page files (one per Web page) and a set of .widget files. A widget may define named attributes and/or named slots (or one anonymous slot). A slot is somewhat similar to the position between opening and closing a ‘library tag’. The code inHybrid Java Pages slots has Java context of the point of call of the widget, so widgets are transparent for Java context (same as HTML elements are by the way).

The rest of the .widget file is just ARBITRARY Hybrid Java Pages code – no other programming support or configuration is necessary to define a widget.

Technology suggests a very natural framework that includes application, page and widget data persistent in the scope of HTTP session. Developer defines such data as members of ‘code-behind’ classes, which may contain handlers of those states called by the framework with a proper dispatching in case of widgets.

Compiler resolves the identifiers used in widgets and pages against Java definitions in Java blocks, definitions of widget attributes, in instances of widgets and page state classes. Widgets may send signals to parent and page.

Read more...

Thursday, May 28, 2009

Java Server Pages (JSP) in breif

JavaServer Pages (JSP) is a technology based on the Java language and enables the development of dynamic web sites. JSP was developed by Sun Microsystems to allow server side development. JSP files are HTML files with special Tagscontaining Java source code that provide the dynamic content.

JSP source code runs on the web server in the JSP Servlet Engine. The JSP Servlet engine dynamically generates the HTML and sends the HTML output to the client’s web browser.

JSP is based on Java, an objectoriented language. JSP offers a robust platform for web development. JSP code on the server is classed as the implementation. By having a separation of presentation and implementation, designers work only on the presentation and developers concentrate on implementing the application.

JSPs are built on top of SUN’s servlet technology. JSPs are essential an HTML page with special JSP tags embedded. These JSP tags can contain Java code. The JSP file extension is .jsp rather than .htm or .html. The JSP engine parses the .jsp and creates a Java servlet source file. It then compiles the source file into a class file, this is done the first time and this why the JSP is probably slower the first time it is accessed. Any time after this the special compiled servlet is executed and is therefore returns faster.

Read more...

Wednesday, May 20, 2009

Implementing a thread-safe JSP page

We can make a thread-safe JSP by having them implement the SingleThreadModel interface. This is done by adding the below directive within your JSP page.

<%@ page isThreadSafe="false" %>

By this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine.

More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition.

SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use . You should try really hard to make them thread-safe the old fashioned way by making them thread-safe.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP