Core Tag Simple Examples

For using core tags in JSP, first put “jstl.jar” and “standard.jar” in the WEB-INF/lib directory of the web application. Then you have to include the taglib directive in JSP as

 <%@ taglib uri =”http://java.sun.com/jstl/core” prefix =”c” %>

Here the uri is nothing but the uri specified in the c.tld file (c.tld is there in standard.jar). You can give any value as prefix other than the reserved words jsp, jspx, java, javax, servlet, sun, sunw. Whatever we are giving as prefix, it should come before the tag. Like if you are giving a prefix ‘s’ then the tag ‘out’ will be written as ‘s:out’ rather than ‘c:out’. 

Below are some core tags and their simple usage. Though it is not required to see the tld file for using simple tags, it is good idea to do so to have a better understanding.

1) out tag. See the tag description in c.tld

 <tag>
    <name>out</name>
    <tag-class>org.apache.taglibs.standard.tag.el.core.OutTag</tag-class>
    <body-content>JSP</body-content>
    <description>
 Like &lt;%= … &gt;, but for expressions.
    </description>
    <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>default</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>escapeXml</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

Here the <name> is nothing but name of the tag
<tag-class> specifies the name of the tag handler class.
<body-content> specifies whether the tag can have a body or not. Here the value ‘JSP’ tells that the tag ‘out’ can have a JSP body.
<attribute> element specifies the attributes that a tag can have.
<name> is the name of the attribute
<required> specifies whether the attribute is mandatory or not. If it is true, we have to provide a value for that attribute.
<rtexprvalue> specifies whether the attribute can take request time expressions or not.

The attribute default is being used for passing a default value to the tag. In case the value attribute is null the tag will display the default value.

escapeXml tells whether the xml has to be rendered or not while displaying the value.

From the above description it is clear that the ‘out’ tag can have a JSP body and it has a mandatory attribute value. The tag is used for display values in JSP.  The value to be displayed should be passed as the attribute value to the tag.

<%@ taglib uri =”http://java.sun.com/jstl/core” prefix =”c” %>
<html>
<body>
<c:out value =”Welcome to Core Tags” />
</body>
</html>

Usage of escapeXml.

Try this <c:out value =”<b>Welcome to Core Tags</b>” />. On running the same you can see the <b> tags being displayed on screen. The XML is displayed as a text.

Try this <c:out value =”<b>Welcome to Core Tags</b>” escapeXml = “false”/>. On running the same you can see the <b> tags are gone and the text is displayed in bold. Here the XML is being rendered. By default escapeXml is set to  true.

Technical

Life Cycle of JSP

There are seven main phases in JSP’s Life Cycle.

  1. Page Translation :-Here the jsp page is parsed and converted to a java file that contains the corresponding servlet.
  2. Page Compilation :- Here the servlet page that is created after translation is compiled to a class file.
  3. Load class :- The compiled class is loaded.
  4. Instance Creation :- An instance of the loaded class is created.
  5. jspInit Method :- The initialization method gets called before any other methods.
  6. _jspService :- The method gets called for each request.
  7. jspDestroy :- The method gets called when the ServletContainer decided to take the servlet out of service.

Implicit Objects in JSP

There are nine implicit objects in JSP

  1. application :- web application(javax.servlet.ServletContext)
  2. session :- user’s session(javax.servlet.http.HttpSession)
  3. request :- request to the page(javax.Servlet.http.HttpServletRequest)
  4. response :- sending response(javax.Servlet.http.HttpServletResponse)
  5. out :- outputstream(javax.servlet.jsp.JspWriter)
  6. page :- this(java.lang.Object)
  7. pageContext :- page(javax.servlet.jsp.PageContext)
  8. config :- configuration(javax.servlet.ServletConfig)
  9. exception :- exception(java.lang.Throwable)

MVC Architecture-Difference between Model 1 and Model 2

Model View Controller Architecture

