Wednesday, January 7, 2015

How to Configure database credential in context.xml file and read it using Hibernate configuration file


Put your context.xml file in the META-INF directory



context.xml

<?xml version="1.0" encoding="UTF-8"?>
<context>
<Resource name="jdbc/dbresource" auth="Container"
        type="javax.sql.DataSource" username="imusername" password="impassword"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@//IP:1521/mydb"
        maxActive="10" maxIdle="4" />
</context>

Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="hibernate.connection.datasource">java:/comp/env/jdbc/dbresource</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>

<!-- Resource mapping -->
<mapping class="com.raj.UserData"/>
  </session-factory>

</hibernate-configuration>


Keep Visiting :)
Write your comment 

Tuesday, January 6, 2015

For Performance of Code

“During development sometimes developers argue that they are just adding a very small feature and it will take no time at all (especially if the feature
isn’t used). And then other developers on the same project make the same claim, and
suddenly the performance has regressed by a few percent. The cycle is repeated in the
next release, and now program performance has regressed by 10%. A couple of times
during the process, performance testing may hit some resource threshold—a critical
point in memory use, or a code cache overflow, or something like that. In those cases,
regular performance tests will catch that particular condition and the performance team
can fix what appears to be a major regression. But over time, as the small regressions
creep in, it will be harder and harder to fix them.”
- By Scott Oaks (Java Perfomrnace: The Definitive Guide )

Tuesday, December 30, 2014

JAVA CODE TO GET COMPLETE JVM DETAIL

import java.io.*;

class JVMFlight{
public static void main(String bag[]){
System.out.println("--start--");

StringWriter stringBuffer = new StringWriter();
PrintWriter out = new PrintWriter(stringBuffer);

out.println();
out.println();
out.println("    java.home: " + System.getProperty("java.home"));
out.println("    java.vendor: " + System.getProperty("java.vendor"));
out.println("    java.version: " + System.getProperty("java.version"));
out.println("    java.vm.name: " + System.getProperty("java.vm.name"));
out.println("    java.vm.version: " + System.getProperty("java.vm.version"));
out.println("    os.name: " + System.getProperty("os.name"));
out.println("    os.arch: " + System.getProperty("os.arch"));
out.println("    os.version: " + System.getProperty("os.version"));
out.println("");

System.out.println(stringBuffer.toString());
System.out.println("--end--");
}
}

Monday, December 22, 2014

Difference between sendRedirect() vs forward()

sendRedirect()

response.sendRedirect();

sendRedirect can let the servlet to move on to a completely new Application Url

Need response object to call the method.

It takes the string url path as an Argument. The value may be a complete url path or a relative path

Created new request and response object to the redirected URL

Assume that the client originally typed in:
http://www.myworld.com/com/raj/info.do

When the request comes into the servlet named “info.do”, 
the servlet calls sendRedirect() with a relative URL that does NOT start with a forward slash:
sendRedirect(“personal/Information.html”); // NO SLASH AT THE BEGINNING

The Container builds the full URL (it needs this for the “Location” header it
puts in the HTTP response) relative to the original request URL:
http://www.myworld.com/com/raj/personal/Information.html

But if the argument to sendRedirect() DOES start with a forward slash:
sendRedirect(“/personal/Information.html”); // SLASH AT THE BEGINNING

The Container builds the complete URL relative to the web Container itself, instead
of relative to the original URL of the request. So the new URL will be:
http://www.myworld.com/personal/Information.html

sendRedirect() change the Url as per the usage of the string value in the argument

Browser created new request object to the redirected url

.forward()

RequestDispatcher view = request.getRequestDispatcher(“Process.jsp”);
view.forward(request,response);

Need RequestDispatcher object with the string Url value as an Argument

Need to pass request, response object into the forward method

Takes the same (existing) request and response object to the redirected URL

The Url is not getting changed in the browser location bar.

JSP page vs PageContext

page

class: java.lang.Object
page represents the current servlet/jsp object.
page represents 'this'.

pageContext

class: javax.servlet.jsp.PageContext
Use to foward/redirect to particular url.
Use to include particular url.
Use to store/retrieve page level values.
It provides the following list of objects 

ServletRequest
ServletResponse
ServletContext
ServletConfig
HttpSession

application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
request = pageContext.getRequest();
response = pageContext.getResponse();
out = pageContext.getOut(); 


It provides following list of constants

public static final String APPLICATION
public static final int APPLICATION_SCOPE
public static final String CONFIG
public static final String EXCEPTION
public static final String OUT
public static final String PAGE
public static final int PAGE_SCOPE
public static final String PAGECONTEXT
public static final String REQUEST
public static final int REQUEST_SCOPE
public static final String RESPONSE
public static final String SESSION
public static final int SESSION_SCOPE

Object obj = pageContext.getAttribute("BeginnersBook", PageContext. REQUEST_CONTEXT);
Object obj = pageContext.getAttribute("BeginnersBook", PageContext. PAGE_CONTEXT);
Object obj = pageContext.getAttribute("BeginnersBook", PageContext. APPLICATION_CONTEXT);
If it finds the attribute it would assign it to Object obj else it would return Null.

pageContext.setAttribute(“userName”, “RAJ”, PageContext.APPLICATION_CONTEXT);
pageContext.removeAttribute(“userName”, PageContext.APPLICATION_CONTEXT);

Keep Visiting :)
Give your valuable Comment

EL Expression Implicit Objects/Variables


  1. pageContext
  2. pageScope
  3. requestScope
  4. sessionScope
  5. applicationScope
  6. param
  7. paramValues
  8. header
  9. headerValues
  10. cookie
  11. initParam
Keep Visiting :)
Give your valuable Comment


JSP ELEMENTS

PAGE DIRECTIVE 

         <%@ page import=”java.util.*” %>

DECLARATION

          <! int num = 10;>

SCRIPLET

          <% out.println(num) %>

EXPRESSION

          <%=num%>
          <%= pageContext.getAttribute(“foo”) %>

ACTION

          <jsp:include page=”foo.html” />

EL EXPRESSION

           ${pageContext.request.contextPath}
           ${pageContext.foo}

JSP USEBEAN

<jsp:useBean id="" class="" type="" scope="">BODY</jsp:useBean>

*id = Unique Name of the Bean (i.e. Object Name)<br>

 type (reference type) = Type of the Bean<br>

*class (object type) = Class of the Bean<br>

Scope = Scope of the Bean (i.e. page, request, session, application)<br>

i.e. Type id = new Class();


jsp:useBean =  
Create the new object of the specified class. with type specified in the type="" attribute. First it will find the object in the specified scope. and if it is not available then it will create the object and will store the object in the specified scope.  
Body gets evaluated only if new object is getting created.

<jsp:getProperty name=”beanId” property=”name” />

using get tag we can get particular property of the bean object.

<jsp:setProperty name=”beanId” property=”name” value=”Fred” />
using set tag we can set particular property value for the bean object

Keep Visiting :)
Give your valuable Comment



Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...