Web Development in Java
Servlets and JSP (Java Server Pages):
Web development in Java is based on Java-based technologies that it is to build dynamic and interactive web applications. Java provides a range of tools and frameworks to develop robust, scalable, and secure web applications.
Servlets:
- Servlets
are Java classes that dynamically process and respond to requests on the
server-side.
- They
handle HTTP requests and generate dynamic content by writing Java code
directly.
- Servlets
can manage complex tasks
- Interact
with databases
- Handle
business logic
- Generate
responses in various formats (HTML, JSON, XML, etc.).
- Requires
a significant amount of Java programming
- It
involves mixing code with HTML
- It
may not be as readable or maintainable in larger applications.
It extends the HttpServlet class provided by the Java Servlet API.
- The
doGet() and doPost() methods are inherited from the HttpServlet
class.
- It can be overridden in a servlet class to handle specific HTTP request types.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//Used to dynamic Webpage
public class MyServlet extends HttpServlet {
// Override doGet() for handling HTTP GET requests
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Logic to handle GET requests
}
// Override doPost() for handling HTTP POST requests
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Logic to handle POST requests
}
}
JSP (Java Server Pages):
- It
is a technology that simplifies the creation of dynamic web content using HTML-like
syntax with embedded Java code.
- Are
text-based templates that contain HTML along with Java code snippets enclosed
in <% %> tags.
- It
allows developers to separate presentation logic (HTML) from business logic
(Java code), making it easier for web designers and developers to collaborate.
- JSP
pages are compiled into Servlets by the server before execution.
<html>
<head>
<title>JSP Page Demonstration</title>
</head>
<body>
<h1>Hello, From JSP!</h1>
<%-- Java code embedded within JSP --%>
<%
String msg = "Welcome to the World of JSP!";
out.println("<p>" + msg + "</p>");
%>
<%-- Using JSP Expression --%>
<p>Today's date and time: <%= new java.util.Date() %></p>
</body>
</html>
Developing Dynamic and Interactive Website in Java:
A
Website is a collection of Webpages. It is accessed through the internet. A
website is based on Client-Server model. A Web page is either a static page or
a dynamic page. The attached video demonstrates the development of an
interactive and dynamic website of two pages using Java Technologies for Web development.
A static webpage in HTML accepts a number using HTML form and validation is performed using client-side script. A dynamic webpage is created using both Servlet and JSP to generate the table of the entered number.
Static Web Page:
It is developed as a HTML page with HTML form to collect the number from the user interacting with this page in the client browser. The action attribute of the HTML form specifies either the name of the Servlet or the JSP page which will generate the dynamic Webpage to the display the table of the accepted number.
<!DOCTYPE html>
<html>
<head>
<title>Number Table Generator Using Servlet or JSP</title>
</head>
<body>
<h1>Form Action attribute specify name of Servlet to generate Dynamic Webpage</h1>
<h2>Enter a Number</h2>
<form action="tableGenerator.jsp" method="get">
<label for="number">Enter a Number:</label>
<input type="number" id="number" name="number" required>
<input type="submit" value="Generate Table">
</form>
</body>
</html>
Dynamic Web Page:
Dynamic web pages are more interactive. It can display content that changes based on user interactions or data input. The content may be personalized for each user. Here, either Servlet or JSP is used to generate the dynamic Webpage to display the table of the accepted number.
Servlet Code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TableGeneratorServlet
*/
@WebServlet("/TableGeneratorServlet")
public class TableGeneratorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TableGeneratorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Get the parameter value for the accepted number
String acceptedNumberParam = request.getParameter("number");
int acceptedNumber = Integer.parseInt(acceptedNumberParam);
// Generate and display the table of accepted number
out.println("<html><body>");
out.println("<h1>Generated Table Using Servlet</h1>");
out.println("<h2>Table of Accepted Number (" + acceptedNumber + ")</h2>");
out.println("<table border='1'>");
for (int i = 1; i <= 10; i++) {
out.println("<tr><td>" + acceptedNumber + "</td><td> x </td><td>" + i + "</td><td> = </td><td>" + (acceptedNumber * i) + "</td></tr>");
}
out.println("</table>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
JSP Code:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Number Table</title>
</head>
<body>
<h2>Generated Table Using JSP</h2>
<%
String numberStr = request.getParameter("number");
int number = Integer.parseInt(numberStr);
%>
<table border="1">
<% for (int i = 1; i <= 10; i++) { %>
<tr>
<td><%= number %></td><td> x </td><td><%= i %></td>
<td> = </td>
<td><%= number * i %></td>
</tr>
<% } %>
</table>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TableGeneratorServlet
*/
@WebServlet("/TableGeneratorServlet")
public class TableGeneratorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TableGeneratorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Get the parameter value for the accepted number
String acceptedNumberParam = request.getParameter("number");
int acceptedNumber = Integer.parseInt(acceptedNumberParam);
// Generate and display the table of accepted number
out.println("<html><body>");
out.println("<h1>Generated Table Using Servlet</h1>");
out.println("<h2>Table of Accepted Number (" + acceptedNumber + ")</h2>");
out.println("<table border='1'>");
for (int i = 1; i <= 10; i++) {
out.println("<tr><td>" + acceptedNumber + "</td><td> x </td><td>" + i + "</td><td> = </td><td>" + (acceptedNumber * i) + "</td></tr>");
}
out.println("</table>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Number Table</title>
</head>
<body>
<h2>Generated Table Using JSP</h2>
<%
String numberStr = request.getParameter("number");
int number = Integer.parseInt(numberStr);
%>
<table border="1">
<% for (int i = 1; i <= 10; i++) { %>
<tr>
<td><%= number %></td><td> x </td><td><%= i %></td>
<td> = </td>
<td><%= number * i %></td>
</tr>
<% } %>
</table>
</body>
</html>
Code Explanation:
- The
<%@ page %> directive is used to set the page's language, character
encoding, and other settings.
- HTML
content (headings, paragraphs) can be written directly within the JSP.
- Java
code can be embedded within <% %> tags, allowing dynamic content
generation.
- The
out.println() method outputs content to the HTML page.
- JSP Expression <%= %> evaluates expressions and directly prints the result into the HTML output.
Think:
Servlets
focus on handling the business logic and generating responses programmatically
using Java.
JSP
separates the presentation layer (HTML) from the logic layer (Java code),
allowing easier management of code and design in web applications.
In
practice, JSPs and Servlets are both used together: Servlets handle the
backend processing and business logic, while JSPs handle the presentation and
structure of the web pages. This combination provides a structured and
manageable way to create dynamic and interactive web applications in Java.
Java-based frameworks like Spring MVC, Struts, and Java Server Faces (JSF) provide ready-made components, MVC (Model-View-Controller) architecture, and other features to streamline and simplify web development.
No comments: