Posts Tagged ‘j2ee’

Basic Standard Action Tags In JSP Tutorial With Examples JSP Technical Advices

Standard Action Tags In JSP

Standard Action Tags In JSP are meant for performing some action at run time. As the name of the tag itself suggests these tags are used for run time behavior. Unlike scripting tags which are meant for generating related servlet code at the time of translation,Standard Action Tags In JSP generates code for run time behavior.

 

Examples of Standard Action Tags In JSP are as follows :

 

  1. <jsp:include file= ”AnotherJsp.jsp”/>
  2. <jsp:forward file=”AnotherJsp.jsp”/>
  3. <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”>
  4. <c:set var=”attrName” value=”attrVal” scope=”request”/>

 

There are so many other tags as well amongst which these are some and important.

Let us take these tags individually as see how do they contribute for Standard Action Tags In JSP

 

  1. <jsp:include file= ”AnotherJsp.jsp”/> : This tag is used for run time inclusion of another JSP page in an existing one. This inclusion is not similar to inclusion with include directive. Include directives are meant for static inclusion. This means that the code from the included JSP will be included int the including JSP at the translation time. But in the case of <jsp:include file= ”AnotherJsp.jsp”/> tag a run time call to the included JSP is done and the response from the included JSP is added to the response of the including JSP.
  2. <jsp:forward file=”AnotherJsp.jsp”/> : This tag is used for forwarding the request to another JSP at run time. The difference here from run time or dynamic inclusion is that here the response from the forwarded JSP only goes to the user and the response from the first JSP from where the forward happens gets flushed out.
  3. <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”> : This tag is used to create or fetch a java bean object ,set or get its property value/s and retrieve or set this java bean object in certain scope.
  4. <c:set var=”attrName” value=”attrVal” scope=”request”/> : This tag is used to set an attribute in a particular scope.This is an important tag as our MVC applications rely heavily on attributes
Number of View :1238

Tags: , , , , ,

No Comments


Explain Different Types Of Tags In JSP Interview Asked Questions And Answers

Different Types Of Tags In JSP

 

Java Sever Page (JSP) is an abstraction for servlet in which we have less java code and more tags(HTML and JSP). A JSP page is basically used for views with dynamic data(unlike HTML) and evolved basically for people who do not know java and still work at j2ee server side programming.

Depending upon the type of the usage the tags in JSP page are categorized as :

 

  1. Scripting Tags
  2. Standard Action Tags
  3. Custom Tags

Scripting Tags : These tags are meant for compile time and translation.This means that these tags are meant for translation of code to the servlet. Different types of scripting tags are:

a. Directive Tag

b. Declarative Tag

c. Scriptlet Tag

d. Expression Tag

 

a. Directive Tag : There are three types of directive tags :

1.Page Directive : It is used for imports,encoding etc.

Example: <%@ page import=”…” %>

2.Include Directive : Its is used for static inclusion of JSP and HTML pages

Example: <%@ include file=”anotherJsp.jsp” %>

3.Tag lib Directive: It is used for including tag libraries

Example: <%@ taglib uri=”…” prefix = “..”%>

b. Declarative Tag : These tags are used to declare variables or methods for the servlet translated servlet class.

Example: <%! int i =0;%>

 

c. Scriptlet Tag : These tags are used to write java code inside the JSP page.What goes inside the scriptlet tag is merged into the service method of the translated servlet code.

Example: <% System.out.println(“Hello”);%>

 

d. Expression Tag : These tags are used to print expressions.What is written inside this tag goes as an argument for out.print() inside the service method of the translated servlet.

Example : <%= “Hello” %>

 

It should be noted that we do not use semicolon inside expression tag because it will go as an argument in ou.print() function,hence will result in syntax error.While in case of declarative tag and scirptlet tag we use semicolon.

Number of View :1263

Tags: , , , , , , , , , , ,

No Comments


How To Get Servlet Init Parameters In JSP

How To Get Servlet Init Parameters In JSP

We know that we can declare init parameters for JSP file in web.xml file as we do in the case of servlet.The entry in the web.xml file will be as follows:

<servlet>
    <servlet-name>myJSP</servlet-name>
    <jsp-file>/MyJsp.jsp</jsp-file>
    <init-param>
            <param-name>paramName</param-name>
            <param-value>paramValue</param-value>
     </init-param>
</servlet>
<servlet-mapping>
     <servlet-name>myJSP</servlet-name>
      <url-pattern>/MyJsp.jsp</url-pattern>
</servlet-mapping>

Now comes the question that how can be access or retrieve those initialization parameters in the JSP page.Among the life cycle methods of a JSP page jspInit() method is one of the methods which can be overridden.The jspInit() method is called by the init() method of the servlet which is the result of the translation of the JSP page.To access the initialization parameters is one of the cases where we override the jspInit() method.When we need to declare a method we use the declarative tag as follows :