Model 1 Architecture: - Here JSP page will be responsible for all tasks and will be the target of all requests. The tasks may include authentication, data access, data manipulation etc. The architecture is suitable for simple applications.

 

Disadvantages: – Since the entire business logic is embedded in JSP chunks of java code had to be added to the JSP page.

For a web designer, the work will be difficult as they mite face problems with business logic.

The code is not reusable.

 

Model 2 Architecture : – The servlet act as the controller of the application and will be target of every request. They analyze the request and collect data required to generate response to JavaBeans object that act as model of the application. The JSP page forms the view of the application.

 

Advantages: – Reusability

Ease of maintenance.

Designer’s need to work only with presentation of the data.

Difference between Scriptlet and Declaration

Declaration :- Used for declaring variables and methods.

example : <%!  int num =0;  %>

During translation and compilation phase of JSP life cycle all variables declared in jsp declaration become instance variables of servlet class and all methods become instance methods. Since instance variables are automatically initialized, all variables declared in jsp declaration section gets their default values.

Scriptlet:- Used for embedding java code fragments in JSP page.

example : <%  num++; %>

During translation phase of JSP Life cycle all scriptlet become part of _jspService() method. So we cannot declare methods in scriptlet since we cannot have methods inside other methods. As the variables declared inside scriptlet will get translated to local variables they must be initialized before use.

 

Writing BLOB value to table from java code

  1. Set the connection with database
  2. Create a sample table which has two column one is of type number(Sno) and the other one is of type BLOB(value)
  3. If u have a long string that you want to store in the table as BLOB, first convert the string to a byte array.                                                                                                                        
  4. byte[] bytes = new byte[content.length()]; //content is the string 
  5. bytes = content.getBytes();
  6. For storing a BLOB, insert a row into the sample table with an empty BLOB first.
  7. BLOB empty_BLOB= oracle.sql.BLOB.empty_lob();
  8. INSERT INTO sample_table(Sno,value) VALUES ( 1,?);
  9. stmt1.setBlob(1,empty_BLOB);
    stmt1.executeQuery();
    connection.commit();
  10. Now the table has one row with an empty BLOB.
  11. Now select the new row from table for updation
  12. select value AS ATTACHMENT from sample_table where Sno=1 for update
  13. Blob text = null;
         if(rs1.next())
         {
  14.       text = rs1.getBlob(“ATTACHMENT”);
  15.      }
  16.   DataOutputStream outStream = new DataOutputStream(((BLOB)text).getBinaryOutputStream());
       outStream.flush(); 
       outStream.write(bytes,0,bytes.length);//bytes is teh byte array that has the value to be stored.
       outStream.close();
       PreparedStatement stmt3=connection.prepareStatement(“update sample_table set value=? where Sno=1″);
       stmt3.setBlob(1,text);
       int update=stmt3.executeUpdate();
  17. The value is stored into the table as a BLOB
        

Why struts action is being called twice while submitting the form data

Recently I have come across an issue where the struts action class is being called twice whenever the form is submitted. Even after trying with lots of debug messages we did not get any idea about this mystery.

Then We Found it!

See our submit , try If u get any clue…

<html:submit onclick=”submitMe(‘F’)” value=”Submit” />

Here is the javascript function.

function submitMe(act)
{
document.forms[0].action.value=act;
document.forms[0].submit();
}

Got it???

just see the bold statement. We are using html:submit tag which will submit the form. But in the javascript function we have given an explicit submit too. No wonder the form is getting submitted twice!!!!!!!!!!!!!

Getting Error ‘ 403 Forbidden’ from WebLogic server after Deployment

My application was running fine in Web Logic server. After doing some modification in the project in some other PC, I copied the code to my PC, built it and deployed it in the server. Deployment was successful. But when I tried to access the application through explorer I got the following message

‘403 Forbidden
The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.’

After spending quite a lot time going through the files that I newly updated and compared it with those that were running fine earlier, I was not able to rectify it. Later I found that my web.xml was not written properly. It doesn’t have any content other than the default ones.