How can I set a cookie and delete a cookie from within a JSP page
Cookie mycook = new Cookie(“name”,”value”);
response.addCookie(mycook);
Cookie killmycook = new Cookie(“mycook”,”value”);
killmycook.setMaxAge(0);
killmycook.setPath(“/”);
killmycook.addCookie(killmycook);
What is JavaServer Pages Standard Tag Library (JSTL
A tag library that encapsulates core functionality common to many JSP applications.JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization and locale-specific formatting tags, SQL tags, and functions
How do I prevent the output of my JSP or Servlet pages from being cached by the browser
You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.
What is the difference between following
Both the tags include information from one JSP page in another. The differences are:
< jsp : include page = … >
This is like a function call from one jsp to another jsp. It is executed ( the included page is executed and the generated html content is included in the content of calling jsp) each time the client page is accessed by the client. This approach is useful while modularizing a web application. If the included file changes then the new content will be included in the output automatically.
< % @ include file = … >
In this case the content of the included file is textually embedded in the page that have < % @ include file=”..”> directive. In this case when the included file changes, the changed content will not get included automatically in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.
What is JSP technology
Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.
What is JSP scriptlet
A JSP scripting element containing any code fragment that is valid in the scripting language used in the JSP page. The JSP specification describes what is a valid scriptlet for the case where the language page attribute is “java”.
What is JSP document
A JSP page written in XML syntax and subject to the constraints of XML documents.
Difference Between Forward and Include in JSP
The <jsp:forward> action enables you to forward the request to a static HTML file, a servlet, or another JSP.
<jsp:forward page=”url” />
The JSP that contains the <jsp:forward> action stops processing, clears its buffer, and forwards the request to the target resource. Note that the calling JSP should not write anything to the response prior to the <jsp:forward> action.
You can also pass additional parameters to the target resource using the <jsp:param> tag.
<jsp:forward page=”test.htm” >
<jsp:param name=”name1″ value=”value1″ />
<jsp:param name=”name2″ value=”value2″ />
</jsp:forward>
In this example, test.jsp can access the value of name1 using request.getParameter(“name1”).
To “include” another resource with a JSP, you have two options: the include directive and the include action.The include directive executes when the JSP is compiled, which parses any JSP elements in the included file for a static result that is the same for every instance of that JSP. The syntax for the include directive is
<@ include file=”some-filename” %>.
The include action, on the other hand, executes for each client request of the JSP, which means the file is not parsed but included in place. This provides the capability to dynamically change not only the content that you want to include, but also the output of that content. The syntax for the include action is <jsp:include page=”some-filename” flush=”true” />. Note that the flush attribute must always be included (in JSP 1.1) to force a flush of the buffer in the output stream.
You can also pass parameters to the included file using the same process
<jsp:forward> action:
<jsp:include page=”template.htm” flush=”true” >
<jsp:param name=”name1″ value=”value1″ />
</jsp:include>
What is expression in JSP
Expression tag is used to insert Java values directly into the output.
Syntax for the Expression tag is: <%= expression %>
An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. The most commonly used language is regular Java.
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”/>