Archive for the ‘JSP’ Category

jsp:useBean

Posted: December 4, 2009 in J2EE, Java, JSP
Tags: ,

Consider the classes Person and Employee

package foo;
public abstract class Person {
private String name;
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
}

package foo;
public class Employee extends Person {
private int empID;
public void setEmpID(int empID) {
this.empID = empID;
}
public int getEmpID() {
return empID;
} }

Now if we try to get the attribute name on a jsp page, then

<jsp:useBean id=”person” type=”foo.Person” class=”foo.Employee” scope=”page”>

The generated Servlet is

foo.Person person = null;
// code to get the person attribute
if (person == null){
person = new foo.Employee();

Note:

If type is used without class, the bean must already exist.
If class is used (with or without type) the class must NOT be abstract, and must have a public no-arg constructor

type == reference type
class == object type

type is what you DECLARE (can be abstract)
class is what you INSTANTIATE (must be concrete)
type x = new class()

Advertisements

JSP Elements

Posted: December 2, 2009 in J2EE, Java, JSP
Tags: ,

Scriplet                <%         %>        Just plane old java

Directive            <%@     %>          Special Instructions to the container

Expression         <%=     %>         argumentto the out.println() method

Declaration      <%!       %>            Used to declare members of the generated servlet class. i.e, this is used to declare static variables and methods outside the _jspService() method.

Note :  All Expression and Scriptlet code lands inside the service method (_jspService) . i.e the variables declared in a scriplet are local variables

JSP Directives

Posted: December 2, 2009 in J2EE, Java, JSP
Tags: , ,

The directive tag gives special information about the page to JSP Engine. This changes the way JSP Engine processes the page. Using directive tag, user can import packages, define error handling pages or session information of JSP page.

There are mainly 3 JSP Directives

  1. Page Di rective
  2. Include Directive
  3. Taglib Directive
  1. Page Directives
  • language
  • extends
  • import
  • session
  • buffer
  • autoFlush
  • isThreadSafe
  • info
  • errorPage
  • IsErrorPage
  • contentType

language:

This attribute is used to denote the language used by a file. Language denotes the scripting language used in scriptlets, declarations, and expressions in the JSP page and any included files.

Syntax of language attribute available for page directive is

<%@ page language = “lang” %>

extends:

This is used to signify the fully qualified name of the Super class of the Java class used by the JSP engine for the translated Servlet.

Syntax of extends attribute available for page directive is <%@ page extends = “package.class”%>;

import:

The import attribute is used to import all the classes in a java package into the current JSP page. With this facility, the JSP page can use other java classes.

Syntax of import attribute available for page directive is  <%@ page import = “java.util.*” %>;

session:

The session attribute, when set to true, sets the page to make use of sessions.

Syntax of session attribute available for page directive is
<%@ page session=”true|false” %>;

buffer:

If a programmer likes to control the use of buffered output for a JSP page then the buffer attribute can be made use of for achieving this.

Syntax of buffer attribute available for page directive is

<%@ page buffer = “none|8kb|sizekb” %>;

In the above statement page and buffer are keywords. The size of buffer size is mentioned in kilobytes.  This is used by the out object to handle output sent from the compiled JSP page to the client web browser. The default value is 8kb. If a user specifies a buffer size then the output is buffered with at least the size mentioned by the user.

Other page directives are

<%@ page contentType=”description” %> Sets the content type and character encoding of the page.

<%@ page autoFlush=”true” %> Use to flush the page buffer when it fills. Default value is true. If value of autoFlush is false, the JSP engine will throw an exception if the buffer overflows.

<%@ page errorPage=”path” %> This defines a page of the given path to display if an error occurs in the JSP page

<%@ page isErrorPage=”true” %> This gives permission an error page access to the exception implicit variable. Its default value is false.

<%@ page isThreadSafe=”true” %> This tells the JSP that multiple pages can execute in parallel. Defaults to true.

Include Directive

Include a raw file of given path in attribute value.

Syntax ::  <%@ include file=”path” %>
Taglib Directive

Taglib directive is used to configures tags with prefix x to use the tag library foo.

Syntax :: <%@ taglib prefix=”x” uri=”foo” %>

Request Dispatcher

Posted: December 1, 2009 in Java, JSP
Tags: ,
  • Request Dispatcher has got only methods – forward() and inlcude() both of which takes in the HttpServletRequest and HttpServletResponse Objects as the parameters
  • We can get a RequestDispatcher in 2 ways – from request or  servletContext
  • RequestDispatcher view = request.getRequestDispatcher(“output.jsp”);
  • RequestDispatcher view = getServletContext.getRequestDispatcher(“/output.jsp”); (You MUST use the forward slash with the getRequestDispatcher() method of ServletContext.)
  • Now that the  RequestDispatcher knows which jsp to forward the response, we can call the forward method                                                                                                                                                                        view.forward(request, response)
  • The include method is not used that much in the real. Its usually used to send the request to something else (preferably another servlet ), do some work and come back with the response.
  • Its more like asking help for handling a request – a temporary transfer of control.

JSessionId and Cookies

Posted: June 22, 2009 in J2EE, Java, JSP, Uncategorized
  • They are both used to store/retrieve information from the client side of the connection

http://www.webmasterworld.com/html/3568254.htm

When a visitor comes to your site, your server (running some scripting engine) says: “OK, I got another visitor. I do already have fifty, so this one will be 51st. Gotta record it somewhere, not to get confused…” The server may record it in various ways. For example, it can create a temporary file with the number as a name, and store all stuff about that particular visitor in the file.
This is called a Session ID (SID). This is an identificator of a particular visitor that browses the site at the moment.

The server also stores the SID in the visitor’s browser. When the server sends the first page of the site to the visitor, it sends the SID along with it. It says: “Here’s your page dude, and by the way, you’re the visitor number 51 currently, don’t forget it, OK?”
This is called a cookie.
The browser stores the cookie somewhere on the visitor’s computer. For example, Internet Explorer saves them in C:\Documents andSettings\Username\Local Settings\Temporary Internet Files.

When the visitor wants to view another page on the same site, the browser requests it from the server, and passes the cookie along with the request. “Hey dude, gimme that page please… Oh, and by the way, I’m the visitor 51, hope you didn’t forget me :)”
The server receives the request, the cookie and thinks: “Wow, seems like I know this guy! He asks for the second page already! OK, he gets it…” and all the following server actions are done according to the “memory” that he has about that particular user.



