Posts Tagged ‘jsp’

Basic Standard Action Tags In JSP Tutorial With Examples JSP Technical Advices

Standard Action Tags In JSP

Standard Action Tags In JSP are meant for performing some action at run time. As the name of the tag itself suggests these tags are used for run time behavior. Unlike scripting tags which are meant for generating related servlet code at the time of translation,Standard Action Tags In JSP generates code for run time behavior.

 

Examples of Standard Action Tags In JSP are as follows :

 

  1. <jsp:include file= ”AnotherJsp.jsp”/>
  2. <jsp:forward file=”AnotherJsp.jsp”/>
  3. <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”>
  4. <c:set var=”attrName” value=”attrVal” scope=”request”/>

 

There are so many other tags as well amongst which these are some and important.

Let us take these tags individually as see how do they contribute for Standard Action Tags In JSP

 

  1. <jsp:include file= ”AnotherJsp.jsp”/> : This tag is used for run time inclusion of another JSP page in an existing one. This inclusion is not similar to inclusion with include directive. Include directives are meant for static inclusion. This means that the code from the included JSP will be included int the including JSP at the translation time. But in the case of <jsp:include file= ”AnotherJsp.jsp”/> tag a run time call to the included JSP is done and the response from the included JSP is added to the response of the including JSP.
  2. <jsp:forward file=”AnotherJsp.jsp”/> : This tag is used for forwarding the request to another JSP at run time. The difference here from run time or dynamic inclusion is that here the response from the forwarded JSP only goes to the user and the response from the first JSP from where the forward happens gets flushed out.
  3. <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”> : This tag is used to create or fetch a java bean object ,set or get its property value/s and retrieve or set this java bean object in certain scope.
  4. <c:set var=”attrName” value=”attrVal” scope=”request”/> : This tag is used to set an attribute in a particular scope.This is an important tag as our MVC applications rely heavily on attributes
Number of View :1238

Tags: , , , , ,

No Comments


Explain Different Types Of Tags In JSP Interview Asked Questions And Answers

Different Types Of Tags In JSP

 

Java Sever Page (JSP) is an abstraction for servlet in which we have less java code and more tags(HTML and JSP). A JSP page is basically used for views with dynamic data(unlike HTML) and evolved basically for people who do not know java and still work at j2ee server side programming.

Depending upon the type of the usage the tags in JSP page are categorized as :

 

  1. Scripting Tags
  2. Standard Action Tags
  3. Custom Tags

Scripting Tags : These tags are meant for compile time and translation.This means that these tags are meant for translation of code to the servlet. Different types of scripting tags are:

a. Directive Tag

b. Declarative Tag

c. Scriptlet Tag

d. Expression Tag

 

a. Directive Tag : There are three types of directive tags :

1.Page Directive : It is used for imports,encoding etc.

Example: <%@ page import=”…” %>

2.Include Directive : Its is used for static inclusion of JSP and HTML pages

Example: <%@ include file=”anotherJsp.jsp” %>

3.Tag lib Directive: It is used for including tag libraries

Example: <%@ taglib uri=”…” prefix = “..”%>

b. Declarative Tag : These tags are used to declare variables or methods for the servlet translated servlet class.

Example: <%! int i =0;%>

 

c. Scriptlet Tag : These tags are used to write java code inside the JSP page.What goes inside the scriptlet tag is merged into the service method of the translated servlet code.

Example: <% System.out.println(“Hello”);%>

 

d. Expression Tag : These tags are used to print expressions.What is written inside this tag goes as an argument for out.print() inside the service method of the translated servlet.

Example : <%= “Hello” %>

 

It should be noted that we do not use semicolon inside expression tag because it will go as an argument in ou.print() function,hence will result in syntax error.While in case of declarative tag and scirptlet tag we use semicolon.

Number of View :1263

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

No Comments


How To Get Servlet Init Parameters In JSP

How To Get Servlet Init Parameters In JSP

We know that we can declare init parameters for JSP file in web.xml file as we do in the case of servlet.The entry in the web.xml file will be as follows:

<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>

Now comes the question that how can be access or retrieve those initialization parameters in the JSP page.Among the life cycle methods of a JSP page jspInit() method is one of the methods which can be overridden.The jspInit() method is called by the init() method of the servlet which is the result of the translation of the JSP page.To access the initialization parameters is one of the cases where we override the jspInit() method.When we need to declare a method we use the declarative tag as follows :

<%!
 public void jspInit()
 {
   ServletConfig sConfig = getServletCOnfig();
   String str= sConfig.getInitParameter(“paramName”);
   getSerrvletContext().setAttribute(“name”,str);
 }
%>

Here we have overridden the jspInit() method in the declarative tag.Since this method is called by the init method of the translated servlet code we can access the servlet config object with the help of getServletConfig() method as it is passed by the server to the servlet through init() method. This method (jspInit()) will appear as an overridden method in the translated servlet.The point should be noted here that init() method in the servlet calls this jpInit() life cycle method of the  jsp page so it can access the servlet config object.The life cycle method _jspService() is not permitted to be overridden .Initialization parameters for the the JSP page can be understood as the configuration parameters to be accessed by the translated servlet.After all a JSP page is a servlet at the end of the day and it is the translated servlet only which processes the requests from the clients.

 

 

 

Number of View :1412

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

No Comments


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 :2035

Tags: , , , , , , ,

No Comments


Difference Between Parameters And Attributes In Servlets/JSP Parameter vs. Attribute

Parameters and Attributes in Servlets And JSPs

 

Parameters and attributes in servlets and JSPs in java web applications are ways of communication among various entities(servlets and JSPs). However, there are differences between parameters and attributes.

The differences are as follows :

 

Attributes in Servlets and JSPs:

Attributes in servlets and JSPs can be of various scope.e.g.-request,session,servlet context(Application)

When attributes in servlets and JSPs are obtained in the code the return type is object of an “Object” class.

Attributes  in servlets and JSPs can be set or got from the code of the of servles and JSPs.

 

Parameters in servlets and JSPs:

Parameters  in servlets and JSPs are valid till the request response cycle only.

The return type of he code for obtaining  the parameters  in servlets and JSPs  is an object of  the  type String.

Parameters  in servlets and JSPs can only be read from the code.

Parameters  in servlets and JSPs can be set only through web.xml or appended in url from html parameters(text boxes etc.)

 

 

Code for getting a parameter is::

request.getParameter("paramname");

code for  seting an attribute is ::

scope.setAttribute("name","value");

code of getting an attribute value is ::

scope.getAttribute("name");

Here scope symbolizes different scopes(Request,Session or Application))

 

Parameters can be set from web.xml file as follows:

 

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
 <init-param> <param-name>pname</param-name>
<param-value>pvalue</param-value>
</init-param>
</servlet> 

or 

<web-app> 

<context-param>
 <param-name>pname</param-name>
<param-value>pvalue</param-value>
</context-param>
 ...
 </web-app>

 

Conceptually Parameters in servlets and JSPs are used to capture user input/s in the application or to read from some configuration file.

Attributes in servlets and JSPs are used to pass data from one servlets or JSP to another servlet or JSP.Since attributes in servlets and JSPs can be associated to various scopes,its usage spans form maintaining session to maintain request attributes or maintain or get a value across the application itself. It is the concept  of usage which itself describes the differences between the two.

 

 

Number of View :900

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