<%!
 public void jspInit()
 {
   ServletConfig sConfig = getServletCOnfig();
   String str= sConfig.getInitParameter(“paramName”);
   getSerrvletContext().setAttribute(“name”,str);
 }
%>

Here we have overridden the jspInit() method in the declarative tag.Since this method is called by the init method of the translated servlet code we can access the servlet config object with the help of getServletConfig() method as it is passed by the server to the servlet through init() method. This method (jspInit()) will appear as an overridden method in the translated servlet.The point should be noted here that init() method in the servlet calls this jpInit() life cycle method of the  jsp page so it can access the servlet config object.The life cycle method _jspService() is not permitted to be overridden .Initialization parameters for the the JSP page can be understood as the configuration parameters to be accessed by the translated servlet.After all a JSP page is a servlet at the end of the day and it is the translated servlet only which processes the requests from the clients.

 

 

 

Number of View :1412

Tags: , , , , , , , , , , ,

No Comments


Declare Servlet Init Parameters For JSP Page In web.xml File JSP Initialization FAQ

How To Declare Initialization Parameters For JSP Page In web.xml file

Servlet initialization parameters are declared  inside web.xml page and are used to pass the value to the servlet as a part of configuration.Now the question comes that since we do not make an entry for JSP inside web.xml page,how to pass the init parameters to the JSP page.We should remember that a JSP page is a servlet at the end of the day.A JSP is ultimately converted into a servlet then only it processes requests.Like the entry for init parameters in web.xml in case of servlet we make an entry for the JSP page as well,but with a slight difference.The web.xml file looks like this :

<servlet>
    <servlet-name>myJSP</servlet-name>
    <jsp-file>/MyJsp.jsp</jsp-file>
    <init-param>
            <param-name>paramName</param-name>
            <param-value>paramValue</param-value>
     </init-param>
</servlet>
<servlet-mapping>
     <servlet-name>myJSP</servlet-name>
      <url-pattern>/MyJsp.jsp</url-pattern>
</servlet-mapping>

Here we can find that the same way as we declare  init paramters for servlet within the servlet tags,the same way we make an entry for the JSP page so that the init parameters can be declared for that particular JSP page.The only difference is that instead of he <servlet-class> tag we use <jsp-file> tag.The url pattern for the JSP page is same as the JSP file name as we use the JSP file name as URL pattern in the request.Inside the <jsp-file> tag we use the JSP file name with .jsp extension because we need to access the JSP page and not the servlet class.

This is the way we declare servlet init parameters for the JSP page inside the web.xml file.We call it as servlet init parameters for JSP page because these init parameters are sent to the servlet which is the result of translation of the JSP page.And these are used as the configuration parameters for the translated servlet.Now the init parameters can be accessed in he JSP page code.

Number of View :2035

Tags: , , , , , , ,

No Comments


Define URL Rewriting Frequently Asked Interview Question Answers

What Is URL Rewriting

 

As we know that session needs to be maintained because http is a stateless protocol.Due to which connection is lost or session is lost after every request response cycle between the client and the server.In order to maintain or track the session the server needs to get the additional information of the session id so that the session object pertaining to the client or server can be retrieved at the server side and the session can be managed.The session can be managed by either  cookies,hidden parameter or URL rewriting.

URL Rewriting is used in the cases where client browser does not accept cookies.In such case the server does not get the session id thus it is unable to relate he session object with the client or user.In order to maintain the session some extra piece of code needs to be added so that even if the cookies are not accepted at the browser level still the session can be maintained.URL Rewriting is something to fall upon as a precautionary measure.The code to get session and URL Rewriting is demonstrated below :

request.getSession();
out.println("<a href = "\"+response.encodeURL()+""\>Click Here</a>");

Here the anchor tag is used for URL Rewriting.Since the extra measure is used to take care that if the client browser does not accept cookies the session id is attached to the URL so that the session can be maintained.In URL Rewriting the session id is passed as an extra information with the URL that means rewriting of the URL is done.URL Rewriting can also be done with redirection as below :

response.sendRedirect(response.encodeRedirectURL("URL-Pattern of the servlet"));

The first time the server  tries with both cookies and URL Rewriting ,if it finds that the cookies are not coming it checks for session id as a part of URL Rewriting and finds that it has to fall upon URL Rewriting if the code has provisioned that.

 

Number of View :1205

Tags: , , , , , , , , ,

No Comments


Basic Servlet Attributes Thread Safety Interview Questions Answers Explained

Servlet Attributes Thread Safety  

An attribute is used to communicate some value from one servlet to another. Depending upon the scope of the attribute we can access the attributes at parts of the application.An attribute can be set at either of the scopes as follows :

1.Application

2.Session

