Web Technologies Based on Java

  • By Pooja Nandode-Bhavsar
  • June 6, 2023
  • JAVA Programming
Web Technologies based on Java

Web Technologies Based on Java

  • Servlet

In this blog, I am going to discuss web technologies based on Java to develop java based web applications. Servlet and JSP are the two java based technologies using which we are able to develop web applications.

 

So, let’s first understand what is meant by Web Application.

Web application is software that runs on a web browser and basic web application consists of client and server architecture.

In which the client can send requests for the resources(a piece of information) to the server then the server will provide a response to the client’s request.

 

The request sent by the client to the server is of two types 

1. Static request

In which a response is  fixed for each request

 

2. Dynamic request

In which the client gets a new  or different response every time.

Every Server can send the response for static requests but the server does not have the response for dynamic requests. So the server sends that dynamic request coming from the client to its helper application(also called a servlet) then the helper application receives that dynamic request from a client, processes that request and generates a dynamic web response for that dynamic request then sends back that response to the server then the server sends that response on to the client web browser. Each server can contain multiple servlets in its web container. So Servlet is a server-side web technology used to generate dynamic web responses.

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

What is a servlet?

Servlet technology is used to develop a web application. Servlets reside at the server side and are used to generate a dynamic web page.

The servlet is an API/package that provides many interfaces and classes including documentation.  

The servlet is an interface that comes under Javax. servlet packaged and this servlet interface must be implemented for creating any Servlet.

The servlet is a class that enhances the capabilities of the servers and responds to incoming requests.

Servlets can respond to any type of request.

The servlet is a web component that is deployed/add on the server to create a dynamic web page.

Web Technologies based on Java

 

How to create a servlet application

 

1. Open eclipse IDE 

 

2. Go to file -> new-> dynamic web project-> give it a name as “First Servlet Application”->Next -> click on generate deployment descriptor file ->finish

It will create a servlet application

 

Each servlet application consists of at least 3 files

1. Web. xml which is the deployment descriptor file

In which we can specify for which user request which servlet should be called

2. Index.html file which acts as a client Means through this client page we can send requests to the server.

3. FirstServlet.java which is acts as a servlet that accepts requests from the server and generates responses after processing the client request

 

There are 3 ways to make any java class a servlet

1. By implementing the Servlet interface

2. by extending HttpServlet class

3. by extending GenericServlet class

 

The most commonly used way is by extending HttpServlet class

 

As we run the project. It is deployed on the server then the server creates two important objects for each servlet i.e request and response object

These objects will get processed as the application gets further processed

 

Note: Looking for Java training in Pune? Master the fundamentals, object-oriented concepts, and advanced Java concepts with hands-on exercises and real-world projects

 

Program to implement Servlet Programming in Java.

 

In the below program, we are doing the addition of 2 numbers using Servlet

 

1.Index.Html

 

<!DOCTYPE html>

<html>

<head>

<meta charset=”ISO-8859-1″>

<title>Insert title here</title>

</head>

<body>

 

      

 <form action =”add” >

       

            Enter 1st number : <input type=”text” name=”num1″><br>

            Enter 2nd number : <input type=”text” name=”num2″><br>

            <input type =”submit”>

      

       </form>

</body>

</html>

 

 

In the above HTML code we are creating 2 text field where users can enter 2 numbers. As the user clicks on the submit button, 2 numbers are sent in the request as a parameter to the server with the action name “add”. Then the server accepts that request from the client and checks in the web.xml file for “/add”  url which servlet should be mapped. 

 

In the below, the web.xml file for “/add” url  “p1.AddServ”  servlet should be called. Here,  p1 is the name of the package

 

In web.xml file we can provide configuration for servlet using two main tag, <servlet>

And <servlet-mapping>

 

In  <servlet> tag, we can provide the information(fully qualified name of servlet) of servlet and  in  <servlet-mapping> tag we need to provide url pattern of request. I.e “/add”  here..

 

2.web.xml

 

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” id=”WebApp_ID” version=”3.1″>

  <display-name>ServletProject</display-name>

 

