Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

November 6, 2009

JSP and SERVLET

difference between JSP and SERVLET

Servlets and Java Server Pages are complementary APIs both providing a means for generating dynamic Web content. Infact JSP's are extensions to servlets which are mainly intended for presentation and to separate presentation from business logic. Functionality wise both are equally good and same.

In Servlets the dynamic content is generated by writing the HTML code embedded in Java code where as in JSP the dynamic content is generated by writing the Java code embedded in HTML code and JSP scriptlets. Finally JSPs are best suit for presentation and servlets are best suit for Controlling purpose


Q : Why is that we deploy servlets in a webserver.What exactly is a webserver?
A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser. Understand that a Web server's delegation model is fairly simple. When a request comes into the Web server, the Web server simply passes the request to the program best able to handle it. The Web server doesn't provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses

Why is that we deploy servlets in a webserver.What exactly is a webserver?
When you increase the buffer size in jsp using the buffer tag then it means that you are increasing the data holding capacity of the buffer. When you access the jsp(incresed buffer size) then the user will observe that the page is loaded with data slowly.I means the user can actually see that the page content is being loaded. Its a good practise to use that way coz the user will be at ease.

Syntactically we can a write a constructor in a servlet. But if u write parameterized constructors ensure that u also add zero-arg constructor.If u dont add a zero-arg const. servlet instantiation fails. Infact a constructor doesnt serve any purpose in a servlet except for the zero-arg const. which is used at the time of instantiation of the servlet object.

what is the importance of deployment descriptor in servlet?
The deployment descriptor is not confined to servlets. It belongs to the application. The deployment descriptor basically tell the container where to look out for the resources which patterns to follow how are the resources mapped etc
you can customize web application configuration via the deployment descriptor These configuration include context and servlet initialization parameters servlet loading welcome page session timeout period error page init parameters mime types and can make application distributable.

November 5, 2009

Servlets n JSPs

1. How do u difine an error page?

2.What r the methods called in servlet life cycle?

3.How can u make session variables un available?

4. How to change the port no of agiven application server?

5.Why and in wat kind of requirments we need to use EJBs?

1. Create one jsp page and there is tag called IsErrorPage True it means this is an error page . I u need to call this error page in ur application u can decalre errorpage "give realtive url"

2. Init(). For intilizing the servlet.
Service(). For servicing the request .
Destroy(). TO destroy the resource allocated by Init().

3. In Jsp There having an attribute like Session false.

4. Inside the appication server(jBoss) u just go to server /default/deploy/tomacat5.5.rar/server.xml. Inside server.xml u can see Tag
8080. give some other name apart from 8080.

Restart the server and try with new port.

5. EJB is mainly for distributed application. It is following the RMI concept.

In RMI we are binding the Remote name and keep in the RMiregistry.
In Ejb We have a container it will provide lot of service like

1.persistance
2.Security
3.concurrency control.
4.Load balancing Etc
It will make our transction more powerful.

Invoking JSP Error page

Can I invoke a JSP error page from a servlet?
Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets.
If your servlet opens an OutputStream or PrintWriter, the JSP engine will throw the following translation error:
java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained
The following code snippet demonstrates the invocation of a JSP error page from within a controller servlet:
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 {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Reference : http://www.geekinterview.com/question_details/2410

November 1, 2009

Nice Servlets and Jsp questions

What is session?
A : The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests. The session is stored on the server.

What is servlet context ?
A: The servlet context is an object that contains a information about the Web application and container. Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use.

What is a servlet ?
A: servlet is a java program that runs inside a web container.

Can we use the constructor, instead of init(), to initialize servlet?
A: Yes. But you will not get the servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

How do I include static files within a JSP page?
A: Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase.

How can I implement a thread-safe JSP page?
A :You can make your JSPs thread-safe adding the directive <%@ page isThreadSafe="false" % > within your JSP page.

What is a Expression?
A: Expressions are act as place holders for language expression, expression is evaluated each time the page is accessed. This will be included in the service method of the generated servlet.

Reference : http://www.javacertificate.net/jsp_iqns.htm

October 25, 2009

Servlets and JSPs

GET requests can be bookmarked but POST can not be.
GET is idempotent(Its used for getting things from the server, its not supposed to change anything ) , POST is not

addHeader() and setHeader() methods of response : Both will add a header and value to response if the header is not already present. The difference between these two occurs when the header is already there.
setHeader() overwrites the existing value
addHeader() adds an additional value

We can not do a send redirect after writing to the response. If we try to do so, it will throw an llegal state exception

Difference b/w redirect and a request dispatch : redirect makes the client do the work and sendRedirect makes sth on the server do the work.

we can not use Servlet init param unless the servlet is initialised.

sendredirect is called on response and request dispatcher is called on request
response.sendRedirect() and request.getRequestDispatcher() and then dispatcher.forward()

Also, in case of request dispatcher, the URL is not changed, so the user does not get to know about that.