You are on page 1of 8

Deployment Descriptor

====================
Session Information
================
- The session information will also be contained in web.xml or deployment descri
ptor file.
- This tag will tell how much time the a client can be idle. So if the client id
le beyond that time then the session will be expired.
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
- This tag says that the application will wait for the client for 30 minutes if
after that also still if it is idle then automatically the session will expire.
session.setMaxInactiveInterval(no_of_seconds);
Servlet Context Parameter
======================
- This is used when we want any information need to be shared to all the compone
nts of the project.
- The context object for the whole application will be one. That is only one obj
ect gets created for the whole application.
- So whatever the value can be stored in context we can share it anywhere in the
application.
- Using this object we can access the parameters which are stored in web.xml as
context parameter.
To create the ServletContext object we can use the following code.
ServletContext context = this.getServletContext();
- Here this means the current object of the servlet.
- To access the values which are in web.xml as a context parameter we can use th
e following code.
Enumeration enum1=context.getInitParameterNames()
Add the Context Parameter in web.xml
=============================
<context-param>
<description>This driver for the odbc based client</description>
<param-name>driver</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
<context-param>
<param-name>protocol</param-name>

<param-value>jdbc:odbc:dsnname</param-value>
</context-param>
- Now this context Parameter can be accessed in Servlet or jsp using ServletCont
ext object.

Demo1
FirstContext.java
===============
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class FirstContext extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletRespons
e response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
out.println("<html>");
out.println("<body>");
ServletContext ctx=this.getServletContext();
Enumeration enum1=ctx.getInitParameterNames();
while(enum1.hasMoreElements())
{
String param_name=(String)enum1.nextElement();
String param_value=ctx.getInitParameter(param_name);
out.println("<center>");
out.println("Param Name :"+param_name);
out.println(" Param Value :"+param_value);
out.println("<br/>");
out.println("</center>");
}
out.println("<center>");
out.println("<a href='SecondContext'>Second Context</a>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse respons
e)

throws ServletException, IOException


{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse respon
se)
throws ServletException, IOException
{
processRequest(request, response);
}
}
SecondContext.java
==================
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SecondContext extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletRespons
e response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
out.println("<html>");
out.println("<body>");
ServletContext ctx=this.getServletContext();
Enumeration enum1=ctx.getInitParameterNames();
while(enum1.hasMoreElements())
{
String param_name=(String)enum1.nextElement();
String param_value=ctx.getInitParameter(param_name);
out.println("<center>");
out.println("Param Name :"+param_name);
out.println(" Param Value :"+param_value);
out.println("</center>");
}
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse respons
e)
throws ServletException, IOException {

processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse respon
se)
throws ServletException, IOException {
processRequest(request, response);
}
}

ServletConfig Parameter - Initialization Parameter


======================================
- This ServletConfig object will be created per each servlet and jsp.
- This object can get the values from the deployment descriptor which has stored
as the initialization parameter for the servlet.
- This parameter are specific to a servlet hence it can be accessible to only th
at servlet.
- To create the ServletConfig object we have different ways.
ServletConfig config = this.getServletConfig();
OR
ServletConfig config;
public void init(ServletConfig config1)
{
config=config1;
}
How the initialization Parameter will be stored in Deployment Descriptor
=====================================================
<servlet>
<servlet-name>FirstServletConfig</servlet-name>
<servlet-class>FirstServletConfig</servlet-class>
<init-param>
<param-name>company</param-name>
<param-value>NIIT Cloud Campus</param-value>
</init-param>
</servlet>
Demo 2
===========
FirstServletConfig.java
=====================
import
import
import
import

java.io.IOException;
java.io.PrintWriter;
javax.servlet.*;
javax.servlet.http.*;

public class FirstServletConfig extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletRespons
e response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
out.println("<html>");
out.println("<body>");
ServletConfig config=this.getServletConfig();
String param_value=config.getInitParameter("company");
out.println("<center>");
out.println("Param Name: company");
out.println("Param Value: "+param_value);
out.println("<center>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse respons
e)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse respon
se)
throws ServletException, IOException {
processRequest(request, response);
}
}
web.xml
=========
<servlet>
<servlet-name>FirstServletConfig</servlet-name>
<servlet-class>FirstServletConfig</servlet-class>
<init-param>
<param-name>company</param-name>
<param-value>NIIT Cloud Campus</param-value>
</init-param>
</servlet>

Chapter 7 - Working with JSP


=========================
- A jsp page is just like a html page which will have java code embedded in it.
- The java code which we are embedding using Scriplets.

- There are lot of component which we can find in case of jsp page they are
1. Directives

2.Scriplets

3.Action Tags

4. Built-in or Implicit Objects

Scriplet
===========
Three different scriplets which we find in jsp page they are
1. Declaration Scriplet : <%! code goes here %>
- These scriplet will be used for declaring variable and this will be added befo
re the service() method.
- All the variables which are required as a global variable then we can declare
it in declaration scriplet.
Example
<%!
Connection conn;
ResultSet rs;
%>
2. Normal Scriplet

: <% code goes here %>

- Whatever processing code is required for that jsp page then we can write in no
rmal scriplet.
- This code will be embedded into the service() method.
Example
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:dsn_name");
%>
3. Expression Scriplet : <%= expression %>
- This scriplet basically used for adding the expression.
- And this expression result directly will be added to the output.
Example
<%=(10+20)%>
Directives
==================
- There are three different type of directives which are available in jsp pages.
1. page
1.Page directive
================

2.taglib

3.include

- The basic requirement for the jsp page can be implemented using page directiv
e.
- This directive is common is all the jsp pages.
- There are different attribute or properties are available in page directive th
ey are
1. contentType - This attribute will contain text/html or any other type which t
he client browser will support.
2. import - This attribute will contain what ever the packages required for the
jsp page that we can assign here.
3.session - This is a boolean property so it will take true or false value . If
it is true that mean it will support session else it will not support.
4.errorpage - This attribute will contain the errorpage address. So if any error
occurs in this jsp then it will redirect to the errorpage address.
5. isErrorPage - This is a boolean property so it will take true or false value.
True denotes that the current jsp page is a errorpage and false denotes that it
is not an error page.
6. isElIgnored - This property will specify to make the page as el to be ignored
or should recognize.
Syntax
===========
<% page contentType="text/html" import="java.util.*,java.sql.*"%>
Tomorrow - MR - Lab@home Till 5 th Chapter
Thursday - 26/05/2016 - CR - @9 A.M

You might also like