0% found this document useful (0 votes)
0 views12 pages

Structure of Web-Application: Tomcat

The document outlines the structure and deployment methods of a web application using servlets and JSP, detailing manual deployment and WAR file creation. It compares servlets and JSP in terms of coding complexity, roles in MVC architecture, and session management. Additionally, it explains JSP architecture, directives, implicit objects, and exception handling in JSP.

Uploaded by

prernasingh9804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views12 pages

Structure of Web-Application: Tomcat

The document outlines the structure and deployment methods of a web application using servlets and JSP, detailing manual deployment and WAR file creation. It compares servlets and JSP in terms of coding complexity, roles in MVC architecture, and session management. Additionally, it explains JSP architecture, directives, implicit objects, and exception handling in JSP.

Uploaded by

prernasingh9804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Tomcat

WebApps

projectname

WEB-INF JSP HTML

classes lib web.xml

.class files Jar files


(servlet/POJO
class)

Structure of web-application
How is a servlet (web application) deployed? Explain.
→ A servlet (web application) can be deployed manually or creating a war (web application
archive) file.

1. To manually deploy a servlet we create the hierarchy of folders as above and place the
required files in the respective folders. Then start the server and access the resource.

For eg

a. if project name is “mywebapp”, then to access the index file

http://localhost:8080/mywebapp (/index.html) optional

b. if project name is “mywebapp”, then to access a servlet with url pattern

/welcome

http://localhost:8080/mywebapp/welcome

2. By creating war file

→ War file can be created by using JAVA IDE like Netbeans, eclipse.

In netbeans war file can be created by right clicking on project and

choosing the clean and build option.

In eclipse war file can be created by right clicking the project and

choosing export option.

The war file created is then placed in the “webapps” folder as shown in

figure. And then the server is started which will create the same

hierarchy of folders as shown in the above figure.


Servlet JSP
Writing code for servlet is harder than JSP is easy to code as it is java in html.
JSP as it is html in java.

Servlet plays a controller role in MVC JSP is the view in MVC approach for
approach. showing output

Servlet can accept all protocol requests. JSP only accept http requests

In Servlet, we can override the service()


method. In JSP, we cannot override its service()
method.
In Servlet by default session
management is not enabled, user have
to enable it explicitly. In JSP session management is
automatically enabled.
Servlet is faster than JSP. JSP is slower than Servlet because the first
step in JSP lifecycle is the translation of JSP
to servlet and then compile.
What is JSP Architecture?

Following steps includes the JSP Architecture as shown in the above figure.

1. Web client sends request to Web server for a JSP page (extension .jsp).
2. As per the request, the Web server (here after called as JSP container) loads the
page.
3. JSP container converts (or translates) the JSP file into Servlet source code file (with
extension .java). This is known as translation.
4. The translated .java file of Servlet is compiled that results to Servlet .class file. This
is known as compilation.
5. The .class file of Servlet is executed and output of execution is sent to the client
as response.
JSP (Java Server Pages)
--> Like Servlet, JSP is also a java program that helps us to create dynamic web application by
communicating with the database.

--> Advantages:

-->Faster loading of pages.

-->Nobody can borrow the code.

-->No browser compatibility issues

-->Supported by a number of web servers.

Note: When a JSP is compiled, it is converted into servlet by the web container.

1. Create a JSP to display Tribhuvan University 10 times.

2. Create a JSP to count no. of times it has been visited.

3. Create a method int sum(int a,int b) to find sum of two integers in a JSP.

4. Create a JSP to display current date and time.

[Hint: import java.util.Date]

To import multiple packages:

<%@ page import="java.util.*, java.net.*" %>

Reading HTML form parameters in a JSP.

a. To display Hello name [name provided from HTML form]

b. To check odd even

c. find sum of two numbers

d. find the length of a word


Directive: A directive is a way to give special instructions to the Container at page translation time.
They do not produce any output that is visible to the client.

The directives are characterized using <%@ %> tags.

Following are the directives available in JSP:

a. page --> The page directive allows importing classes, setting content type and so on. It can be placed
anywhere within the document.

b. include-->The include directive is used to include HTML file, JSP or servlet into a JSP file. Usually the
included files are for tables, headers and footers.

