JagooIndia
Forum of Indian Youth
JAVA INTERVIEW QUES
Core Java
JDBC
Servlet
JSP
Struts
EJB
JMS
J2ME
RMI
Spring
Seminar & Projects
Electronics
Electrical
Computer Science /IT
Mechanical
Civil
Marketing
Finance
Technical Certificarion
Sun
Oracle
Microsoft
IBM
Cisco
PMI
Domain Certifications
Finance
Insurance
Project Management
   
 

JSP interview Question

 
 
How can I set a cookie and delete a cookie from within a JSP page
A cookie, mycookie, can be deleted using the following scriptlet:
    <%
    //creating a cookie
    Cookie mycookie = new Cookie("aName","aValue");
    response.addCookie(mycookie);
    //delete a cookie
    Cookie killMyCookie = new Cookie("mycookie", null);
    killMyCookie.setMaxAge(0);
    killMyCookie.setPath("/");
    response.addCookie(killMyCookie);
    %>
How does a servlet communicate with a JSP page
The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.
    public void doPost (HttpServletRequest request, HttpServletResponse response) {
    try {
        govi.FormBean f = new govi.FormBean();
        String id = request.getParameter("id");
        f.setName(request.getParameter("name"));
        f.setAddr(request.getParameter("addr"));
        f.setAge(request.getParameter("age"));
        //use the id to compute
        //additional bean properties like info
        //maybe perform a db query, etc.
        // . . .
        f.setPersonalizationInfo(info);
        request.setAttribute("fBean",f);
        getServletConfig().getServletContext().getRequestDispatcher
                      ("/jsp/Bean1.jsp").forward(request, response);
        } catch (Exception ex) {
    . . .
       }
    }
The JSP page Bean1.jsp can then process fBean, after first extracting it from the default request scope via the useBean action.

jsp:useBean id="fBean" class="govi.FormBean" scope="request"
/ jsp:getProperty name="fBean" property="name"
/ jsp:getProperty name="fBean" property="addr"
/ jsp:getProperty name="fBean" property="age"
/ jsp:getProperty name="fBean" property="personalizationInfo" /
How do I have the JSP-generated servlet subclass my own custom servlet class, instead of the default
One should be very careful when having JSP pages extend custom servlet classes as opposed to the default one generated by the JSP engine. In doing so, you may lose out on any advanced optimization that may be provided by the JSP engine. In any case, your new superclass has to fulfill the contract with the JSP engine by:
Implementing the HttpJspPage interface, if the protocol used is HTTP, or implementing JspPage otherwise Ensuring that all the methods in the Servlet interface are declared final Additionally, your servlet superclass also needs to do the following:

The service() method has to invoke the _jspService() method

The init() method has to invoke the jspInit() method
The destroy() method has to invoke jspDestroy()
If any of the above conditions are not satisfied, the JSP engine may throw a translation error.
Once the superclass has been developed, you can have your JSP extend it as follows:

    <%@ page extends="packageName.ServletName" %>
How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values
You could make a simple wrapper function, like
    <%!
    String blanknull(String s) {
    return (s == null) ? "" : s;
    }
    %>
    then use it inside your JSP form, like
    <input type="text" name="shoesize" value="<%=blanknull(shoesize)% >" >
How can I get to print the stacktrace for an exception occuring within my JSP page
By printing out the exception’s stack trace, you can usually diagonse a problem better when debugging JSP pages. By looking at a stack trace, a programmer should be able to discern which method threw the exception and which method called that method. However, you cannot print the stacktrace using the JSP out implicit variable, which is of type JspWriter. You will have to use a PrintWriter object instead. The following snippet demonstrates how you can print a stacktrace from within a JSP error page:
    <%@ page isErrorPage="true" %>
    <%
    out.println("    ");
     PrintWriter pw = response.getWriter();
     exception.printStackTrace(pw);
    out.println(" ");
    %>
How do you pass an InitParameter to a JSP
The JspPage interface defines the jspInit() and jspDestroy() method which the page writer can use in their pages and are invoked in much the same manner as the init() and destory() methods of a servlet. The example page below enumerates through all the parameters and prints them to the console.
    <%@ page import="java.util.*" %>
    <%!
    ServletConfig cfg =null;
    public void jspInit(){
    ServletConfig cfg=getServletConfig();
    for (Enumeration e=cfg.getInitParameterNames(); e.hasMoreElements();) {
    String name=(String)e.nextElement();
    String value = cfg.getInitParameter(name);
    System.out.println(name+"="+value);
    }
    }
    %>
How can my JSP page communicate with an EJB Session Bean
The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean:
    <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %>
    <%!
    //declare a "global" reference to an instance of the home interface of the session bean
    AccountHome accHome=null;
    public void jspInit() {
    //obtain an instance of the home interface
    InitialContext cntxt = new InitialContext( );
    Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");
    accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class);
    }
    %>
    <%
    //instantiate the session bean
    Account acct = accHome.create();
    //invoke the remote methods
    acct.doWhatever(...);
    // etc etc...
    %>
Can we implement an interface in a JSP
No
What is the difference between ServletContext and PageContext
ServletContext: Gives the information about the container. PageContext: Gives the information about the Request
What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()
request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource,
context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.
How to pass information from JSP to included JSP
Using <%jsp:param> tag.
What is the difference between directive include and jsp include
<%@ include>: Used to include static resources during translation time. JSP include: Used to include dynamic content or static content during runtime.
What is the difference between RequestDispatcher and sendRedirect
RequestDispatcher: server-side redirect with request and response objects. sendRedirect : Client-side redirect with new request and response objects
How do I mix JSP and SSI #include
If you're just including raw HTML, use the #include directive as usual inside your .jsp file.

But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data.inc file contains jsp code you will have to use
    <%@ vinclude="data.inc" %>
The is used for including non-JSP files.

PREVIOUS

 
 
   © Copyright Jagoo India.com. All right reserved.