Unit V Ajava - Update
Unit V Ajava - Update
Class.forName(“oracle.jdbc.driver.OracleDriver”);
2. 2-B DriverManager.registerDriver()
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver())
• Step 3: Establish a connection using the Connection class object
Connection con =
DriverManager.getConnection(url,user,password)
class XYZ {
public static void main(String[] args) throws Exception
{
String url
= "jdbc:mysql://localhost:3306/table_name"; // table details
String username = "root"; // MySQL credentials
String password = "123";
String query
= "select *from students"; // query to be run
Class.forName(
"com.mysql.cj.jdbc.Driver"); // Driver name
Connection con = DriverManager.getConnection(
url, username, password);
System.out.println(
"Connection Established successfully");
Statement st = con.createStatement();
ResultSet rs
= st.executeQuery(query); // Execute query
rs.next();
String name
= rs.getString("name"); // Retrieve name from
db
• Create Statement
• Prepared Statement
• Callable Statement
.
Servlets
• Servlet technology is used to create a web application (resides at
server side and generates a dynamic web page).
• Servlet technology is robust and scalable because of java language.
Before Servlet, CGI (Common Gateway Interface) scripting language
was common as a server-side programming language.
There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc
• Servlet is a technology which is used to create a web application.
• Servlet is an API that provides many interfaces and classes including
documentation.
• Servlet is an interface that must be implemented for creating any
Servlet.
• Servlet is a class that extends the capabilities of the servers and
responds to the incoming requests. It can respond to any requests.
• Servlet is a web component that is deployed on the server to create a
dynamic web page.
Advantages of Servlet
1.Better performance: because it creates a thread for each
request, not process.
2.Portability: because it uses Java language.
3.Robust: JVM manages Servlets, so we don't need to worry about the
memory leak, garbage collection, etc.
4.Secure: because it uses java language.
Life Cycle of a Servlet
The entire life cycle of a Servlet is managed by the Servlet
container which uses the javax.servlet.Servlet interface to understand
the Servlet object and manage it.
Stages of the Servlet Life Cycle:
• Loading a Servlet.
• Initializing the Servlet.
• Request handling.
• Destroying the Servlet.
Servlet Life Cycle Methods
There are three life cycle methods of a Servlet :
• init()
• service()
• destroy()
GenericServlet class
• GenericServlet class
implements Servlet, ServletConfig and Serializable interfaces. It
provides the implementation of all the methods of these interfaces
except the service method.
• GenericServlet class can handle any type of request so it is protocol-
independent.
Methods of GenericServlet class
1.public void init(ServletConfig config)
2.public abstract void service(ServletRequest request,
ServletResponse response)
3.public void destroy()
4.public ServletConfig getServletConfig()
5.public String getServletInfo()
6.public void init()
import java.io.*;
import javax.servlet.*;
public class First extends GenericServlet{
public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");
}
}
Difference between Servlet and JSP
Servlet JSP
Writing code for servlet is harder than JSP as it is HTML in java. JSP is easy to code as it is java in HTML.
Servlet plays a controller role in the ,MVC approach. JSP is the view in the MVC approach for showing output.
Servlet is faster than JSP. JSP is slower than Servlet because the first step in the JSP lifecycle is the
translation of JSP to java code and then compile.
Servlet can accept all protocol requests. JSP only accepts HTTP requests.
In Servlet, we can override the service() method. In JSP, we cannot override its service() method.
In Servlet by default session management is not enabled, user have to enable it In JSP session management is automatically enabled.
explicitly.
The Lifecycle of a JSP Page
The JSP API
• The JSP API consists of two packages:
1.javax.servlet.jsp
2.javax.servlet.jsp.tagext
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two
interfaces are as follows:
3.JspPage
4.HttpJspPage
JSP Scripting elements
• scriptlet tag
• expression tag
• declaration tag
File: index.html
1.<html>
2.<body>
3.<form action="welcome.jsp">
4.<input type="text" name="uname">
5.<input type="submit" value="go"><br/>
6.</form>
7.</body>
8.</html>
File: welcome.jsp
1.<html>
2.<body>
3.<%
4.String name=request.getParameter("uname");
5.out.print("welcome "+name);
6.%>
7.</form>
8.</body>
9.</html>