Wednesday, October 24, 2007

JSP Interview questions-part4


Question: How can I declare methods within my JSP page?

Answer: You can declare methods for use within your JSP page as declarations. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions.

Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare. For example:

<%! public String whereFrom(HttpServletRequest req) {

HttpSession ses = req.getSession();

...

return req.getRemoteHost(); }%>

<% out.print("Hi there, I see that you are coming in from "); %>

<%= whereFrom(request) %>

Another Example

file1.jsp:

<%@page contentType="text/html"%>

<%! public void test(JspWriter writer) throws IOException{

writer.println("Hello!"); } %>

file2.jsp

<%@include file="file1.jsp"%>

<%test(out);% >

Question: Is there a way I can set the inactivity lease period on a per-session basis?

Answer: Typically, a default inactivity lease period for all sessions is set within your JSP engine admin screen or associated properties file. However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis. This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created. For example:

<% session.setMaxInactiveInterval(300); %>

would reset the inactivity period for this session to 5 minutes. The inactivity interval is set in seconds.

Question: How can I set a cookie and delete a cookie from within a JSP page?

Answer: A cookie, mycookie, can be deleted using the following scriptlet:

<% //creating a cookie

Cookie mycookie = new Cookie("aName","aValue");

response.addCookie(mycookie);

//delete a cookie

Cookie killMyCookie = new Cookie("mycookie", null);

killMyCookie.setMaxAge(0);

killMyCookie.setPath("/");

response.addCookie(killMyCookie); %>

Question: How does a servlet communicate with a JSP page?

Answer: 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 request scope via the useBean action.

jsp:useBean id="fBean" class="govi.FormBean" scope="request"/ jsp:getProperty name="fBean" property="name" / jsp:getProperty name="fBean" property="addr" / jsp:getProperty name="fBean" property="age" / jsp:getProperty name="fBean" property="personalizationInfo" /

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com