JavaBeat FAQs
SPONSORS
Download a movie FAQ
JAVABEAT
Home
Articles
Tips
QnA
Forums
FAQs Topics Books Topic
Java Java Books
Struts Struts Books
Spring Spring Books
Hibernate Hibernate Books
JBoss Seam Seam Books
J2EE J2EE Books
EJB EJB Books
JSF JSF Books
Servlets Servlets Books
JDBC JDBC Books
RMI RMI Books
JMS JMS Books
JNI JNI Books
Design Patterns Patterns Books
Exception Exception Books
Apache Ant Ant Books
Groovy
Cobertura
Quartz

Java Servlets Interview Questions

Servlets FAQs - 1 | Servlets FAQs - 2 | Servlets FAQs - 3 | Servlets FAQs - 4 | Servlets FAQs - 5 | Servlets FAQs - 6 | Servlets FAQs - 7 | Servlets FAQs - 8 | Servlets FAQs - 9 | Servlets FAQs - 10

71) Is there a way to disable a user's ability to double-click a submit image/button (and therefore submitting duplicate data -- multiple submits)? Is there a way to do this with Javascript?

 

Give the submit image (or button) an onClick() handler. Have the handler check if a flag is set and if not set the flag and submit the form and then clear the form.

72) What are the main differences between Servlets and ISAPI?

 

The first difference is obviously that Servlets is the technology from Sun Microsystems and ISAPI is from Microsoft.

Other Differences are:

ï‚·  Servlet is a simple .class file and ISAPI is a DLL

ï‚·  Servlets run in the Servlet containers and may be in-process or out of process. ISAs run in the same address space as the HTTP server

ï‚·  Servlet container preprocesses and postprocesses the data communication between the client and server. ISAPI Filters provide the capability of pre-processing and post-processing of all data sent between the client and the server

ï‚·  Java is the only choice for writing Servlets, VC++/MFC is used to write ISAPI code

ï‚·  Servlets works on most of the Web servers plus third party containers can be integrated with other web servers to provide servlets on them. ISAPI works on only ISAPI-compliant Web server (for example, Microsoft Internet Information Server)

ï‚·  Servlets can connect to the Databases through JDBC as well as jdbc-odbc bridges. ISAPI can connect to Databases through only ODBC

ï‚·  Servlets have access to many server-side technologies like EJB and etc. ISAPI is limited in scope

ï‚·  Multiple commands can be implemented in a servlet by using pathinfo. ISAPI allows multiple commands in one DLL, implemented as member functions of the CHttpServer object in the DLL.

          ï‚·  Content generation and content presentation can be done seperately in          Servlets with the help of JSP. ISAPI code has to generate HTML code itself.

 

73) Can I associate a servlet with a particular mime-type, so if the client requests a file of that type, my servlet will be executed?

 

In web.xml you can use a mime-mapping to map the type with a certain extension and then map the servlet to that extension.

e.g.

    <mime-mapping>
        <extension>
            zzz
        </extension>
        <mime-type>
            text/plain
        </mime-type>
    </mime-mapping>
 
    <servlet-mapping>
        <url>
            *.zzz
        </url>        
        <servlet-name>
            MyServlet
        </servlet-name>
    </servlet-mapping>

So, when a file for type zzz is requested, the servlet gets called.

74) What are the different cases for using sendRedirect() vs. getRequestDispatcher()?

 

When you want to preserve the current request/response objects and transfer them to another resource WITHIN the context, you must use getRequestDispatcher or getNamedDispatcher.

If you want to dispatch to resources OUTSIDE the context, then you must use sendRedirect. In this case you won't be sending the original request/response objects, but you will be sending a header asking to the browser to issue a request to the new URL.

If you don't need to preserve the request/response objects, you can use either.

75) How do I access the value of a cookie using JavaScript?

You can manipulate cookies in JavaScript with the document.cookie property. You can set a cookie by assigning this property, and retrieve one by reading its current value.

The following statement, for example, sets a new cookie with a minimum number of attributes:

document.cookie = "cookieName=cookieValue";

And the following statement displays the property's value:

alert(document.cookie);

The value of document.cookie is a string containing a list of all cookies that are associated
with a web page. It consists, that is, of name=value pairs for each cookie that matches the
current domain, path, and date. The value of the document.cookie property, for instance,
might be the following string:

cookieName1=cookieValue1; cookieName2=cookieValue2;

76) How do I write to a log file using JSP under Tomcat? Can I make use of the log() method for this?

 

Yes, you can use the Servlet API's log method in Tomcat from within JSPs or servlets. These messages are stored in the server's log directory in a file called servlet.log.

 

77) How can I use a servlet to print a file on a printer attached to the client?

 

The security in a browser is designed to restrict you from automating things like this. However, you can use JavaScript in the HTML your servlet returns to print a frame. The browser will still confirm the print job with the user, so you can't completely automate this. Also, you'll be printing whatever the browser is displaying (it will not reliably print plug-ins or applets), so normally you are restricted to HTML and images.

[The JavaScript source code for doing this is:

<input type="button" onClick="window.print(0)" value="Print This Page">
 

78) How do you do servlet aliasing with Apache and Tomcat?

 

Servlet aliasing is a two part process with Apache and Tomcat. First, you must map the request in Apache to Tomcat with the ApJServMount directive, e.g.,

ApJServMount /myservlet /ROOT

Second, you must map that url pattern to a servlet name and then to a servlet class in your web.xml configuration file. Here is a sample exerpt:

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>com.mypackage.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>/myservlet</url-pattern>
</servlet-mapping>
 

79 ) I want my servlet page to redirect to a login page if the session has timed out. How can I know if my session has timed out?

 

If the servlet engine does the time-out, following code should help you:

//assume you have a HttpServletRequest request
if(request.getSession(false)==null) {
  //no valid session (timeouted=invalid)
  
  //code to redirect to login page
}
 

80) Can Tomcat be configured to interpret all, or selected, .html files within a given context as JSP? Or, do JSP files have to end with a .jsp extension?

 

yes you can do that by modifying the web.xml file. You will have to invoke the org.apache.jasper.runtime.JspServlet for all the requests having extension .html. You can do that by changing the Servlet mapping code:
<servlet-mapping>
<servlet-name>
jsp
</servlet-name>
<url>*.html</url>

</servlet-mapping>


And comment out the following block

<mime-mapping>
<extension>
html
</extension>
<mime-type>
text/html
</mime-type>
</mime-mapping>

Related Articles
OpenCms 7 Development
Liferay Portal Enterprise Intranets
ZK Developer’s Guide : Online Media Library
The BIRT Environment and Your First Report
AJAX - The Complete Reference
The Java 6.0 Compiler API
Spring Web Flow - Introduction
Introduction to Spring Web Framework
Integrating Struts With Spring
What's new in Struts 2.0? - Struts 2.0 Framework
more articles
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria books

Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat