31/12/2019

JSP in Java | Java Server Page in Java :Studywow

Studywow: Studywow presents the detailed  description or main topics of JSP or Java Server Page in Java.

Q1. What is JSP? or what is Java Server Page?

Ans. Java Server Pages (JSP) is a technology that helps software developers to create dynamically generated web pages based on HTML, XML, or other document types. To deploy and run there Java Server Pages, a compatible web server with a servlet container, such as Apache Tomcat or Jetty, is required. It can be thought of as an extension to servlet because it provides more functionality than servlet şuch as expression language, JSTC etc.

Q2. Describing the JSP working?

Ans. JSP can be used independently or as the view component of a server-side model-view-controller design, normally with JavaBeans as the model and Java servlets (or a framework such as Apache Struts) as the controller. JSP allows Java code and certain pre-defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver a document. The compiled pages, as well as any dependent Java libraries, use Java bytecode rather than a native software format. Like any other lava program, they must be executed within a Java Virtual Machine (IVM that integrates with the server's host operating system to provide an abstract platform-neutral environment.

Q3. What is the differences between Java Servlet and JSP ?

Ans. Difference between Java Servlet and JSP :

1. Definition of JSP and Java Servlet :-

Java Server Page: JSP is a webpage scripting language.
Servlet: Servlet are the Java programs.

2. Speed of JSP and Java Servlet :-

Java server page: It run slower and takes time to convert into Java servlets.
Servlet: It run faster compared to JSP.

3. Model View Controller :-

Java server page:  In this, it act as a view.
Servlet: It this, it acts as a controller.

4. Execution :-

Java server page: It executes inside a web server.
Servlet: It is compiled into a Java servlet.

5. Custom tag :-

Java server page: Can be build and call directly by Javabeans.
Servlet: There is no such custom tag.

6. Which is Easy from JSP and Java Servlet :-

Java server page: To code in JSP is more easy.
Servlet: To code in servlet is more difficult.

Q4. Describe the architecture of JSP (Java Server Page)?
Or How to generate dynamic content in JSP?

Ans. The web server needs a JSP engine i.e. container to process JSP pages. The JSP container is responsible for intercepting requests for JSP pages. A JSP container works with the web server to provide the runtime environment and other services a JSP needs. The following diagram shows the JSP architecture:

JSP Architecture Flow : To process the flow of JSP architecture, you need to follow some basic steps as discussed below :
Step 1: The user goes to a JSP page and makes the request via Internet in user's web browser.
Step 2: Web browser sends on HTTP request to the web server requesting JSP page.
Step 3: Web server accepts the requested.jsp file and pass the JSP file to the JSP servlet engine.
Step 4: Now, JSP engine loads the JSP page from disk and converts it into a servlet.
Step 5: Servlet produces an output in HTML format.
Step 6: The output passes to the web server inside an HTTP response.
Step 7: Web server sends the HTTP response to web browser in the form of static HTML content.
Step 8: Web browser loads the static page into the browser and thus user can view the dynamically generated page.
Typically, the JSP engine checks to see whether a servlet for a JSP file already exists and whether the modification date on the JSP is older than the servlet. If the JSP is older than its generated servlet, the JSP container assumes that the JSP hasn't changed and that the generated servlet still matches the ISP's contents. This makes the process more efficient than with other scripting languages (such as PHP) and therefore faster.
So in a way, a JSP page is really just another way to write a servlet without having to be a Java programming wiz. Except for the translation phase, a JSP page is handled exactly like a regular servlet.

Q5. Advantages of JSP (Java Server Page)?

Ans. Some of the advantages of JSP are:
(a) It allows tag based programming. So, extensive java knowledge is not required.
(b) It is suitable for both Java and non-java programmer.
(c) There are nine implicit objects which can be used directly in our JSP program.
(d) It takes care of exception handling.
(e) Modifications done in JSP program are recognised by underlying server automatically without reloading of web application.
(f) It allows us to use separate presentation logic (html) from Java code (business logic).
(g) It increases readabiltiy of code because of tags.
(h) It gives built-in JSP tags and also allows to develop custom JSP tags and to use third party supplied tags.
(i) It is easy to learn as well as easy to apply.

Q6. Write down the various components of JSP (Java Server Page)?

Ans. Components of JSP (Java Server Page): JSPs are comprised of standard HTML tags and JSP tags. The structure of JavaServer pages are simple and easily handled by the servlet engine. In addition to HTML, JSPs components can be categorised as follows:

(a) Directives:

A directives tag always appears at the top of your JSP file. It is global definition sent to the ISP engine. Directives contain special processing instructions for the web container. You can import packages, define error handling pages or the session information of the JSP page. Directives are defined by using <%@ and %> tags.
Syntax: <%@ directive attribute="value" %>

(b) Declarations:

This tag is used for defining the functions and variables to be used in the JSP. This element of JSPs contains the Java variables and methods which you can call in expression block of JSP page. Declarations are defined by using <%! and %> tags. Whatever you declare within these tags will be visible to the rest of the page.
Syntax: <%! declaration (s) %>

(c) Scriptlets:

In this tag we can insert any amount of valid Java code and these codes are placed in jsp Service method by the JSP engine. Scriptlets can be used anywhere in the page. Scriptlets are defined by using <% and %> tags.
Syntax: <% Scriptlets%>

(d) Comments:

Comments help in understanding what is actually code doing. JSPs provides two types of comments for putting comment in your page. First type of comment is for output comment which is appeared in the output stream on the browser. It is written by using the <!-- and --> tags.
Syntax: <!-- comment text -->
Second type of comment is not delivered to the browser. It is written by using the <% -- and --%> tags.
Syntax; <%-- comment text --%>

(e) Expressions:

Expressions in JSPs is used to output any data on the generated page. These data are automatically converted to string and printed on the output stream. It is an instruction to the web container for executing the code with in the expression and replace it with the resultant output content. For writing expression in JSP, you can use <%= and %> tags.
Syntax: <%= expression %>


Studywow: Read More Topics on Java (click here).


Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!