3.Request

 

Now in order to find out which attribute/s is/are thread safe,we can ponder upon with following discussion.

1.Application Attributes : An application attribute is a value set at application level. The code to set the attribute at application level is :

getServletContext().setAttribute(“name”,”value”);

As these attributes are accessible throughout the application,any servlet or any other web entity can update or get these attributes from various parts of the application. Since application attributes can be accessed or modified by any servlet in the web application,these are not thread safe.

 

2.Session Attributes : A session attribute is a value set at session scope. Session is used to retain user specific data. The code to set the attribute at application level is :

req.getSession().setAttribute(“name”,”value”);

When we talk about thread safety of session attributes,at one glimpse it deceives to appear as thread safe. In cases where we have the new tab feature in the modern browsers,the same session for the web application is opened in the new tab. In such case, if session attribute are updated from one tab will be reflected in the another tab with the same session. Thus we can say that session attributes are also not thread safe.

3.Request Attributes : A request attribute is a value set at request scope.he code to set the attribute at application scope is :

req.setAttribute(“name”,”value”);

As we know that each request is a unique object , whatever attribute we set at request scope cant be updated or get from another request. Thus request attributes are thread safe.

Conclusion : Thus request attributes are thread safe and application and session attributes are not.

Number of View :1411

Tags: , , , , , , , , ,

No Comments


Statements In JDBC JDBC Interview Questions JDBC Statements PreparedStatement and CallableStatement JDBC Technology Call Stored Procedures

Statements In JDBC

 

Statements  in JDBC are carrier for sql queries in JDBC.  Statements in JDBC are obtained from connection objects.

Connection is the handle for the session between JDBC and Database server.Without connection object we cannot have a statement object and without statement object,sql queries cannot be run at the database server side from the JDBC code.

There are three types of statements in JDBC.These are as follows :

1.Statement

2.Prepared Statement

3.Callable Statement

 

1.Statement : These statements are used to execute queries like select statements at the server side. Every time a statement is run it is compiled at the server (database side),it is compiled at the server side and run.

The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "select * from tablename";
Statement st  = conn.createStatement(sql);
st.executeQuery();

 

2.Prepared Statement : These statements are used where we have to execute same query multiple number of times with small changes like update value.In such cases we do not need to compile the query again and again at the server side but we need to compile he query once and keep executing again and again at the the server side.Thus,prepared statements help in this case as these are precompiled statements which can be executed ample times thus saving time.

 

The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "insert into tablename values (?,?,?)";
PreparedStatement pst  = conn.prepareStatement(sql);
pst.setString(1,"java");
pst.setInt(2,3);
pst.setFloat(3,3.14);
pst.executeQuery();

 

The ‘?’ are placeholders to feed the value to the precompiled statements.setXXX methods are used to set the values to the placeholders depending upon the  data type at the server side.In the code pst.setString(1,”java”),1 denotes the first place holder.

 

3.Callable Statement : These statements are used to execute stored procedures and function at the database server side.

The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "{? = execute procesdureorfunctionname(?,?)}";
CallableStatement cst  = conn.prepareCall(sql);
cst.registerOutParameter(1,java.sql.Types.String);
cst.setString(2,"java");
cst.setInt(3,3);

cst.execute();
String str = cst.getString(1);

 

Number of View :1066

Tags: , , , , , , , , , , ,

No Comments


Life Cycle Of A Servlet Explained Handling Servlet Life-Cycle Events Servlet Life Cycle Servlet Interview Questions Java Servlet Programming

Life Cycle Of A Servlet 

A servlet is a java code which runs at the server side to process request and return response to the client.

In other words, a servlet is an extension to the server which processes request and returns response as an HTML page to the client. Like applets we donot have main method in a servlet. As in case of applet,its life cycle is taken care by the browser itself,similarly in case of servlet its life cycle of a servlet is managed by the server/container.

 

Life Cycle Of A Servlet denotes the phases it goes through. These phases are symbolized by the life cycle methods in the servlet.As server manages the Life Cycle Of A Servlet, these life cycle methods are called by the server itself. Let us have a look at different Life Cycle methods Of A Servlet. These are as follows :

 

  1. init
  2. service
  3. destroy

 

1. init : The init method is the first Life Cycle method Of A Servlet. It is this method which after being called by the server makes a servlet eligible for being called a servlet. After the servlet class constructor is called,the init method is called by the server. The method looks like :

public void init(ServletConfig sc) throws ServletException {
  // Initialization code...
}

 

Here we can see that the ServletConfig object is passed to the init method. This object is created by serverand passed to the init method. Having received the ServletConfig object ,a servlet is said to be a servlet.

2. service : This life cycle method is called every time a request is sent to the servlet .This means that the servlet’s init method has been called and servlet is ready to process requests.This method looks like :

