BE-wis Lab Manual
BE-wis Lab Manual
BE-wis Lab Manual
Laboratory Manual
For
It is my great pleasure to present this laboratory manual for Final Year engineering
students for the subject of Web Information System keeping in view the vast coverage required
for Programming with Java Language
As a student, many of you may be wondering with some of the questions in your mind
regarding the subject and exactly what has been tried is to answer through this manual.
As you may be aware that MGM has already been awarded with ISO 9000 certification
and it is our endure to technically equip our students taking the advantage of the procedural
aspects of ISO 9000 Certification.
Faculty members are also advised that covering these aspects in initial stage itself, will
greatly relived them in future as much of the load will be taken care by the enthusiasm energies
of the students once they are conceptually clear.
Dr. S.D.Deshmukh
Principal
Vision of JNEC
College seeks to be the engineering college of choice in Maharashtra that
can provide the best learning experience, the most productive learning
community, and the most creative learning environment in Engineering
Education and will be recognized as one of the best Engineering Colleges
in India.
Mission of JNEC
To develop innovative engineers with human values, well equipped to
solve complex technical problems, address the needs of modern society
and pursue lifelong learning, by providing them competent, caring and
committed faculty.
IT Vision:
IT department is committed to ensure the quality education to students’
by providing innovative resources & continuous up-gradation of the
department. To achieve “Heights of Excellence” in the world we strive to
organize regular interaction with Industry and Alumni.
IT Mission:
To impart core technical competency & knowledge in students through
curriculum and certification programs to fulfill the industry requirements
which ultimately benefits society at large.
This manual is intended for the Final Year students of Information Technology in the
subject of Web Information System. This manual typically contains practical/Lab Sessions
related advanced Programming in Java covering various aspects related the subject to enhanced
understanding.
As per the syllabus along with Study of Java Language, we have made the efforts to
cover various aspects of Web Information System covering different Techniques used to
construct and understand concepts of advance Java Programming.
Students are advised to thoroughly go through this manual rather than only topics
mentioned in the syllabus as practical aspects are the key to understanding and conceptual
visualization of theoretical aspects covered in the books.
1. Make entry in the Log Book as soon as you enter the Laboratory.
2. All the students should sit according to their roll numbers starting from their left to right.
3. All the students are supposed to enter the terminal number in the log book.
5. All the students are expected to get at least the algorithm of the program/concept
to be implemented.
1. Submission related to whatever lab work has been completed should be done during the next
lab session. The immediate arrangements for printouts related to submission on the day of
practical assignments.
2. Students should be taught for taking the printouts under the observation of lab teacher.
2. Introduction to Servlet.
5. Study of cookies.
Theory:
J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the
mainframe-scale computing typical of large enterprises. Sun Microsystems (together with
industry partners such as IBM) designed J2EE to simplify application development in a
thin client tiered environment. J2EE simplifies application development and decreases
the need for programming and programmer training by creating standardized, reusable
modular components and by enabling the tier to handle many aspects of programming
automatically.
J2EE includes many components of the Java 2 Platform, Standard Edition (J2SE):
The Java Development Kit (JDK) is included as the core language package.
Write Once Run Anywhere technology is included to ensure portability.
Support is provided for Common Object Request Broker Architecture (CORBA),
a predecessor of Enterprise JavaBeans (EJB), so that Java objects can
communicate with CORBA objects both locally and over a network through its
interface broker.
Java Database Connectivity 2.0 (JDBC), the Java equivalent to Open Database
Connectivity (ODBC), is included as the standard interface for Java databases.
A security model is included to protect data both locally and in Web-based
applications.
J2EE also includes a number of components added to the J2SE model, such as the
following:
Container Types
J2EE server
The runtime portion of a J2EE product. A J2EE server provides EJB and Web
containers.
Enterprise JavaBeans (EJB) container
Manages the execution of enterprise beans for J2EE applications. Enterprise beans
and their container run on the J2EE server.
Web container
Manages the execution of JSP page and servlet components for J2EE applications.
Web components and their container run on the J2EE server.
Application client container
Manages the execution of application client components. Application clients and
their container run on the client.
Applet container
Manages the execution of applets. Consists of a Web browser and Java Plug-in
running on the client together.
Theory:
As we know that the servlet extends the HttpServlet and overrides the doGet ()
method which it inherits from the HttpServlet class. The server invokes doGet () method
whenever web server receives the GET request from the servlet. The doGet() method
takes two arguments first is HttpServletRequest object and the second one is
HttpServletResponse object and this method throws the ServletException.
Whenever the user sends the request to the server then server generates two
objects’ first is HttpServletRequest object and the second one is HttpServletResponse
object. HttpServletRequest object represents the client's request and the
HttpServletResponse represents the servlet's response.
Inside the doGet() method our servlet has first used the setContentType() method
of the response object which sets the content type of the response to text/html It is the
standard MIME content type for the html pages.After that it has used the method
getWriter () of the response object to retrieve a PrintWriter object. To display the output
on the browser we use the println () method of the PrintWriter class.
Program
Import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class hello extends HttpServlet
{ private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{ PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1 align=center>Basic servlet program</h1>");
out.println("</body>");
out.println("</html>");
}
}
Output:
Conclusion:
Hence we studied how to create a sample servlet to display date and the time.
Journal Write-up:
- HTTP protocol & its methods
- Introduction to servlet
- Servlet configuration
- Life cycle of servlet
- Requests and Responses
Theory:
• getHeaderNames
Rather than looking up one particular header, you can use the
getHeaderNames method to get an Enumeration of all header names received on
this particular request.
• getHeaders
In most cases, each header name appears only once in the request.
Occasionally, however, a header can appear multiple times, with each occurrence
listing a separate value. Accept-Language is one such example. You can use
getHeaders to obtain an Enumeration of the values of all occurrences of the
header.
Program:
Output
Journal Write-up:
- Introduction to request header
- Understanding request header
1. Accept
2. Accept-Charset
3. Accept-Encoding
4. Accept-Language
5. Authorization
6. Connection
7. Content-Length
8. Cookie 9. Host 10. User-Agent
4. Database operations
Theory:
In this program we are going to insert the data in the database from our java program
in the table stored in the database.
Inside this method call the getWriter() method of the PrintWriter class. We can
insert the data in the database only and only if there is a connectivity between our
database and the java program. To establish the connection between our database and the
java program we first need to call the method forName(), which is static in nature of the
class Class. It takes one argument which tells about the database driver we are going to
use. Now use the static method getConnection() of the DriverManager class. This
method takes three arguments and returns the Connection object. SQL statements are
executed and results are returned within the context of a connection. Now your
connection has been established. Now use the method createStatement() of the
Connection object which will return the Statement object. This object is used for
executing a static SQL statement and obtaining the results produced by it. We have to
insert a values into the table so we need to write a query for inserting the values into the
table. This query we will write inside the executeUpdate() method of the Statement
object. This method returns int value.
If the record will get inserted in the table then output will show "record has been
inserted" otherwise "sorry! Failure".
Program
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
if(i!=0)
{
out.println("The record has been inserted");
}
else
{
out.println("Sorry! Failure");
}
rs = statement.executeQuery("select * from emp_sal");
while(rs.next())
{
out.println("<p><table>" + rs.getString(1) + " "
+ rs.getInt(2) + "</p></table>");
}
rs.close();
statement.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Rajesh
5. Study of Cookies
Theory:
Cookies
Also known as browser cookies or tracking cookies, cookies are small, often
encrypted text files, located in browser directories. They are used by web developers to
help users navigate their websites efficiently and perform certain functions. Due to their
core role of enhancing/enabling usability or site processes, disabling cookies may prevent
users from using certain websites.
Cookies are created when a user's browser loads a particular website. The website
sends information to the browser which then creates a text file. Every time the user goes
back to the same website, the browser retrieves and sends this file to the website's server.
Computer Cookies are created not just by the website the user is browsing but also by
other websites that run ads, widgets, or other elements on the page being loaded. These
cookies regulate how the ads appear or how the widgets and other elements function on
the page.
Cookie Class
The getCookies() method of the request object returns an array of Cookie objects.
Cookies can be constructed using the following code:
Method Description
Returns the comment describing the purpose of this
getComment()
cookie, or null if no such comment has been defined.
getMaxAge() Returns the maximum specified age of the cookie.
getName() Returns the name of the cookie.
Returns the prefix of all URLs for which this cookie is
getPath()
targeted.
getValue() Returns the value of the cookie.
If a web browser presents this cookie to a user, the
setComment(String)
cookie's purpose will be described using this comment.
Sets the maximum age of the cookie. The cookie will
expire after that many seconds have passed. Negative
values indicate the default behavior: the cookie is not
setMaxAge(int)
stored persistently, and will be deleted when the user web
browser exits. A zero value causes the cookie to be
deleted
This cookie should be presented only with requests
setPath(String)
beginning with this URL.
Sets the value of the cookie. Values with various special
characters (white space, brackets and parentheses, the
equals sign, comma, double quote, slashes, question
setValue(String)
marks, the "at" sign, colon, and semicolon) should be
avoided. Empty values may not behave the same way on
all browsers.
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
if (newCookie)
{ out.println("Cookie Path: " + cookie.getPath() + "<br>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
private long getNextCookieValue()
{ return new java.util.Date().getTime();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{ doGet(request, response);
}
}
Output
Cookie Path: /Cookies11
Journal Write-up:
- Introduction to cookies
- Types of cookie
- Different methods used to perform operations on cookies
Theory:
Session
A session is a conversation between the server and a client. A conversation
consists of series of continuous request and response.
When there is a series of continuous request and response from a same client to a
server, the server cannot identify from which client it is getting requests. Because HTTP
is a stateless protocol.
Solution is, when a client makes a request it should introduce itself by providing
unique identifier every time. There are five different methods to achieve this.
Program
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
public class SessionTracker extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(true);
Integer count=(Integer)session.getValue("tracker.getCount");
if(count==null)
count=new Integer(1);
else
count=new Integer(count.intValue()+1);
session.putValue("tracker.count",count);
out.println("<html><head><title>Session Tracker</title></head>");
out.println("<body><h1>Session tracking demo</h1>");
out.println("you have visited this page"+count+((count.intValue()==1)?
"time":"times:"));
out.println("<p>");
out.println("<h2>Here is your session data:</h2>");
String[] names=session.getValueNames();
for(int i=0;i<names.length;i++)
{
out.println(names[i]+":"+session.getValue(names[i])+"<br>");
}
out.println("</body></html>");
}
}
Output:
Conclusion: Hence we have performed program for session tracking.
Journal Write-up:
- Introduction to session
- Session tracking methods
- The session life cycle
Theory:
Custom Tag
A custom tag is a user-defined JSP language element. When a JSP page containing
a custom tag is translated into a servlet, the tag is converted to operations on an object
called a tag handler. The Web container then invokes those operations when the JSP
page's servlet is executed.
The Java code that actually does the work of the custom tag is contained in a tag
library that you create. The tag library contains what some refer to as "tag handlers,"
which are simply Java programs that use some special servlet tag classes. The JSP
developer doesn't have to deal with the code, but instead deals with an easy-to-understand
XML-formatted tag statement.
Program:
1.CustomTag.java
package custompkg;
import javax.servlet.annotation.WebServlet;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
@WebServlet(name = "CustomTag", urlPatterns = {"/CustomTag"})
public class CustomTag extends TagSupport
{
/* Invokes when the start tag of the custom tag is encountered */
public int doStartTag() throws JspException
{
try
{
JspWriter out=pageContext.getOut();
out.println("<BR><B>Copyright RoseIndia Technologies<B>");
}
catch (Exception IOException)
{
System.err.println("IO Exception");
System.err.println("ioException.toString()");
}
/* Returning the SKIP_BODY, as the body content is not be evaluated */
return SKIP_BODY;
}
/* Invokes when the end tag of the custom tag is encountered */
public int doEndTag() throws JspException
{
/* Skip the processing of the rest of the page */
return SKIP_PAGE;
}
}
2. newtag_library.tld
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Copyright Info</short-name>
<uri>/newtag_library1.tld</uri>
<description>
Developing First Custom Tags
</description>
<tag>
<name>CTag</name>
<tag-class>custompkg.CustomTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3.index.jsp
Output:
Conclusion:
Hence we have performed program to create custom tags.
Journal Write-up:
- Introduction to custom tags
- JSP
- The taglib Directive
- The Tag Library
- The Tag Library Descriptor
- Tag Library Deployment
Program
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function validate(field,alerttxt)
{
with(field)
{
if(value==null||value=="")
{
alert(alerttxt);
return false;
}
else
{
return true;
}
}
}
function validate_form(thisform)
{
with(thisform)
{
if(validate(email,"email must be filled out!")==false)
{
email.focus();
return false;
}
}
}
</script>
</head>
<body>
<h1>Required Field Validation</h1><br/>
<form name="form1" action="valid.html" onsubmit="return validate_form(this) "
method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html>
Output
Conclusion: Hence we have performed the program for java script validations.
Journal Write-up:
- Introduction to JSP
- Java script form validations