Members BookmarksPolls Fresher Jobs Funny Pictures MCA Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

Advertisements


website counter



jsp Q & A


Posted Date: 17 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: General

Posted By: Aparanjitha       Member Level: Gold
Rating:     Points: 5



1)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 JSPengine 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.

2)How can I set a cookie and delete a cookie from within a JSP page?

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

%>

3)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.

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





4)How can I enable session tracking for JSP pages if the browser has disabled cookies?

We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; 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");

%>

hello2.jsp

hello2.jsp

<%@ page session="true" %>

<%

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

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

5)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 action.




value="<%=java.text.DateFormat.getDateInstance().format(new java.util.Date())

%>"/ >

<%-- scriptlets calling bean setter methods go here --%>



6)How does JSP handle run-time exceptions?

You can use the errorPage attribute of the page directive to have uncaught runtime exceptions automatically forwarded to an error processing page.

For example:

<%@ page errorPage="error.jsp" %>

redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive:

<%@ page isErrorPage="true" %>

the Throwable object describing the exception may be accessed within the error page via the exception implicit object.

Note: You must always use a relative URL as the value for the errorPage attribute.

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

<%

response.setHeader("Cache-Control","no-store"); //HTTP 1.1

response.setHeader("Pragma","no-cache"); //HTTP 1.0

response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

%>

8)How do I use comments within a JSP page

You can use "JSP-style" comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments are not visible at the client.

For example:

<%-- the scriptlet is now commented out

<%

out.println("Hello World");

%> --%>

You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client. For example:



Of course, you can also use comments supported by your JSP scripting language within your scriptlets. For example, assuming Java is the scripting language, you can have:

<%

//some comment

/**

yet another comment **/ %>

9)Can I stop JSP execution while in the midst of processing a request?

Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine. The trick (asuming Java is your scripting language) is to use the return statement when you want to terminate further processing. For example, consider:

<% if (request.getParameter("foo") != null) {

// generate some html or update bean property

} else {

/* output some error message or provide redirection back to the input form after creating a memento bean updated with the 'valid' form elements that were input. This bean can now be used by the previous form to initialize the input elements that were valid then, return from the body of the _jspService() method to terminate further processing */

return;

}

%>

10)Is there a way to reference the "this" variable within a JSP page?

Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the servlet generated by the JSP page.





Responses

Author: Deepu    19 Mar 2008Member Level: Diamond   Points : 1
Good work Aparanjitha.Keep going eith your postings.


Author: Aparanjitha    19 Mar 2008Member Level: Gold   Points : 2
thanks for ur suggestions and encouragement keep on reading my resoucres and give me some more advises .. Thank you


Author: Deepu    19 Mar 2008Member Level: Diamond   Points : 3
Sure,I will go on reading your postings.If you go on accepting my advices i will give more advises for your development in the site.Keep going.But just keep in mind that your postings are going to be read by hundreds of people throughout the world.


Author: Aparanjitha    19 Mar 2008Member Level: Gold   Points : 2
thnx for your reply to my msg. i mine it that my postings will be read by hundreds of people . so im sure that i will place good resources


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Great Failures....
Previous Resource: jsp Q &A
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.