UNIT-VI Notes Adavance Java
UNIT-VI Notes Adavance Java
UNIT-VI Notes Adavance Java
******************Servlet******************
• Servlets:-
- Web browser & servers.
- Servlets are web pages generated by the web servers in response to the
request send by the clients.
-> Sun, Netscape and Microsoft web servers offer servlet API.
-> The programs developed for this API can be moved to any of these
environment without recompiling them.
-> Java security manager will helps us to set restriction to access resources of
server machine.
-> As we know java has great collection of java class libraries and all these are
available for servlet.
• Types of Servlets:
service()
destroy()
Step-1:
- User enter URL in web browser and generates HTTP request for this URL.
Step-2:
- HTTP request received by web server. Server map this request to the
particular servlet then servlet loaded into address space of web server.
Step-3:
- Server invokes the init() method of servlet. When servlet first loaded into the
computer memory then this method will get called.
Step-4:
- Server invoke service() method. This method is used to process HTTP request.
- service() method accept the request parameter and process it and try to
create HTTP response for the client.
- servlet available in server address space and it is able to process each HTTP
request.
Step-5:
- Server may take decision to unload the servlet from its memory. Server will
use algorithm to take this decision.
- The server will invoke destroy() method to released the resources which are
allocated for the servlet.
2. Create a Servlet
Here, we are going to use apache tomcat server in this example. The steps are
as follows:
2. Create a Servlet
Diagram:-
As you can see that the servlet class file must be in the classes folder. The
web.xml file must be under the WEB-INF folder.
2) Create a Servlet:-
There are three ways to create the servlet.
The HttpServlet class is widely used to create the servlet because it provides
methods to handle http requests such as doGet(),doPost, etc.
In this example we are going to create a servlet that extends the HttpServlet
class. In this example, we are inheriting the HttpServlet class and providing the
implementation of the doGet() method. Notice that get request is the default
request.
DemoServlet.java:-
1. set classpath
Put the java file in any folder. After compiling the java file, paste the class file
of servlet in WEB-INF/classes directory.
There are many elements in the web.xml file. Here is given some necessary
elements to run the simple servlet program.
web.xml file
There are too many elements in the web.xml file. Here is the illustration of
some elements that is used in the above web.xml file. The elements are as
follows:
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
res.setContentType("text/html");
PrintWriter=res.getWriter();
String name=req.getParameter("nm");
pw.print("<html><body>");
pw.print("<br>Welcome"+name);
pw.print("</body></html>");
pw.close();
Web.xml
<web-app>
<servlet>
<servlet-name>SVMP14</servlet0-name>
<servlet-class>HttpServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SVMP14</servlet-name>
<url-pattern>/college</url-pattern>
</servlet-mapping>
</web-app>
loginPage.html
<html>
<body>
<form name="myform"
Action=""http://localhost:9696/SVMP14/college" method="GET">
</form>
</body>
</html>
********************************************************
• Cookies in Servlet:
A cookie is a small piece of information that is persisted between the multiple
client requests.
of the browser. After that if request is sent by the user, cookie is added with
request by default. Thus, we recognize the user as the old user.
Diagram:-
• Types of Cookie:-
There are 2 types of cookies in servlets.
1. Non-persistent cookie
2. Persistent cookie
• Non-persistent cookie:-
It is valid for single session only. It is removed each time when user closes the
browser.
• Persistent cookie:-
It is valid for multiple session. It is not removed each time when user closes
the browser. It is removed only if user logout or signout.
• Advantage of Cookies:-
1. Simplest technique of maintaining the state.
• Disadvantage of Cookies:-
• Cookie class:-
javax.servlet.http.Cookie class provides the functionality of using cookies. It
provides a lot of useful methods for cookies.
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++)
1)
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
res.setContentType("text/html");
String name=req.getParameter("cname");
String value=req.getParameter("cvalue");
res.addCookie(ck);
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("</body></html>");
pw.close();
Web.xml
<web-app>
<servlet>
<servlet-name>vjtech</servlet0-name>
<servlet-class>AddCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vjtech</servlet-name>
<url-pattern>/ajp</url-pattern>
</servlet-mapping>
</web-app>
HomePage.html
<html>
<body>
</form>
</body>
</html>
2)
• GetCookieServlet-
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
res.setContentType("text/html");
Cookie ck[]=req.getCookies();
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
for(int i=0;i<ck.length;i++)
pw.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
pw.println("</body></html>");
pw.close();
Web.xml
<web-app>
<servlet>
<servlet-name>vjtech1</servlet-name>
<servlet-class>GetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vjtech1</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
</web-app>
********************************************************
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import java.util.*;
HttpSession hs=req.getSession(true);
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
Date d1=(Date)hs.getValue("date");
Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674).
UNIT-VI Servlets
pw.println("</body></html>");
pw.close();
Web.xml
<web-app>
<servlet>
<servlet-name>vjtech2</servlet0-name>
<servlet-class>DateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vjtech2</servlet-name>
<url-pattern>/getsession</url-pattern>
</servlet-mapping>
</web-app>
******************************************************
• JSP:
- JSP stands for Java Server Page.
<html>
<body>
<%
if(request.getParameter("name")==null)
else
out.println("Hello
"+request.getParameter("name"));
%>
</body>
</html>
- A JSP directive begin with <%@ and ends with %>. We use directive like
<%@ varname="value" %>
1) content_type:
2) import:
3) extends:
4) implements:
5) method:
6) language:
1) Compilation
2) Initialization
Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674).
UNIT-VI Servlets
3) Execution
4) Cleanup
• JSP Compilation:-
- Here, we can compile the jsp page.
• JSP Initialization:-
- It is used to initialize database connection, create lookup tables and open
files, etc.
• JSP Execution:-
In this phase, JSP engine trying to process request and will try to generate
response.
//body
• JSP Cleanup:-
-In this phase, JSP got removed from the container. it means, whatever the
resources allocated for the JSP got removed.
VJTech Academy…