Showing posts with label Java Server Pages. Show all posts
Showing posts with label Java Server Pages. 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...

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...

Monday, December 1, 2008

forward sendRedirect

Difference Between sendRedirect() and Forward() :

Forward( ) :

javax.Servlet.RequestDispatcher interface.

  • RequestDispatcher.forward( ) works on the Server.
  • The forward( ) works inside the WebContainer.
  • The forward( ) restricts you to redirect only to a resource in the same web-Application.
  • After executing the forward( ), the control will return back to the same method from where the forward method was called.
  • The forward( ) will redirect in the application server itself, it does'n come back to the client.
  • The forward( ) is faster than Sendredirect( ) .
To use the forward( ) of the requestDispatcher interface, the first thing to do is to obtain RequestDispatcher Object. The Servlet technology provides in three ways.

1. By using the getRequestDispatcher( ) of the javax.Servlet.ServletContext interface , passing a String containing the path of the other resources, path is relative to the root of the ServletContext.
RequestDispatcher rd=request.getRequestDispatcher ("secondServlet");
Rd.forward(request, response);
2. getRequestDispatcher( ) of the javax.Servlet.Request interface , the path is relative to current HttpRequest.
RequestDispatcher rd=
getServletContext( ).getRequestDispatcher("servlet/secondServlet"); Rd.forward(request, response);

3. By using the getNameDispatcher( ) of the javax.Servlet.ServletContext interface.
RequestDispatcher rd=
getServletContext( ).getNameDispatcher("secondServlet");

Rd.forward(request, response);
Sendredirect( ) :

javax.Servlet.Http.HttpServletResponce interface

  • RequestDispatcher.SendRedirect( ) works on the browser.
  • The SendRedirect( ) allows you to redirect trip to the Client.
  • The SendRedirect( ) allows you to redirect to any URL.
  • After executing the SendRedirect( ) the control will not return back to same method.
  • The Client receives the Http response code 302 indicating that temporarly the client is being redirected to the specified location , if the specified location is relative , this method converts it into an absolute URL before redirecting.
SendRedirect( ) will come to the Client and go back,.. ie URL appending will happen.
Response. SendRedirect( "absolute path");
Absolutepath – other than application , relative path - same application.

Conclusion:

forward() runs on the server side where as sendRedirect runs both on the client as well as on the server side thats why the response generated by sendRedirect() is slow as compared to rd.forward().

sendRedirect() you can forward the request to any web application either in the same server or to the another one. Incase of forward() the request has to be forwarded to the same web application.

forward() method holds the previous request and response objects while using sendRedirect(),it will create fresh request and response objects.

sendRedirect() always sends a header back to the client/browser, this header then contains the resource(page/servlet) which you wanted to be redirected. the browser uses this header to make another fresh request. sendRedirect has a overhead since its like any other Http request being generated by ur browser.

response.sendRedirect() sends a response to the browser asking it to load another page, whereas in the case of RequestDipsatcher.forward() control is transferred to another servlet or jsp within the server.

Using sendRedirect() on one server, we call redirect a call to a resource on located on different server which is not possible using forward().

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container.

sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

Read more...

Wednesday, November 19, 2008

Jsp Objects / Implicit Objects in Jsp

Jsp Implicit Objects:

Implicit objects in JSP are Java objects that JSP Container makes available to developers in each page. These implicit Objects are automatically available in JSP. JSP Container provides developer to access these implicit objects in their program using JavaBeans and Servlets. These objects need not be declared or instantiated by the JSP author.

They are available only within the _jspService method and not in any declaration. These objects are called implicit objects since they are automatically instantiated by the container and are accessed using standard variables. The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration.

Many implicit objects available in JSP. Some of them are mentioned below:

request object in jsp

The request object has a request scope. It is an instance of the classes that implement javax.servlet.ServletRequest interface. It encapsulates the request coming from a client and uses the getParameter() method to access request parameters. It is passed to the JSP by the container as a parameter to the _jspService() method. This denotes the data included with the HTTP Request. The client first makes a request that is then passed to the server. The requested object is used to take the value from client’s web browser and pass it to the server. This is performed using HTTP request like headers, cookies and arguments.

response object in jsp

The response object has a page scope. It is an instance of the classes that implement javax.servlet.ServletResponse class. It encapsulates the response generated by the JSP to be sent to the client in response to the request. It is generated by the container and passed to the JSP as a parameter to the _jspService() method. This denotes the HTTP Response data. The result or the information from a request is denoted by this object. This is in contrast to the request object. The class or the interface name of the object response is http.HttpServletResponse. The object response is of type Javax.servlet.http. ttpservletresponse. Generally, the object response is used with cookies. The response object is also used with HTTP Headers.

Session object in jsp

The session object has a scope of an entire HttpSession. It is an instance of the javax.servlet.http.HttpSession class. It represents the session created for the requesting client, and stores objects between client's requests. The session object views and manipulates session information, such as the session identifier, creation time, and last accessed time. It also binds objects to a session, so that the user information may persist across multiple user connections.

