Unit - 6
Unit - 6
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
Output:
Welcome to jsp
<html>
<body>
<%
out.print(2*5);
%>
</body>
</html>
Output:
10
Add two number
<html>
<head><title>Sum of two number</title></head>
<body>
<form method="post" action="addition.jsp">
<br>First NO <input type="text" name="no1" value="">
<br>Second NO <input type=”text” name="no2" value="">
<br><input type="submit" value="Add" name="submit">
</form>
</body>
</html>
<html>
<head><title>Sum of two number</title></head>
<body>
Sum of number <br>
<%
int x,y;
x=Integer.parseInt(request.getParameter("no1"));
y=Integer.parseInt(request.getParameter("no2"));
out.print(x+y);
%>
</body>
</html>
JSP implicit objects
There are 9 jsp implicit objects. These objects
are created by the web container that are available to all
the jsp pages.The JSP container automatically
instantiates these object while writing the script
content
Types of JSP implicit objects:
There are 9 jsp implicit objects which are given below.
1) JSP out implicit object
For writing any data to the buffer, JSP provides an
implicit object named out. It is the object of
JspWriter.
Example of out implicit object
<html>
<body>
<% out.print("welcome to nepal"); %> </body>
</html>
2) JSP request implicit object
The JSP request is an implicit object of type HttpServletRequest i.e.
created for each jsp request by the web container. It can be used to get
request information such as parameter, header information, remote
address, server name, server port, content type, character encoding etc.
It can also be used to set, get and remove attributes from the jsp request
scope.
Example of JSP request implicit object
request.html
<form action=“request.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br>
</form>
request.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
3) JSP response implicit object
In JSP, response is an implicit object of type
HttpServletResponse. The instance of HttpServletResponse
is created by the web container for each jsp request.
It can be used to add or manipulate response such as
redirect response to another resource, send error etc.
Example of response implicit object
index.html
<form action=“response.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
response.jsp
<%
response.sendRedirect("http://www.google.com");
%>
4) JSP config implicit object
A config object is a configuration object of a servlet
that is mainly used to access and receive configuration
information such as servlet context, servlet name,
configuration parameters, etc. It uses various methods
used to fetch configuration information.
5) JSP application implicit object
An application object is another implicit object
implemented to initialize application-wide parameters
and maintain functional data throughout the JSP
application.
6) session implicit object
A session object is the most commonly used implicit object
implemented to store user data to make it available on
other JSP pages until the user's session is active.
The session implicit object is an instance of
a javax.servlet.http.HttpSession interface.
This session object has different session methods to
manage data within the session scope.
7) pageContext implicit object
In JSP, pageContext is an implicit object of type
PageContext class.The pageContext object can be used to
set,get or remove attribute from one of the following
scopes:
request
session
application
8) Page implicit object
A page object is an implicit object that is referenced to the
current instance of the servlet. You can use it instead.
Covering it specifically is hardly ever used and not a
valuable implicit object while building a JSP application.
9) exception implicit object
An exception implicit object is implemented to handle
exceptions to display error messages.
The exception implicit object is an instance of
the java.lang.Throwable class.
It is only available for JSP pages, with the isErrorPage value
set as "True". This means Exception objects can only be
used in error pages.
Java JDBC
JDBC stands for Java Database Connectivity. JDBC is a
Java API to connect and execute the query with the
database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the
database.
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with
the database using JDBC. These steps are as follows:
1) Register the Driver class
2) Create connection
3) Create statement
4) Execute queries
5) Close connection
1) Register the driver class
The forName() method of Class is used to register
the driver class. This method is used to dynamically
load the driver class.
Syntax of forName() method
public static void forName(String className)throws ClassNotFoundException
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","root");
//here test is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
*********THE END********