Posts Tagged ‘web application’

Declare Servlet Init Parameters For JSP Page In web.xml File JSP Initialization FAQ

How To Declare Initialization Parameters For JSP Page In web.xml file

Servlet initialization parameters are declared  inside web.xml page and are used to pass the value to the servlet as a part of configuration.Now the question comes that since we do not make an entry for JSP inside web.xml page,how to pass the init parameters to the JSP page.We should remember that a JSP page is a servlet at the end of the day.A JSP is ultimately converted into a servlet then only it processes requests.Like the entry for init parameters in web.xml in case of servlet we make an entry for the JSP page as well,but with a slight difference.The web.xml file looks like this :

<servlet>
    <servlet-name>myJSP</servlet-name>
    <jsp-file>/MyJsp.jsp</jsp-file>
    <init-param>
            <param-name>paramName</param-name>
            <param-value>paramValue</param-value>
     </init-param>
</servlet>
<servlet-mapping>
     <servlet-name>myJSP</servlet-name>
      <url-pattern>/MyJsp.jsp</url-pattern>
</servlet-mapping>

Here we can find that the same way as we declare  init paramters for servlet within the servlet tags,the same way we make an entry for the JSP page so that the init parameters can be declared for that particular JSP page.The only difference is that instead of he <servlet-class> tag we use <jsp-file> tag.The url pattern for the JSP page is same as the JSP file name as we use the JSP file name as URL pattern in the request.Inside the <jsp-file> tag we use the JSP file name with .jsp extension because we need to access the JSP page and not the servlet class.

This is the way we declare servlet init parameters for the JSP page inside the web.xml file.We call it as servlet init parameters for JSP page because these init parameters are sent to the servlet which is the result of translation of the JSP page.And these are used as the configuration parameters for the translated servlet.Now the init parameters can be accessed in he JSP page code.

Number of View :2300

Tags: , , , , , , ,

No Comments


Types Of Attributes In Servlet Servlet Scopes And Attributes Saving Data In A Servlet Attributes Interview Question

Types Of Attributes In Servlet

 

An attribute is an object used to pass some value from one servlet to another.An attribute in a servlet can be set at various scopes depending upon the visibility in the web application.As we know that there are three scopes at servlet level in the application.Theses scopes are :

1.Request

2.Session

3.Application

An attribute can be set on either of the scopes depending on the situation we want access the attribute value either at request,session or application level.

When we want the value of an attribute o be accessed at request level we use request attributes and likewise for session and application.

Types of attributes in servlet can be explained on the basis of scope it belongs to :

1.Request Attributes

2.Session attributes

3.Application Attributes

 

1.Request Attributes can be set or get as demonstrated below in the code :

req.setAttribute("name","value");
req.getAttribute("name");

2.Session Attributes  can be set or get as shown below :

req.getSession(false).setAttribute("name","value");
req.getSession(false).getAttribute("name");

3.Application Attributes can be set or get as below :

getServletContext().setAttribute("name","value");
getServletContext().getAttribute("name");

Now comes the question where to use what?

Request Attributes : If we want to retain or communicate  a value across servlets where request does not change,we use request attributes.This can be the situation where include or forward is happening between the servlets in a web application.In such cases where we want to communicate a value across servlets we can use request attributes which when set from one servlet can be get in other servlets.

Session Attributes : In cases where user specific data has to be retained or communicated, we use session attributes.We use session attributes when we do not want a user’s data to be interfered by other users.It should be noted that these attributes can be retrieved  even when redirect is happening provided the session is maintained .

 

Application Attributes : In cases where some value has to be set at application level and all the entities in the web application can read that value we use application attributes.

 

 

 

Number of View :1145

Tags: , , , , , , , , , ,

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache