Unit - Iii Server Side Programming: DR S Sankar
Unit - Iii Server Side Programming: DR S Sankar
Unit - Iii Server Side Programming: DR S Sankar
PROGRAMMING
Dr S Sankar
SERVLETS
Servlets: Java Servlet Architecture
Servlet Life
Cycle- Form GET and POST actions-
Session Handling
Understanding Cookies
Servlets
What are Servlets?
Java Servlets are programs that run on a Web or Application server and act
as a middle layer between a web browser and databases
Servlets work on the server-side.
Servlets are capable of handling complex requests obtained from web server.
Servlets offer several advantages
Servlets are platform-independent because they are written in Java.
Servlets are trusted.
The full functionality of the Java class libraries is available to a servlet.
It can communicate with applets, databases, or other software via the sockets
and RMI mechanisms
•A client sends a request to a Web Server, through a Web Browser.
•The Web Server searches for the required Servlet and initiates it.
•The Servlet then processes the client request and sends the
response back to the server, which is then forwarded to the client.
Advantages of a Servlet
import javax.servlet.*;
import javax.servlet.http.*;
Servlet Life Cycle and Its Methods
Servlet Life Cycle
A servlet life cycle can be defined as the entire
process from its creation till the destruction
It has three life cycle methods
init()
service()
destroy()
init() method
The init method is called only once.
It is called only when the servlet object is created
It is used for servlet object initializations
It loads some data into object and are used through out the life cycle.
Syntax
public void init() throws ServletException
{
// Initialization code...
}
service() Method
<html> LoginForm.html
<body>
<form name="loginForm" method="post" action=“LoginServlet">
Username: <input type="text" name="username"/> <br/>
Password: <input type="password" name="password"/> <br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
How to handle HTML form data with Java Serv
let
import javax.servlet.*; LoginServlet.java
import javax.servlet.http.*;
import java.io.*;
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
resp.setContentType("text\html");
PrintWriter out = req.getWriter();
String un = req.getParameter("username");
String pwd = req.getParameter("password");
String msg = "Your username is: "+ un + "Your password is: "+ pwd;
out.println("<html>");
out.println("<body>");
out.println("<h2>" + msg + "</h2>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
Session Tracking and its techniques
Session Tracking / Handling
Session simply means amount of time user spends in web application.
Session Tracking is a way to maintain state (activity) of an user. It is
also known as session management in servlet.
Http protocol is a stateless protocol.
Since Http is stateless protocol, it does not keep the user state
(activity)in between web pages.
To recognize the user and his activity throughout the application,
session tracking is important.
Session Tracking Techniques
for(int i=0;i<c.length;i++)
{
out.print("Name: "+c[i].getName()+" and Value: "+c[i].getValue());
}
Login.html
<html>
<body>
<form action=“createcookie" method="post">
Username:<input type="text" name="username"/><br/>
Password:<input type="text" name="password" /></br>
<input type="submit" value=“login"/>
</form>
</body>
</html>
Enter username: jai and password: 1234
Click on login button.
public class CreateCookieServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
resp.setContentType("text\html");
PrintWriter out = resp.getWriter();
String un = req.getParameter("username");
String pd = req.getParameter("password");
out.println("<html><body>");
out.println(“<p> Cookies are created. Click on the below button to get
cookies<“/p>”);
out.println("<form method='post' action='GetCookieServlet'>");
out.println("<input type='submit' value=‘Get Cookie' />");
out.println("</form></body></html>");
out.close();
}
}
public class GetCookieServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletExpcetion, IOException
{
resp.setContentType("text\html");
PrintWriter out = resp.getWriter();
out.println(“<html><body><p>”);
String id = request.getParameter("id");
String name = request.getParameter("name");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "", "");
Statement st = con.createStatement();
st.executeUpdate(query);
out.println(“<html><body>Sucessfully inserted…</body></html>”);
con.close();
out.close();
}
}
JSP
Java Server Pages
Why JSP?
To separate presentation and business logic
Supports dynamic content.
JSP are extended version of Servlet.
JSP vs Servlet
JSP Life Cycle
JSP Elements
JSP Scripting tags
JSP Directive tags
JSP Action tags
JSP Scripting tag
JSP Directives tag
JSP Action tags
JSP program to work with Database
1. Create db and table
2. Create HTML file
3. Create JSP file
Import necessary packages
Register DB driver
Establish connection
Create Query
Execute Query
Process the result
Close the connection
Step 1: Creation of Database and Table in
MySQL
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String un = request.getParameter("t1");
}
}