java4all@1986 java. Powered by Blogger.

>> Thursday, June 16, 2011

Q).Can we implement an interface in Jsp?
A).No! In JSP we can't implement the interface. However we can use the class in the JSP file which has implemented the interface.

Q).How do you pass an init parameter to a Jsp?
A).You can pass the init parameters into your JSP file by decaring them into your deployment descriptor web.xml file.
<servlet> 
   <servlet-name>My Page</servlet-name>
   <jsp-file>/mypage.jsp</jsp-file> 
   <init-param> 
      <param-name>name</param-name> 
      <param-value>bhaskar</param-value) </init-param>
</servlet>

<servlet-mapping>
   <servlet-name>My Page</servlet-name>
   <url-pattern>/urlpattern</url-pattern>
</servlet-mapping>

You can access init parameters into your JSP file using the following code :

<%
String email = getServletConfig().getInitParameter("name");
%>

Q)How can i enable session tracking for jsp if the browsers has disabled cookies?
A).URL rewriting is another way to track the session if the browser has disabled the cookies. URL rewriting essentially includes the session ID within the link itself as a name/value pair.
We have two methods for this purpose :  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 these methods determines whether the cookies are enabled or not. If cookies are enabled then the input URL will returned unchanged since the session ID will be persisted as a cookie, otherwise append the session ID for each and every link that is part of your servlet response.

For Example :

hello1.jsp
<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
<a href='<%=url%?phpMyAdmin=70ac9566533a2665b6597346aab7f985&phpMyAdmin=f43d4e0b88acea2d2a393515f6bf38f2>'>hello2.jsp</a>

hello2.jsp
<%@ page session="true" %>
<%
Integer i= (Integer )session.getValue("num");
out.println("Num value in session is " + i.intValue());
%>
 
Q).How can I invoke a JSP error page from a servlet?
A).Yes, You can invoke JSP error page from a servlet by passing the exception object as a javax.servlet.jsp.jspException request attribute.

Example :

protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL, Throwable e) throws
ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try {
// Code that might cause exception
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/ErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}


Q).How will you include a static file in a JSP page?
A).You can include a static file in a JSP page by using include directive :

<%@ include file="relativeURL" %>

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP