Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Tomcat Interview Questions and Answers

Related differences

Jboss vs Tomcat

Ques 11. How can I access members of a custom Realm or Principal?

When you create a custom subclass of RealmBase or GenericPrincipal and attempt to use those classes in your webapp code, you'll probably have problems with ClassCastException. This is because the instance returned by request.getUserPrincipal() is of a class loaded by the server's classloader, and you are trying to access it through you webapp's classloader. While the classes maybe otherwise exactly the same, different (sibling) classloaders makes them different classes.


This assumes you created a My Principal class, and put in Tomcat's server/classes (or lib) directory, as well as in your webapp's WEB-INF/classes (or lib) directory. Normally, you would put custom realm and principal classes in the server directory because they depend on other classes there.

Here's what you would like to do, but it throws ClassCastException:
MyPrincipal p = request.getUserPrincipal();

String emailAddress = p.getEmailAddress();

Here are 4 ways you might get around the classloader boundary:
  • Reflection
Principal p = request.getUserPrincipal(); 
String emailAddress = p.getClass().getMethod("getEmailAddress", null).invoke(p, null);
  • Move classes to a common classloader
You could put your custom classes in a classloader that is common to both the server and your webapp - e.g., either the "common" or bootstrap classloaders. To do this, however, you would also need to move the classes that your custom classes depend on up to the common classloader, and that seems like a bad idea, because there a many of them and they a core server classes. 
  • Common Interfaces 
Rather than move the implementing custom classes up, you could define interfaces for your customs classes, and put the interfaces in the common directory. You're code would look like this: 
public interface MyPrincipalInterface extends java.security.Principal { 
public String getEmailAddress();
}
public class MyPrincipal implements MyPrincipalInterface { 
...
public String getEmailAddress() {
return emailAddress;
}
}
public class MyServlet implements Servlet { 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
MyPrincipalInterface p = (MyPrincipalInterface)request.getUserPrincipal();
String emailAddress = p.getEmailAddress();
...
}

Notice that this method gives you pretty much the webapp code you wanted in the first place
  • Serializing / Deserializing
You might want to try serializing the response of 'request.getUserPrincipal()' and deserialize it to an instance of [webapp]MyPrincipal.

Is it helpful? Add Comment View Comments
 

Ques 12. How do I override the default home page loaded by Tomcat?

After successfully installing Tomcat, you usually test it by loading http://localhost:8080 . The contents of that page are compiled into the index_jsp servlet. The page even warns against modifying the index.jsp files for this reason. Luckily, it is quite easy to override that page. Inside $TOMCAT_HOME/conf/web.xml there is a section called <welcome-file-list> and it looks like this:

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
The default servlet attempts to load the index.* files in the order listed. You may easily override the index.jsp file by creating an index.html file at $TOMCAT_HOME/webapps/ROOT. It's somewhat common for that file to contain a new static home page or a redirect to a servlet's main page. A redirect would look like:

<html>

<head>
<meta http-equiv="refresh" content="0;URL=http://mydomain.com/some/path/to/servlet/homepage/">
</head>

<body>
</body>

</html>
This change takes effect immediately and does not require a restart of Tomcat.

Is it helpful? Add Comment View Comments
 

Ques 13. How do I enable Server Side Includes (SSI)?

Two things have to be done for tomcat to aknowledge SSI scripts: 
  • Rename $CATALINA_BASE/server/lib/servlets-ssi.renametojar to $CATALINA_BASE/server/lib/servlets-ssi.jar. 
  • Uncomment the section of web.xml found in $CATALINA_BASE/conf/web.xml that deals with SSI. it looks like this when it is uncommented: 
<servlet> 
<servlet-name>ssi</servlet-name> 
<servlet-class>org.apache.catalina.ssi.SSIServlet</servlet-class> 
<init-param> 
<param-name>buffered</param-name> 
<param-value>1</param-value> 
</init-param> 
<init-param> 
<param-name>debug</param-name> 
<param-value>0</param-value> 
</init-param> 
<init-param> 
<param-name>expires</param-name> 
<param-value>666</param-value> 
</init-param> 
<init-param> 
<param-name>isVirtualWebappRelative</param-name> 
<param-value>0</param-value> 
</init-param> 
<load-on-startup>4</load-on-startup> 
</servlet>

Is it helpful? Add Comment View Comments
 

Ques 14. Question : How to communicate between two webservers in two diff systems?

By using plug module .

Is it helpful? Add Comment View Comments
 

Ques 15. How web server handles multiple requests for same action class(Struts) concurrently?

Struts or any webserver makes new thread for each new request. so multiple request is served with new request object.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook