Web Technology Practical No. 6
Web Technology Practical No. 6
Problem Statement: Implement the sample program demonstrating the use of JSP.
Objectives: To use client side and server side web technologies.
Outcome: Apply the client side and server side technologies for web application
development
Software & Hardware Requirments:
Operating System: Ubuntu
Software: Eclipse
Programming Language: Java(Servlet/JSP)
Server: Tomcat
Browser: Mozilla/Google Chrome
Hardware: i3 Processor, 4GB RAM, 500GB HDD
Theory:
1. JavaServer Pages.
JavaServer Pages (JSP) is a technology for developing web pages that support dynamic
content which helps developers insert java code in HTML pages by making use of special JSP
tags, most of which start with <% and end with %>.
A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role
of a user interface for a Java web application. Web developers write JSPs as text files that
combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.
Using JSP, you can collect input from users through web page forms, present records
from a database or another source, and create web pages dynamically.
JSP tags can be used for a variety of purposes, such as retrieving information from a
database or registering user preferences, accessing JavaBeans components, passing control
between pages and sharing information between requests, pages etc.
JSP Processing:
The following steps explain how the web server creates the web page using JSP:
● As with a normal page, your browser sends an HTTP request to the web server.
● The web server recognizes that the HTTP request is for a JSP page and forwards it to a
JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of
.html.
● The JSP engine loads the JSP page from disk and converts it into a servlet content. This
conversion is very simple in which all template text is converted to println( ) statements
and all JSP elements are converted to Java code that implements the corresponding
dynamic behavior of the page.
● The JSP engine compiles the servlet into an executable class and forwards the original
request to a servlet engine.
● A part of the web server called the servlet engine loads the Servlet class and executes it.
During execution, the servlet produces an output in HTML format, which the servlet
engine passes to the web server inside an HTTP response.
● The web server forwards the HTTP response to your browser in terms of static HTML
content.
● Finally web browser handles the dynamically generated HTML page inside the HTTP
response exactly as if it were a static page.
● All the above mentioned steps can be shown below in the following diagram:
// Initialization code...
Typically initialization is performed only once and as with the servlet init method, you
generally initialize database connections, open files, and create lookup tables in the jspInit
method.
1.4.2 JSP Execution:
This phase of the JSP life cycle represents all interactions with requests until the JSP is
destroyed. Whenever a browser requests a JSP and the page has been loaded and initialized, the
JSP engine invokes the _jspService() method in the JSP.
The _jspService() method takes an HttpServletRequest and an HttpServletResponse as
its parameters as follows:
HttpServletResponse response)
The _jspService() method of a JSP is invoked once per a request and is responsible for
generating the response for that request and this method is also responsible for generating
responses to all seven of the HTTP methods ie. GET, POST, DELETE etc.
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
%>
</body>
</html>
<body>
</body>
</html>
Conclusion:
Hence, we have successfully designed web page to demonstrate the use of JSP.
.
Program
showinfo.jsp
<%@pageimport="java.sql.*"language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.print("<h2>Student Information Table</h2>");
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from studentinfo");
out.print("<center><table border=\"1\">");
out.print("<tr><th>Id</th><th>Student
Name</th><th>Class</th><th>Division</th><th>City</th></tr>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rs.getString(1)+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(5)+"</td>");
out.print("</tr>");
}
out.print("</table></center>");
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
DataBase:
OutPut:
FAQs:
1. What is JSP?
2. What is the syntax of JSP?
3. How do we connect JSP file to database?
4. What is session management?
5. Explain advantages of JSP over Servlet?
6. Explain Scriptlet, Expression, Declaration tag in JSP?
7. List default object in JSP?