JSP Tags

Posted: June 20, 2009 in J2EE, Java, JSP
Tags: , ,

JSP Tags

There are 4 types of JSP tags.

1) Directives

2) Declarations

3) Scriplets

4) Expressions

Directives

In directives, we can import packages, define error handling pages or session information of the JSP page.

Syntax : <%@directive attribute=”value”%>

where directive can be

  • page : page is used to provide the information about it.

<%@page language=”java”%>

  • include : include is used to include a file in a jsp page.

<%@include file=”/header.jsp”%>

  • taglib : taglib is used to use the custom tags in the JSP pages

<%@taglib uri=”tlds/taglib.tld” prefix=”mytag”%>

and attribute can be

  • language : This tells the server that the page is using java language(supports java only)
  • extends :  This is used when we want to extend a class

<%@page language=”java” import=”java.sql.*,mypackage.myclass” %>

  • session :  When this attribute is true, session is available to the jsp page (by default it is true)

<%@page language=”java” session=”true” %>

  • errorPage : This is used to handle unhandled exceptions in the page

<%@page session=”true” errorPage=”error.jsp”  %>

  • contentType : Used to set MIME type and character set of the JSP

<%@page language=”java” session=”true” contentType=”text/html;charset=ISO-8859-1″  %>

Declarations

  • Used to declaring functions and variables to be used in the JSP page.
  • begins with <%! and ends with %>

Syntax

<%!

// java code

%>

Example

<%@page contentType=”text/html” %>

<html>

<body>

<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value
cnt++;
return cnt;
}
%>

<p>Values of Cnt are:</p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

<p><%=getCount()%></p>

</body>

</html>

Scriptlets

  • Using this tag, we can insert any amoung of valid java code and these codes are placed in the _jspServiceMethod of the JSP engine

Syntax

<%

//java cpde

%>

  • variables available for JSP Scriplets are
    • request :
      • This represent the request from the client and is a subclass of HttpServletRequest.
      • Use this to retrieve the data submitted along with the request

<%
//java codes
String userName=null;
userName=request.getParameter(“userName”);
%>

    • response
      • response is the subclass of HttpServletResponse
    • session
      • It represents the session associated with the request
    • out
      • Its an object of the output stream and is used to send any output to the client.

Expressions

We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.

Syntax

<%=”Any thing”%>