<servlet>

 

<servlet-name>abc</servlet-name>

 

<servlet-class>p1.AddServ</servlet-class>

 

</servlet>

 

<servlet-mapping>

 

<servlet-name>abc</servlet-name>

 

<url-pattern>/add</url-pattern>

 

</servlet-mapping>

</web-app>

 

The below servlet will accepts request from client using request.getParameter() method, here in each servlet we need to creates request and response object. Where request is the object of HttpServletRequest interface and response is the object of HttpServletResponse interface. After getting data from request, servlet can process that request and generates web response and then it adds that response in the “response” object and sends back that response object to the server. Server then sends back that response to the client web page.

 

  1. AddServlet.java

 

package servletDemo;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class AddServ extends HttpServlet

{

public void service(HttpServletRequest req, HttpServletResponse res) throws IOException , ServletException

     {

     int i= Integer.parseInt(req.getParameter(“num1”));

     int j= Integer.parseInt(req.getParameter(“num2”));

    

     int k= i+j;

     //System.out.println

    

      PrintWriter out = res.getWriter();

     out.println(“addition is = “+k);

     }

}

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

 

  • JSP

 

Jsp is another web based java technology through which we can develop web based application. JSP stands for Java Server Pages which is again a server side technology just like a servlet. Because, both servlet and jsp can be run on the server. Jsp can be considered as an extension of servlet because jsp consists of all the feature and concepts of servlet plus jsp has it’s own features like expression language, taglib directives, and jstl tag library which make jsp more powerful than servlet

 

In servlet, we need to create separate files for writing html code and java code. But using jsp, inside one file we can write html and java code together means inside single file we can represent presentation logic and business logic

 

So we can say, 

Jsp = html + java

 

As Jsp file can contain both type of code(html+java), we need to specify that which is the code of java otherwise Otherwise..all code will be considered as html code.So define java code inside html file there are 3 types of tags are available in jsp and that tag are as follows:

 

1. Scriptlet tag 

Within scriptlet tag we can define any type of java code

Symbolically scriptlet tag is enclosed as <%   %>

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″

    pageEncoding=”ISO-8859-1″%>

<!DOCTYPE html>

<html>

<head>

<meta charset=”ISO-8859-1″>

<title>Insert title here</title>

</head>

<body>

 

     <h1>Scriplet Tag Demo</h1>

 

      <%

           int a=10,b=8;

           out.print(“add of 2 numbers =”+(a+b));

             

      

      %>

</body>

</html>

 

Note: Gain comprehensive knowledge and practical skills from SevenMentor. Kickstart your career as a Java developer and stay ahead in the competitive IT industry with a top-notch Java Course in Pune.

2. Declaration tag

Within declaration tag we can define variables and methods

Symbolically the declaration tag is enclosed in <%!    %>

 

3. expression tag

Within the expression tag we Can write printing statements means whatever we have written inside expression tag will be print on the browser hence we don’t need to use out.print statement inside expression tag for printing data. Aslo we don’t have to terminate the statements with semicolon inside expression tag as shown in below code

 

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″

    pageEncoding=”ISO-8859-1″%>

<!DOCTYPE html>

<html>

<head>

<meta charset=”ISO-8859-1″>

<title>Insert title here</title>

</head>

<body>

 

  <%–   <% %> scriptlet tag

    <%! %> declaration tag 

    <%= %> expression tag     –%>

    

   <!–  declaration tag used for declaring variables and methods –>

    

      <%!

            double pi=3.14;               /* declaring varialbes */

    int r=5;

   

          double  areaOfCircle()       /* declaring methods */

          {

          return pi*r*r;

          }

         %>

    

  <!–  Expression tag used for printing statements on the browser –>

    <!– In an Expression tag, we don’t need to use out.print, and also the statements written inside expression tag should not be terminate with semicolon –>

          <%=

             “area of circle  = “+areaOfCircle()

          %>

 

   

    

</body>

</html>

 

The expression tag is enclosed within <%=      %>

Do watch our video on Java: Click Here

Author:-

Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*