What are the four types of J2EE modules?
- Application client module
- Web module
- Enterprise JavaBeans module
- Resource adapter module
How can I set a cookie in JSP?-
response. setHeader(”Set-Cookie”, “cookie string”); To give the response-object to a bean, write a method setResponse (HttpServletResponse response) – to the bean, and in jsp-file:<% bean. setResponse (response); %>
What are JSP ACTIONS?-
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. Available actions include: jsp:include – Include a file at the time the page is requested. jsp:useBean – Find or instantiate a JavaBean. jsp:setProperty – Set the property of a JavaBean. jsp:getProperty – Insert the property of a JavaBean into the output. jsp:forward – Forward the requester to a newpage. Jsp: plugin – Generate browser-specific code that makes an OBJECT or EMBED
How do you pass data (including JavaBeans) to a JSP from a servlet?-
?– (1) Request Lifetime: Using this technique to pass beans, a request dispatcher (using either “include” or forward”) can be called. This bean will disappear after processing this request has been completed. Servlet: request. setAttribute(”theBean”, myBean); RequestDispatcher rd = getServletContext(). getRequestDispatcher(”thepage. jsp”); rd. forward(request, response); JSP PAGE:<jsp: useBean id=”theBean” scope=”request” class=”. . . . . ” />(2) Session Lifetime: Using this technique to pass beans that are relevant to a particular session (such as in individual user login) over a number of requests. This bean will disappear when the session is invalidated or it times out, or when you remove it. Servlet: HttpSession session = request. getSession(true); session. putValue(”theBean”, myBean); /* You can do a request dispatcher here, or just let the bean be visible on the next request */ JSP Page:<jsp:useBean id=”theBean” scope=”session” class=”. . . ” /> 3) Application Lifetime: Using this technique to pass beans that are relevant to all servlets and JSP pages in a particular app, for all users. For example, I use this to make a JDBC connection pool object available to the various servlets and JSP pages in my apps. This bean will disappear when the servlet engine is shut down, or when you remove it. Servlet: GetServletContext(). setAttribute(”theBean”, myBean); JSP PAGE:<jsp:useBean id=”theBean” scope=”application” class=”. . . ” />
What is JSP expression language
A language used to write expressions that access the properties of JavaBeans components. EL expressions can be used in static text and in any standard or custom tag attribute that can accept an expression
How do I use a scriptlet to initialize a newly instantiated bean
A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.
The following example shows the “today” property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the
<jsp:setProperty actionvalue=””/ >
What is difference between custom JSP tags and beans
Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:
- the tag handler class that defines the tag’s behavior
- the tag library descriptor file that maps the XML element names to the tag implementations
- the JSP file that uses the tag library
When the first two components are done, you can use the tag by using taglib directive:
<%@ taglib uri=”xxx.tld” prefix=”…” %>
Then you are ready to use the tags you defined. Let’s say the tag prefix is test:
MyJSPTag or
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags to declare a bean and use to set value of the bean class and use to get value of the bean class.
<%=identifier.getclassField() %>
Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:
Custom tags can manipulate JSP content; beans cannot.Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans.Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.
What are all the different scope values for the tag
< jsp : useBean > tag is used to use any java object in the jsp page. Here are the scope values for < jsp : useBean > tag:
- page
- request
- session and
- application
What do you understand by JSP Actions
JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of “jsp” followed by a colon, followed by the action name followed by one or more attribute parameters.
There are six JSP Actions:
- < jsp : include / >
- < jsp : forward / >
- < jsp : plugin / >
- < jsp : usebean / >
- < jsp : setProperty / >
- < jsp : getProperty / >
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 requestscope via the useBean action.
- <jsp:useBean id=”fBean”class=”govi.FormBean” scope=”request”/>
- <jsp:getPropertyname=”fBean” property=”name”/>
- <jsp:getProperty name=”fBean” property=”addr”/>
- <jsp:getProperty name=”fBean” property=”age”/>
- <jsp:getProperty name=”fBean” property=”personalizationInfo”/>