|
INTERVIEW QUESTIONS
J2EE
JSP
DETAILS
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" /
|
|
|
Category |
JSP Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 10091 users |
Added on |
10/24/2009 |
Views |
74718 |
Rate it! |
|
|
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" / 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 |
|
Is there a way I can set the inactivity lease period on a per-session basis?
|
View Answer
|
|
How can I set a cookie and delete a cookie from within a JSP page?
|
View Answer
|
|
How can I declare methods within my JSP page?
|
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
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All JSP Interview Questions & Answers - Exam Mode /
Learning Mode
|