Structure of Web-Application: Tomcat
Structure of Web-Application: Tomcat
WebApps
projectname
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
/welcome
http://localhost:8080/mywebapp/welcome
→ War file can be created by using JAVA IDE like Netbeans, eclipse.
In eclipse war file can be created by right clicking the project and
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
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
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:
Note: When a JSP is compiled, it is converted into servlet by the web container.
3. Create a method int sum(int a,int b) to find sum of two integers in a 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.
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.
4. info
sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet
interface.
The web container will create a method getServletInfo() in the resulting servlet.For example:
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
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.
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
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:
<%/*multi
Scriptlet: <% %> whatever java code is written inside this tag.
Declaring a variable:
a. In a scriptlet:
b. In a declaration:
<%
try{
catch(Exception ex)
%>
test.jsp
--------
<html>
<body>
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>
</center>
</body>
</html>
footer.html
-----------
<html>
<body>
<center>
<h5>copyright@2021</h5>
</center>
</body>
</html>
test.jsp
-------
<html>
<body>
content
</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:
response-->HttpServletResponse
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
Object page=this;