|
INTERVIEW QUESTIONS
J2EE
JSP
DETAILS
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"%> <html> <body> <%test(out);% > </body> </html>
|
|
|
Category |
JSP Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8370 users |
Added on |
10/24/2009 |
Views |
79313 |
Rate it! |
|
|
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"%> <html> <body> <%test(out);% > </body> </html> Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How can I enable session tracking for JSP pages if the browser has disabled cookies?
|
View Answer
|
|
How do I use a scriptlet to initialize a newly instantiated bean?
|
View Answer
|
|
How does JSP handle run-time exceptions?
|
View Answer
|
|
How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
|
View Answer
|
|
How do I use comments within a JSP page?
|
View Answer
|
|
Can I stop JSP execution while in the midst of processing a request?
|
View Answer
|
|
Is there a way to reference the "this" variable within a JSP page?
|
View Answer
|
|
How do I perform browser redirection from a JSP page?
|
View Answer
|
|
What JSP lifecycle methods can I override?
|
View Answer
|
|
Can a JSP page process HTML FORM data?
|
View Answer
|
|
How do I mix JSP and SSI #include?
|
View Answer
|
|
How can I implement a thread-safe JSP page?
|
View Answer
|
|
How do I include static files within a JSP page?
|
View Answer
|
|
How do you prevent the Creation of a Session in a JSP Page and why?
|
View Answer
|
|
What is the page directive is used to prevent a JSP page from automatically creating a session?
|
View Answer
|
|
Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?
|
View Answer
|
|
Can a JSP page instantiate a serialized bean?
|
View Answer
|
|
Can you make use of a ServletOutputStream object from within a JSP page?
|
View Answer
|