JSP Questions
14.Q.What is a servlet?
1.Q. Are sessions created on the server side?
A: A servlet session is created and stored on the server side. The servlet container keeps track of all the sessions it manages and fulfils servlet API requests to get
HttpSessions
, manipulate object data stored with them and trigger event callbacks.
To the maintain the session, Web clients must pass back a valid session identifier as a cookie header value or dynamically generated URL parameter. In this sense, the session is also stored by the client, but only as a token reference.
2.Q: Can I create a session with GenericServlet?
A: There are no protocol-specific features in
GenericServlet
, which is an implementation of the basic, general purpose Servlet
interface. Servlet-based sessions are designed only for interactions using the HTTP protocol, which has two key features necessary for a servlet container to simulate continuous user sessions: cookies and URL-based navigation, which supports URL-rewriting. The servlet API therefore places the HttpSession
interface in the javax.servlet.http
package, and session references are only available through classes in this package.
3.Q: How can I assemble data from multiple input forms?
A: First, it is best to use a single servlet to handle each form submission. A single servlet for all input would be too complicated. Give each servlet responsibility to validate a single form input, and pass the error cases on to JSP documents that explain the problem and allow users to amend the input.
Servlet concepts
4.Q: What's the difference between applets and servlets?
A: There are many fundamental differences between
Applet
and Servlet
classes, the Java API documentation for the two types will show you they have little in common.
Applets are essentially graphical user interface (GUI) applications that run on the client side in a network environment, typically embedded in an HTML page. Applets are normally based on Abstract Windowing Toolkit components to maintain backward-compatibility with the widest range of browsers' Java implementations. The application classes are downloaded to the client and run in a Java Virtual Machine provided by the browser, in a restrictive security environment called a "sandbox".
Servlets are used to dynamically generate HTTP responses and return HTML content to Web browsers on the server side. Servlets are often used to validate and process HTML form submissions and control a series of user interactions in what is known as a Web application. Servlets can be used to control all aspects of the request and response exchange between a Web browser and the server, called a servlet container.
5.Q: How can I write a servlet using Javascript?
A: Java servlets is a server side technology that delivers dynamic content to Web browsers and other clients. Javascript is also delivered by a Web server, but the code is only interpreted and executed after it has been downloaded by the Web browser. This means that it is not possible to write servlet code in Javascript.
It is possible to include Javascript in the output of servlets and Java Server Pages, just like standard Web pages. It is also possible to dynamically generate Javascript using a servlet and use it as the source for a script tag, though this is only advisable in rare cases.
6.Q: How does the JVM execute a servlet compared with a regular Java class?
A: Servlets are standard Java classes and are executed by the Java Virtual Machine in exactly the same way as any other. However, the environment or context in which servlets are executed is different. A servlet is not invoked directly through a
main
method, the class is loaded and run by a servlet container.
7.Q.Can I include normal Java classes in servlets?
A: Any Java class can be used in a Web application, provided you make the classes available to the servlet container at runtime. The Java API classes can be used directly by adding import statements to your servlet class. Other supporting classes can also be imported, but these classes must be added to the
classes
or lib
directory of your application.
If you need to configure the supporting classes, this can be done with standard servlet configuration features using the
ServletConfig
and ServletContext
objects available to the init(ServletConfig)
method.
8.Q: How can I tell when a servlet is instantiated?
A: A servlet must be instantiated before it is brought into service by the servlet container, so one way to check is to make a request to the servlet and check the response. If you need to check indirectly, you can override the
init(ServletConfig)
method and add log(String)
statements to it. This method is called after the servlet container has instantiated the servlet before it is brought into service.
9.Q: What is URL-rewriting?
A: URL-rewriting is a way of maintaining a session between an HTTP client and a servlet container which does not use cookies. Rather than exchange a session ID in a cookie, the servlet container includes it in the hyperlink URLs it generates for servlets and JSP.
10.Q: Are servlets multi-threaded?
A: Yes, servlets are normally multi-threaded. The servlet container allocates a thread for each new request for a single servlet without any special programming. Each thread of your servlet runs as if a single user were accessing it alone, but you can use static variables to store and present information that is common to all threads, like a hit counter for instance.
11.Q.How can I pass values between JSPs without using sessions?
A: There are four main alternatives to full session-based transfer of data values, two of them use features of the session tracking API: cookies and URL-rewriting, detailed below.
12.Q.What is JavaServer Pages technology?
JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content. The JSP specification, developed through an industry-wide initiative led by Sun Microsystems, defines the interaction between the server and the JSP page, and describes the format and syntax of the page.
13.Q,How does the JavaServer Pages technology work?
JSP pages use XML tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page. It passes any formatting (HTML or XML) tags directly back to the response page. In this way, JSP pages separate the page logic from its design and display.
JSP technology is part of the Java technology family. JSP pages are compiled into servlets and may call JavaBeans components (beans) or Enterprise JavaBeans components (enterprise beans) to perform processing on the server. As such, JSP technology is a key component in a highly scalable architecture for web-based applications.
JSP pages are not restricted to any specific platform or web server. The JSP specification represents a broad spectrum of industry input.
JSP technology is part of the Java technology family. JSP pages are compiled into servlets and may call JavaBeans components (beans) or Enterprise JavaBeans components (enterprise beans) to perform processing on the server. As such, JSP technology is a key component in a highly scalable architecture for web-based applications.
JSP pages are not restricted to any specific platform or web server. The JSP specification represents a broad spectrum of industry input.
A servlet is a program written in the Java programming language that runs on the server, as opposed to the browser (applets). Detailed information can be found at http://java.sun.com/products/servlet.
15.Q.Why do I need JSP technology if I already have servlets?
JSP pages are compiled into servlets, so theoretically you could write servlets to support your web-based applications. However, JSP technology was designed to simplify the process of creating pages by separating web presentation from web content. In many applications, the response sent to the client is a combination of template data and dynamically-generated data. In this situation, it is much easier to work with JSP pages than to do everything with servlets.
16.Q.Where can I get the most current version of the JSP specification?
The JavaServer Pages 2.1 specification is available for download from here.
17.Q.How does the JSP specification relate to the Java Enterprise Edition 5 Platform?
The JSP 2.1 specification is an important part of the Java EE 5 Platform. Using JSP and Enterprise JavaBeans technologies together is a great way to implement distributed enterprise applications with web-based front ends.
18.Q.Which web servers support JSP technology?
There are a number of JSP technology implementations for different web servers. The latest information on officially-announced support can be found athttp://java.sun.com/products/jsp/industry.html.
19.Q.Is Sun providing a reference implementation for the JSP specification?
The GlassFish project is Sun's free, open-source Java EE 5 implementation. It includes an implementation of JSP technology version 2.1. You can download GlassFish builds from https://glassfish.dev.java.net/.
20.Q.How is JSP technology different from other products?
JSP technology is the result of industry collaboration and is designed to be an open, industry-standard method supporting numerous servers, browsers and tools. JSP technology speeds development with reusable components and tags, instead of relying heavily on scripting within the page itself. All JSP implementations support a Java programming language-based scripting language, which provides inherent scalability and support for complex operations.
21.Q.Where do I get more information on JSP technology?
The first place to check for information on JSP technology ishttp://java.sun.com/products/jsp/. This site includes numerous resources, as well as pointers to mailing lists and discussion groups for JSP technology-related topics.
No comments:
Post a Comment