c. taglib-->The taglib directive allows the page to use custom tags inside the JSP page.

Attributes of page directive


1. import --> used to import class/interface of a package.

2. contentType

defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is
"text/html".

3. buffer

sets the buffer size in kilobytes to handle output generated by the JSP page.The default size of the buffer
is 8Kb.

<%@ page buffer="16kb" %>

4. info

sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet
interface.

<%@ page info="Developed by Dynamite Solutions" %>

The web container will create a method getServletInfo() in the resulting servlet.For example:

public String getServletInfo() {


return "Developed by Dynamite Solutions";

5. errorPage

used to define the error page, if exception occurs in the current page, it will be redirected to the error
page.

6. isErrorPage

used to declare that the current page is the error page.

7. isThreadSafe

Servlet and JSP both are multithreaded.Default value of isThreadSafe is true.If you make it false, the web
container will serialize the multiple requests, i.e. it will wait until the JSP finishes responding to a request
before passing another request to it.

<%@ page isThreadSafe="false" %>

8. language

specifies the scripting language used in the JSP page. The default value is "java".

9. isELIgnored

We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its value is
false i.e. Expression Language is enabled by default.

10. session

used to disable a session in JSP. By default session is enabled in JSP.

11. pageEncoding
defines the character encoding used for a JSP file. Can be "UTF-8", "UTF-16" etc.

12. autoFlush

used to control the behaviour of the buffer. Buffer will be flushed automatically when full if autoFlush is
true otherwise throw an exception.

JSP comments:

<%//single line comment %>

<%-- single line comment & multiline both --%>

<%/*multi

line comment */%>

Scriptlet: <% %> whatever java code is written inside this tag.

Expression: <%= %>whatever java code is written inside this tag.

Note: in JSP we can remove out.println by using expression.

Declaring a variable:

a. In a scriptlet:

<% int count=0; %> here count becomes a local variable

b. In a declaration:

<%! int count=0; %>

-->declarations are for static and instance variables and methods.


Exception Handling in JSP

--> Exception can be handled in two ways:

1. using try/catch inside the scriptlet.

<%

try{

catch(Exception ex)

%>

2. Creating an error page to handle the exception.

test.jsp

--------

<%@ page errorPage="exc.jsp" contentType="text/html" pageEncoding="UTF-8" %>

<html>

<body>

<% int c=5/0;

out.println(c);

%>

</body>

</html>

exc.jsp

-------
<%@ page isErrorPage="true" contentType="text/html" pageEncoding="UTF-8" %>

<html>

<body>

<%

out.println(exception);

%>

</body>

</html>

Write a program to include the content of files (header.html and footer.html) into a JSP

header.html

-----------

<html>

<body>

<center>

<h1>Dynamite Solutions </h1>

<h5>Putalisadak, Kathmandu </h5>

</center>

</body>

</html>

footer.html

-----------

<html>
<body>

<center>

<h5>copyright@2021</h5>

</center>

</body>

</html>

test.jsp

-------

<html>

<body>

<%@ include file="header.html" %>

content

<%@ include file="footer.html" %>

</body>

</html>

Session in jsp:

By default JSP have session tracking enabled and a new HttpSession object is instantiated for each new
client automatically. Disabling session tracking requires explicitly turning it off as follows:

<%@page session="false" %>

JSP implicit objects:

out --> JspWriter

used for writing data to the buffer.


request--> HttpServletRequest

used for reading HTML form parameters, cookies.

response-->HttpServletResponse

used for sending cookie to client, redirecting to another resource.

session-->HttpSession

used to set, get or remove the attribute on session object and check its creation time, last accessed
time.

application-->ServletContext

used to get initialization parameter from configuaration file (web.xml). Is not specific to a particular
JSP. The application object is created only once by the web container when the project is deployed on
the server.

config--> ServletConfig

used to get initialization parameter for a particular JSP page. The config object is created by the web
container for each jsp page.

exception--> Throwable

used to print the exception. But it can only be used in error pages.

page-->Object

is assigned to the reference of auto generated servlet class. It is written as:

Object page=this;

You might also like