The session object is valid only for HTTP requests.This denotes the data associated with a specific session of user. The class or the interface name of the object Session is http.HttpSession. The object Session is of type Javax.servlet.http.httpsession. The previous two objects, request and response, are used to pass information from web browser to server and from server to web browser respectively. The Session Object provides the connection or association between the client and the server. The main use of Session Objects is for maintaining states when there are multiple page requests.

jsp Out object

This denotes the Output stream in the context of page. The class or the interface name of the Out object is jsp.JspWriter. The Out object is written: Javax.servlet.jsp.JspWriter. The out object has a page scope. It is an instance of the javax.servlet.jsp.JspWriter class. The JspWriter class is the buffered version of the Printwriter class. It represents the output stream opened back to the client and provides the access to handle servlet's output stream.

jsp PageContext object

This is used to access page attributes and also to access all the namespaces associated with a JSP page. The lass or the interface name of the object PageContext is jsp.pageContext. The object PageContext is written: Javax.servlet.jsp.pagecontext The PageContext object has a page scope. It is an instance of the javax.servlet.jsp.PageContext class. It encapsulates the page-context for the particular JSP page. A pageContext instance provides access to all the namespaces associated with a JSP page. It also provides access to several page attributes such as to include some static or dynamic resource. Implicit objects are added to the PageContext automatically.

jsp Page object

The Page object denotes the JSP page, used for calling any instance of a Page's servlet. The class or the interface name of the Page object is jsp.HttpJspPage. The Page object is written: Java.lang.Object.

jsp Application object

The application object has an application scope. It is an instance of the javax.servlet.ServletContext class. It represents the context within which the JSP is executing. It defines a set of methods that a servlet uses to communicate with its servlet container. These functions include getting the MIME type, request dispatching, and writing contents to a log file. It allows the Web components in the JSP page in the application to share information. This is used to share the data with all application pages. The class or the interface name of the Application object is ServletContext. The Application object is written: Javax.servlet.http.ServletContext.

jsp config object

The config object has a page scope. It is an instance of the javax.servlet.ServletConfig class. The ServletConfig parameter can be set up in the web.xml inside the element. It uses the getInitParameter(String param) to obtain initialization parameters and the getServletContext() method to obtain the ServletContext object. This is used to get information regarding the Servlet configuration, stored in the Config object. The class or the interface name of the Config object is ServletConfig. The object Config is written Javax.servlet.http.ServletConfig.

jsp implicit object exception

The exception object has a page scope. It is an instance of the java.lang.Throwable class. It refers to the runtime exception that resulted in the error-page being invoked. This is available only in an error page, i.e., a page that has isErrorPage=true in the page directive.

Read more...

Sunday, November 2, 2008

JSP 2.1 Updates

Java Server Pages (JSP) 2.1

Jsp2.1 includes webtier technologies namely Java Standard Tag Library(JSTL) and JavaServerFaces(JSF) technology. Complete alignment of JavaServer Faces technology tags and JavaServer Pages (JSP) software code.

JSP 2.1 is Part of J2EE 5.0 and Dependant on JDK 1.4, Servlet 2.5. JSP 2.1 supports annotations for dependency injection on JSP tag handlers and context listeners. New features from Faces into JSP

Support for resource injection through annotations to simplify configuring access to resources and environment data

Expression Language

The expression language provides a way to simplify expressions in JSP. It is a simple language used for accessing implicit objects and Java classes, and for manipulating collections in an elegant manner. EL provides the ability to use run-time expressions outside of JSP scripting elements.

JavaServer Faces and JavaServer Pages each has its own expression language. The expression language included in JSP provides greater flexibility to the web application developer. But those who are working with JSF have found themselves unsatisfied by the JSP expression language.

New unified EL essentially represents a union of the JSP and JSF expression languages and largely benefits JSF technology. The unified EL has the following features:

* Deferred evaluation of expressions.
* Support for expressions that can set values and expressions that can invoke methods.
* Support for using JSTL iteration tags with deferred expressions
Evaluation of EL is categorized as immediate evaluation and deferred evaluation. Immediate evaluation means a JSP page evaluates the expression when the page is rendered. With immediate evaluation, all values are always read-only. JSP EL expressions take the form of ${imExpr}. JSP expressions are evaluated immediately.

Deferred evaluation means that the technology using the unified EL takes over the responsibility of evaluating the expression from the JSP engine and evaluates the expression at the appropriate time during the page lifecycle.

Compatibility

In JSP 2.1 technology, a deferred expression does not make sense in the context of template text because evaluation of an expression is always done immediately by the JSP container. Because no entity other than the JSP container processes template text, a deferred expression would always be left unevaluated if it were included in template text. So therefore, any page authors who have included the #{ character sequence in template text probably meant to use the ${} syntax instead. Acknowledging this fact, the JSP specification authors have mandated that the inclusion of #{ in template text triggers a translation error in JSP 2.1 technology.

Maintaining compatibility with earlier versions is critical for any Java EE platform release. For JSP 2.1 technology, the only backward compatibility issue involves the EL's new support of the #{ syntax that refers to deferred expressions. The #{ character sequence can create backward compatibility problems in two situations: when the sequence is included in template text or in attribute values.

Version requires these jars:
-> ant-1.6.5.jar

-> core-3.1.1.jar

-> jsp-2.1.jar

-> jsp-api-2.1.jar

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP