Hibernate is an Open Source Object Relational mapping framework,
Using Hibernate you can add, edit, delete objects to and from a database.
Why O/R mapping? - Why Object/Relational mapping?
1). Productivity
Hibernate help the developer to write complex SQL and no need JDBC API for result set and data handling. So developer takes more concentrate on business logic and increase productivity.
2). Maintain Ability
Hibernate helps to reduce the coding. So a system with less code is easier to maintain.
3). Free – Cost Effective
Hibernate is free and open source – Cost Effective
4). Learning curve is short
5). Code generation tool
Hibernate tools provided by community helps for developer to generate or develop hibernate application very fast and easy.
Hibernate to run
Real time Example: to make coffee.
Requirements:
Real time example, we are going to prepare coffee. What are the requirements are needed to make coffee.
Someone disable cookies in their browser, when we can't use cookies to manage session in jsp. at that time, we use URLRewriting to manage session in jsp.
We can also maintain session using Servlet API. All browser support servlet API for session management. HttpSession interface support session management. That mean, Servlet API provides HttpSession interface to implement session tracking. The HttpSession interface have many methods.
1). Get creating time: getCreationTime()- it returns the creation time of the session.the time is returned as millisecongs since midnight of January 1, 1970.
2). Get session ID: getId()- it returns session ID for each session.
3). Get last accessed time:getLastAccessedTime(): it return the client;s last request using current session ID.
4). Get values of the session: getValueNames(): it returns the array of all values that are bound into the current session.
5). Get specific value:getValue()- it returns the specific object that are bound to the current session.
Problem:
They are four files: 1) session1.jsp 2). session2.jsp 3). session3.jsp 4). display.jsp
Screen shot:
session1.jsp
Session2.jsp
Session3.jsp
Procedure:
Step 1:
Create Project (Already You have not Project).
Step 2:
Create Folder for storing jsp
Step 3:
Create jsp file session1.jsp
Include following coding into the session1.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Session API < /title >
< /head >
< body >
< form action="http://localhost:8084/SessionAPI/jsp/session2.jsp" method="post" >
FirstName:< input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Create another jsp file session2.jsp
Include following coding into the session2.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Session API < /title >
< /head >
< body >
< %
String fname=request.getParameter("fname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f1",fname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/session3.jsp" method="post" >
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Session API < /title >
< /head >
< body >
< %
String mname=request.getParameter("mname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f2",mname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/display.jsp" method="post" >
LastName < input type="text" name="lname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Create another jsp file display.jsp
Include following coding into the display.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Session API < /title >
< /head >
< body >
< %
String lname=request.getParameter("lname");
Cookies are small information that is send to the server from browser for later use. When we manage session in jsp, cookies are used to manage session. There are four ways to manage the session. In this lesson, we will learn how to create cookies and how to use cookies in jsp. Cookie is encapsulated in javax.servlet.http.Cookie.
Create cookies:
Cookie c=new Cookie(“CookieName”,”CookieValue”);
response.addCookie(c);
Retrieve cookies:
Cookie[ ] display=request.getCookies();
Kill cookies:
Problem:
There are three files. 1). cookie.jsp 2). cookie2.jsp 3). cookie3.jsp
cookie.jsp have one text box to get firstname and click OK the control goes to cookie2.jsp. cookie2.jsp have one text box to get lastname and click OK the control goes to cookie3.jsp. where we will display first name and last name .
Screen shot:
cookie.jsp
cookie2.jsp
cookie3.jsp
Procedure:
Step 1:
Create Project named as CookiesPro
Step 2:
Create Folder named as jsp
Step 3:
3.1). Create jsp file named as cookie.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< br > < br >
< form action="http://localhost:8084/CookiesPro/jsp/cookie2.jsp" method="post" >
First Name: < input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
3.2). Create another jsp file named as cookie2.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b > < br >
< %
% >
< form action="http://localhost:8084/CookiesPro/jsp/cookie3.jsp" method="post" >
Last Name < input type="text" name="lname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< html >
3.3). Create another jsp file named as cookie3.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< %
3.4). Create another jsp file named as display.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP-Display all Cookie < /title >
< /head >
< body >
< %
Cookie[] dis=request.getCookies();
for(int i=0;i
{
out.println(dis[i].getName()+":"+dis[i].getValue());
out.println("");
% >
< br >
< %
}
% >
< /body >
< /html >
From example, the hidden filed’s value such as sureshkumar is passed with HTTP request. From the next page, we get this value by using request.getParameter() method.
Problem:
A.jsp file have one textbox to get Firstname.
B.jsp file have one textbox to get Lastname.
Result.jsp file display or print Firstname and Lastname.
Solution:
Already we have seen this program without session. So we only B.jsp form value. We get A.jsp value as null.
Procedure:
Step1:
Create Project name SessionHidden(Already we have seen how to create project for web development)
Step 2:
Create Folder named jsp to save jsp files.
Step 3:
Create jsp file named A.jsp
A.jsp have one textbox and button.
Screenshot
Create another jsp file named B.jsp
Screenshot
Step 4:
Include following coding into A.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > A JSP Page < /title >
< /head >
< body >
< form method="post" action=http://localhost:8084/SessionHidden/Jsp/B.jsp >
Firstname < input type="text" name="fname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Step 5:
Include following coding into B.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > Result JSP Page < /title >
< /head >
< body >
< %
The HTTP (Hypertext Transfer Protocol) was designed for transferring files over the World Wide Web (WWW). HTTP uses simple communication model. A client who uses browser to send request for some files, the server responds the request. For example, the client request username and the server send response the username to client. After completing the response, the communication connection is complete. And then the client request another resource, the server has no way of distinguishing it from any other client. So HTTP is called Stateless protocol.
Session Tracking:
1). Hidden Form Fields.
2). URL rewriting
3). Cookies
4). HTTP session API
Example:
Letus go for understanding the use of session.
Consider three files A.jsp and B.jsp and Result.jsp
A.jsp file have one textbox to get FirstName as input and OK button.
Screen Shot below
B.jsp file have one textbox to get Lastname as input and OK button.
Screen Shot below
In Result.jsp, we get firstname from A.jsp and lastname from B.jsp and also display the firstname and lastname in Result.jsp..
Procedure:
Step1:
Create Project SessionPro.(Already we have seen how to create Project for web application.
Step 2:
Create Folder ,folder name is jsp to save jsp file
Jsp---- for jsp files
Step 3:
Creating A.jsp
Already I have explained how to create jsp file. So you follow that ways to create jsp file.
Include following coding into the A.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
< title > A JSP Page < /title >
< /head &g
< body gt
< h1 > A JSP Page < /h1 >
< form method="post" action="http://localhost:8084/SessionPro/Jsp/B.jsp" >
FirstName < input type="text" name="Fname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Step 4:
Creating B.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > B JSP Page < /title >
< /head &g
< body gt
< h1 > B JSP Page < /h1 >
< form method="post" action="http://localhost:8084/SessionPro/Jsp/Result.jsp" >
LastName < input type="text" name="Lname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
step 5:
Creating Result.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > Result JSP Page < /title >
< /head >
< body >
< %
String firstname=request.getParameter("Fname");
String secondname=request.getParameter("Lname");
out.println(“Firstname ”+firstname);
out.println(“ < br >”);
out.println(“Lastname ”+secondname);
% >
< /body >
< /html >
Step 6:
Run the A.jsp
You give Firstname as your desired.
I will give Firstname as suresh
OK
The control go to B.jsp Because we specify the path in action.
In B.jsp I give Kumar as Lastname. And Click OK.
The control goes to Result.jsp.
In Result.jsp,we use predefined objct request and their method getParameter().
Request.getParameter() – used to get Form Field value from one page to another page.
Explain:
We can see null and Kumar.
Lastname will be displayed only. Firstname is null.
Why?
We did not use any session management. Because
HTTP is a stateless Protocol. Only It maintains states between two page.
We want to maintain state many pages, we use session tracking.
There are four type of session tracking.
1). Hidden Form Field
2). URL Rewriting
3). Cookies
4). HTTP Session API
Next post we will use Hidden Form Field to maintain states.
NOTE: We have not used any session management so far. Consider above example without session management.