CSS font stacks, developer FAQs & web standards

Your banner ad here

WestNIC provides reliable web hosting services

Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.

JSP directives

Q: Can I instantiate an object without using the @ page include tag?

A: Yes, but perhaps you're talking about the @page import directive? The only way to instantiate a class in an external package is to declare it in an @ page import directive, followed by a standard constructor statement. Bear in mind that your JSP will be compiled into servlet source code and then a servlet class in turn. The page import directive is translated into an import statement in the servlet source and is the only way to place such statements in the source. If the import statement is not present the servlet will not compile.

As with all Java programs, classes in the package java.lang are imported implicitly, so it is possible to instantiate objects in this package without a page include directive.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do we instantiate an object using the @page import directive?

A: The @page import directive is not used to instantiate objects directly but it is usually necessary to import the relevant class before it is instantiated using standard Java syntax. The import is declared in a script directive tag of its own, the object instantiation and any associated statements and programming structures follow in script blocks that follow, as below.

<%@ page import="java.util.Date" %>
<% Date today = new Date(); %>
      

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Understanding JSP includes

Q: What's the difference between the include directive and action element?

A: The @page include directive in a JSP document is resolved at page translation time, when the servlet container converts the JSP into Java source code and then compiles it. The included file contents may be static HTML fragments and optionally include their own JSP tags and scripts. Each included file is inserted into the flow of the parent JSP document as if it were part of the original, so it is a good way of placing template-style code fragments into numerous pages.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What's the difference between the include directive and action element?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How many servlets are created by the include directive or jsp:include?

A: When you use the jsp:include tag, the text of a file is inserted into the output stream of the JSP servlet at the point the tag occurs. This insertion occurs at request time and is not parsed for JSP script content, the file content is just copied to the output stream.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How many servlets are created by the include directive or jsp:include?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Tag exceptions

Q: I get a ClassNotFoundException in my Struts app!

A: This error means that the custom com.jsptags.navigation.pager.PagerTagExtraInfo tag class you have declared in your tag library descriptor is not available to the servlet container's compiler. You should add the pager-taglib-2.0.jar file to the WEB-INF/lib directory of your application.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Tag libraries

Q: What is the basic concept of taglibs?

A: Tag libraries define a set of JSP tags that may be used in a Web application. The tag library descriptor file is used to declare the tags that belong to a library and the attributes and values they may use. The tag library descriptor is loaded by the servlet container when it starts up so that it can insert the relevant code into JSP documents when they are compiled into servlets.

Tag libraries enable the inclusion of relatively complex Java processing using a syntax that is very similar to HTML, that does not require special knowledge of Java language or programming. Tag libraries can also be re-used in multiple Web applications, hence Sun's Java Standard Tag Library (JSTL).

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What's the difference between standard and custom tag libraries?

A: The main difference between the two is that the Java Standard Tag Library (JSTL) is designed for general purposes and custom libraries are written for specific applications.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What's the difference between standard and custom tag libraries?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get "No such tag ... with prefix Hello", what's up?

A: To configure Tomcat to handle your custom tags, you must create an XML format Tag Library Descriptor (TLD) file and configure the application's web.xml file to load it. A minimal tag library descriptor is given below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
I get "No such tag ... with prefix Hello", what's up?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get "attribute binding does not accept any expressions"!

A: This error means that a tag attribute in your Tag Library Definition (TLD) is configured to expect a literal attribute value, not a runtime expression, but your JSP document is attempting to pass an expression to the tag attribute. Look up the tag identified in the stack trace and check the value attributed to the element on the relevant line of your JSP document. To fix the problem you have two options, as follows:

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
I get "attribute binding does not accept any expressions"!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Is the Struts 1.x Action thread safe or not?

A: Unfortunately the wording of the official Apache comparison between Struts versions is ambiguous and implies Struts version 1 is thread safe: “... Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests ... ” This does not mean that Actions are thread safe, rather Actions must carefully be written to be thread-safe.

The Struts version 1 servlet controller creates one instance of an Action class that is used to handle all requests, so Actions are at risk of concurrency problems. You should only use method local variables in your Action class and pass these variables as parameters to other supporting methods. Manage access to databases, the file system and other shared resources within methods, not through the Action class as a whole, and release those resources before you pass control to the View state.

These constraints mean that you should avoid complex decision logic in your Action classes, keep them as simple as possible and handle business logic in separate classes.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning, please read

Home · CSS · Java · Javascript · HTML · Help · Log