public void service(ServletRequest request,
                    ServletResponse response)
      throws ServletException, IOException{
}

3.destroy : This is the last phase in the Life Cycle Of A Servlet. Before the servlet object is garbage collected at the server side this method is called to relinquish the resources at the server side. This method looks like :

public void destroy() {
    // Finalization code...
  }

It can be noted that the init method is called only once in the Life Cycle Of A Servlet, service is called as many times as many the requests are fired and destroy is called only once in the Life Cycle Of A Servlet.

Number of View :838

Tags: , , , , , , , , , ,

No Comments


Request Dispatching In Servlets Servlets / Request Dispatcher RequestDispatcher Vs SendRedirect What Is A Request Dispatcher And How Does It Work What Is A Request Dispatcher And How Do I Use It Include Forward Redirect

Servlet Request Dispatching

Delegating the request from one servlet to another or transfer of excution from one servlet to another is known as request dispatching in servlet.

In another ways we can define it as interservlet communication,which means one servlet calling another servlet.This concept can be used in cases where some piece of code is needed to be written in different servlets.Thus, in such cases we can write that code in a single servlet and keep on calling that code from different servlets.

There are three ways in which request delegation can be done. These ways are as follows :

 

1.Include

2.Forward

3.Redirect

 

1. Include : This method suggests that the response from the another servlet be included in the response of the first  servlet. This is equivalent to copy pasting the response of one servlet into another.In case of inclusion,the response which the client receives is the append of both including and the included servlets.However,for the client it appears that the including servlet only has responded.Following line of code is used to include a servlet response into another servlet :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");
rd.include(request,response);

 

Here RequestDispatcher object is needed .We can see that request and response objects of the first servlet are passed to the servlet which we wish to include through include method so that request remains the same and the included servlet can write to the response of the including servlet and it appears that including servlet itself has processed the request.

2. Forward : It works same as  include except for the difference that the reponse from the first servlet is flushed out and the reponse from the second servlet only is visible.Example code is :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");
rd.forward(request,response);

 

Here also it appears to the client that the first servlet only has processed the request.

3. Redirect : In the case of redirect the first servlet issues a new request to a another servlet to which redirection has happened.In this case client sees a new request in the browser url bar and can realize that the response comes from the second servlet.Example code is :

response.sendRedirect("url-pattern of the servlet");

 

Here we can see that response object is used.This done for the reason that the issuing of the new request at the client side,in the browser url bar has to be done by the first servlet.

 

Number of View :710

Tags: , , , , , , , ,

No Comments


Transaction Locking And Isolation In JDBC Transaction isolation levels Using Transactions JDBC Isolation Example JDBC Guide Basics Of JDBC

Transaction Locking And Isolation In JDBC

Transaction is a set of instructions which follow atomicity,consistency,Integrity and durability.

1. Atomicity : This states that either all actions should complete or none should occur.

2. Consistency : This imposes certain business rule.

3. Integrity : This states that the two actions shall not interfere with each other or access each others data.

4. Durability : This states that the changes occurred due to a certain action or transaction must be stored in a durable persistent storage.

 

When we talk about the Transaction Locking And Isolation In JDBC, following are the issues we need to address when to concurrent transactions are taking place :

1. Dirty read : This happens when uncommitted data is read.Typical example is :

a. Transaction 1 inserts a row into a table.

b.Transaction 2 reads the new row.

c.Transaction 1 rolls back.

2. Non-repeatable read : This happens when data reads from the same table for the same row are not same.Typical example is

a. Transaction 1 reads a row.

b. Transaction 2 updates the row.

c. Transaction 1 reads the same row a second time and gets a different  results.

 

3. Phantom Read : This occurs due to appearing of new records when same query is run at different times.Typical example is :

a. Transaction 1 reads all rows that satisfy a WHERE clause.

b. Transaction 2 inserts additional rows that satisfies the WHERE clause.

c. Transaction 1 executes  the WHERE condition and picks up the additional row.

There are different transaction isolation levels used from JDBC coding to address these issues.The function to set isolation level is :

 

con.setTranscationIsolationLevel(intvalue);

Different integer values can be provided as follows :

0 – JDBC_TRANSACTION_NONE
1 – JDBC_TRANSACTION_READ_UNCOMMITTED
2 – JDBC_TRANSACTION_READ_COMMITTED
4 – JDBC_TRANSACTION_REPEATABLE_READ
8 – JDBC_TRANSACTION_SERIALIZABLE

Once we set the isolation levels using the above function,we can address the issues of transaction level in JDBC connection.As the isolation     level increases it gets stricter and lower level problems are also addressed.If we have set the value to 8 ,the isolation level with value lesser than 8 ,ie. 0,1,2,4, are also taken  care of.

Number of View :662

Tags: , , , , , , , , ,

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache