Translate

WCD MT SOLVED

1.Identify the difference between the include tag and the include directive in JSP. 

Sol: 
Include tag :
The include directive lets you include a segment in the text of the main JSP page at translation time. The syntax of this JSP technology directive is as follows:
<%@ include file="segmentURL" %>
eg:
<td align='right' width='480'>
<%@ include file="/WEB-INF/view/common/copyright.jsp" %>
</td>
The content of the segment is embedded into the text of the main JSP page while the main page is being translated into a servlet class. If the included segment also includes other segments, then all of the segments are embedded recursively until all of the include directives are eliminated.

JSPInclude tag :
The jsprinclude standard action lets you include a segment in the text of the main JSP page at runtime. The syntax of this JSP technology action tag is as follows:
<jsp:include page="segmentURL" />
eg :
<td bgcolor='#CCCCFF' width='160' align='left'>
<jsp:include page="/WEB-INF/view/common/navigation.jsp" />
</td>
The content of the segment is embedded into the HTTP response of the main JSP page at runtime. If the included segment also includes other segments, then all of the segments are embedded recursively until all of the jsp:include actions have been processed. All of this
happens at runtime. The text of the segment is not placed in the main JSP page at translation time.



2.You are accessing a Java bean named CustomerBean from a JSP page. The bean has a property called CustomerName. Use the tag to access the bean. In addition, set and retrieve the value of the CustomerName property. 

Sol :
->useBean tag is used to use a JavaBean inside a jsp page , as we want to use a JavaBean named CustomerBean , hence we write the following code : 
 <jsp:useBean id="myBean" scope="request" class="CustomerBean" />

->then we need to retrieve the value of the property named CustomerName , hence we use the getProperty tag:
<jsp:getProperty name="myBean" property="CustomerName" />

->as we also need to set the value of the javaBean property named Customername , hence we use the setProperty tag:
<jsp:setProperty name="myBean" property="CustomerName" value="Demo Name" />

3.Identify the various life-cycle methods in JSP. In addition, identify the life-cycle methods in JSP that cannot be overridden. 

Sol :
A JSP page is essentially source code, and must be converted into a servlet before it can service requests.Following are the steps:
1.JSP Page Translation
In the first step, the web container translates the JSP file into a Java source file that contains a servlet class definition.
2.JSP Page Compilation
In the second step, the web container compiles the servlet source code into a Java class file.
3.JSP Page Class Loading
In the third step, the servlet class bytecode is loaded into the web container's JVM software using a classloader.
4.JSP Page Servlet Instance
In the fourth step, the web container creates an instance of the servlet class.
5.JSP Page Initialization
In the fifth step, the web container initializes the servlet by calling the jspInit method.
Actually, the container calls the init(SC) method, but your JSP page servlet inherits an init method that forwards the call to the jspInit method.
6.JSP Page Service
The initialized servlet can now service requests. With each request, the web container can call the _jspService method for the converted JSP page.
7.JSP Page Destroyed
When the web container removes the JSP servlet instance from service, it first calls the jspDestroy method to allow the JSP page to perform any required clean up.

4.Consider that you have created an HTML page that contains a text box named t1 and a submit button. The user is required to enter his name in the text box and click the submit button. When the user clicks the Submit button, he/should be redirected to a servlet named WelcomeServlet, which displays the user name along with a welcome message. Create the servlet. 

Sol :
In the html page or jsp page :
<html>
<form id="f1" name="f1" action="WelcomeServlet" method="get">
Name : <input type="text" id="t1" name="t1"/>
<input type="submit" id="b1" name="b1"/>
</form>

In the processRequest method of the Servlet page named WelcomeServlet.java :
String n=request.getParameter("t1");
out.printnl("Welcome ",n);

5.Identify the difference between the context parameters and the init parameters in a servlet? 

Sol : 
Context Parameters
The term “servlet context” essentially refers to the web application and the container in which the servlet runs. An interface javax.servlet.ServletContext provides access to several aspects of this environment, but in the context of this discussion, the methods of interest
are two methods that allow read-only access to a map ofcontext parameters. These methods are:
String getInitParameter(String name)
Enumeration<String>
getInitParameterNames()
Context Parameters cannot be supplied using annotations, because they relate to the entire web-application, rather than to a single element of it.
eg:
<context-param>
<param-name>fragmentContext</param-name>
<param-value>fragment Contextvalue</param-value>
</context-param>
Context parameters can be read in Java code using statements of the following form:
String paramValue =this.getServletContext().getInitParameter("paramName");

Init Parameters :
In addition to configuration parameters for the entireapplication, initialization parameters can be associated with the servlet individually. These are typically known simply as init parameters. This association may be achieved using the deployment descriptor file like this:
<servlet>
<servlet-name>MyServlet</servletname>
<servletclass>
SL314.package.MyServlet</servletclass>
<init-param>
<param-name>name</param-name>
<param-value>Fred Jones</paramvalue>
</init-param>
<init-param>
</servlet>
The annotation that would be equivalent to the preceeding declaration would be:
@WebServlet(name=“MyServlet”,urlPatterns=({“/MyServ”}),
initParams ={@WebInitParam(name=“name”,value=“Fred Jones*#x201D;)})

6.You are developing a Web application using Java servlets. In this application, you want that the user name should be accessible from all the Web pages through out the session. Create an HTTPSession object and store the user name in it. In addition, to verify whether the user name is stored, retrieve it from the HTTPSession object and display it.

Sol :
For this ,create an html or jsp page and input in the name of the user :
<form action="serv1" method="get">
Name : <input type="text" name="t1" id="t1"/>
<input type="submit" name="b1" id="b1"/>
</form>  

In the servlet page :
In the processRequest method, :
String s=request.getParameter("t1");
HttpSession s1=request.getSession();
s1.setAttribute("var1",s);

After settingup the variable var1 for servlet page, it can now be fetchedin any of the servlets by first requestig the session and then using the getAttribute function to fetch the value of the variable var1.

7.State whether you can define a constructor in a servlet? If no, justify your answer. If yes, then how is it different from the init() method? 

Sol :
There is no constructor for the servlet ,we do the initialization for the servlet by the meansof init method , we can override the init method in the servlet but while doing so , the signature has to be the same as that of the original version defined in the Httpservlet ,

The servlet’s init method can throw an UnavailableException or a ServletException to indicate that it failed to configure properly. In this case, the servlet will not be placed into active service and cannot be used. The GenericServlet class (supplied in the servlet API) implements the Servlet interface. It implements the init(ServletConfig) method, which stores the config object (the delegate) and then calls the init() method. It is this no-argument init method that you override in your servlet classes.

8.You are developing a Web application using Java servlets. The application uses a database named MyDatabase. The application stores the database name as context parameter by using the following code snippet: 

<context-param>
<param-name>DatabaseName</param-name>
<param-value>MyDatabase</param-value>
</context-param>

How can you retrieve the parameter?

Sol :
Context parameters can be read in Java code using statements of the following form:
String paramValue =this.getServletContext().getInitParameter("DatabaseName");