Sunday, August 2, 2009

JSP interview Questions

if so, the input URL is returned unchanged since the session ID will be persisted as a cookie. Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other. Basically, we create a new session within hello1.jsp and place an object within this session.

The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object. Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages. Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.

                hello1.jsp

                <%@ page session="true" %>

                <%Integer num = new Integer(100);

                       session.putValue("num",num);

                       String url =response.encodeURL("hello2.jsp");

                %>

                <a href='<%=url%>'>hello2.jsp</a>

                hello2.jsp

                <%@ page session="true" %>

                <%

                Integer i= (Integer )session.getValue("num");

                out.println("Num value in session is "+i.intValue());

How can I declare methods within my JSP page?

 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.

  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) %>

                Example

                file1.jsp:

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

                <%!

                public void test(JspWriter writer) throws IOException{

                writer.println("Hello!");

                }

                %>

                file2.jsp

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

                <html>

                <body>

                <%test(out);% >

                </body>

                </html>

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

 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

 

1

2

3

4

5

6

7

8

9

10

 

No comments:

Post a Comment