Java/J2EE and SOA (176 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Java/J2EE and SOA (176 Blogs)
  • Spring Framework (7 Blogs)
  • PHP and MySQL (23 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Dynamic Web Pages In Java: How To Create Web Pages In Java?

Published on Jul 19,2019 39 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Dynamic Web pages are the need of the hour. The major reason being the need to meet the requirement of constantly changing content at a fast pace. This article focuses on Dynamic web pages in Java. Following pointers will be covered in this article.

Let us get started with Dynamic Web Pages in Java article,

Dynamic Web Pages

 Dynamic web pages are server-side web pages, each time it is viewed, we see different content.It is controlled by Application server processing server-side scripts. The dynamic web pages can also change their contents on the request of the client. They have the capability to generate new content according to time and need. Which simply means that dynamic web pages are never the same for all users. 

We all are well aware of the need for dynamic web pages in day to day life.

The best example of a dynamic web page we always see is captcha.

The main difference between static and dynamic web pages is that static web page remains the same for all clients or users while dynamic web page changes itself according to the time and as per the user’s request.

Servlets 

In Java, a servlet is a way to create those dynamic web pages. Servlets are nothing but the java programs. In Java, a servlet is a type of java class which runs on JVM(java virtual machine) on the server side. Java servlets works on server side.Java servlets are able to handle large and complex problems and requests by users.

Let us move further with dynamic web pages in java

What is a web server?

A web server is used to transfer data in the form of the HTTP protocol. The client just has to type the URL in a browser and the web server provides her/him the required web page to read. So, how it works..? What a web server does inside?

The web server converts the client typed URL into the HTTP protocol in order to respond to the request and with the help of Servlets, it serves the client’s request.

Properties of servlets

  • Servlets work on server side extensions to handle the complex problems.
  • Servlets covers all the limitations of CGI.

Let us move on to the next topic of this Web Pages In Java article,

What is CGI?

CGI (common gateway interface), is an application which is used to produce dynamic contents of web pages. Common gateway interface can be created by using any programming language like c,c++, etc.

While using CGI, when client request anything, the web server performs the following tasks sequentially:- 

  • It receives the request and the required CGI.
  • It generates a new process and calls the required CGI application.
  • CGI generates the output and after getting the information of the request made by the client.
  • It sends the output(response) to the web server and destroys the process.
  • Web server displays it on client’s screen.

In CIG,it has to create and destroy new process for every request,as the number of clients increases, workload also increases and because of which it has lower performance and time to process the requests also increases as CGI can’t communicate directly to the web server.To overcome its limitations, servlets are introduced.  

Servlets are cheaper than CGI and are capable to handle cookies.The java servlet follows a simple process, showing by the below block diagram:-

Steps

  • A client sends the request to a web server.
  • The web server receives the request from client.
  • Servlets receives the request.
  • Servlets process the request and produce the output.
  • Servlet sends the output to the web server.
  • A web server sends it to the client’s browser and browser display it on the client’s screen.

There are two packages by which servlets can build

  •   javax.servlet(Basic) 
  •   javax.servlet.http(Advance)

Advantages of Servlets

  • They are platform independent.
  • They are cheaper than CGI.
  • They are capable to handle cookies.
  • They overcome the limitations of CGI.
  • NO need to create new process for any request.
  • As it is server-side application, it can inherit the security from a web server.

Let us move on to the next topic of this Web Pages In Java article,

What is servlet container

Users did not  have the facility to request and access the static pages but dynamic also, where dynamic web pages can work differently each time for different inputs and according to the time.

     A servlet container is nothing but a concept or idea to use them   

     Java language to develop dynamic web pages (Servlet). 

Servlet container is a part of web server that can easily communicate with the java servlets.

There are three essential methods that can be invoked by the client according to the need:-

  • Init()
  • Service()
  • Destroy()

Web pages in Java Our first servlet program

To develop our first servlet application,we will follow three steps

 Firstly we need to create HTML page which will demand some request from servlet. 

<html>
<head>
<title>First Servlet Program</title>
</head>
<body>
<form action=&rdquo;Servlet&rdquo;>
<input type="submit" value="invoke servlet">
</form>
</body>
</html>

This page will just have a button invoke MyFirstServlet. When you will click this button it will call MyFirstServlet. Now we will create servlet in which we will implement three methods:-

  • Init()
  • Service()
  • Destroy()
Import javax,servlet. *;
Import java.io.*;
Public class OurFirstServlet implements Servlet{
ServletConfig config=null;
Public void init(ServletConfig sc)
{
Config = sc;
System.out.println(&ldquo;in&nbsp; init&rdquo;);
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
res.setContenttype("text/html");
PrintWriter pw = res.getWriter();
pw.println("<h2>hello from servlet</h2>");
System.out.println("in service");
}
//destroy method
public void destroy()
{
System.out.println("in destroy");
}
public String getServletInfo()
{
return "MyFirstServlet";
}
public ServletConfig getServletConfig()
{
return config;
}

In line 1 and 2, we import two packages,second is for PrintWriter.

In line 3, we create a servlet by implementing Servlet interface.

In the first line inside a class, we create a ServletConfig object config which will contain the configuration of Servlet. Initially,it is set to null as no Servlet is there.

Then we created an init method that takes an object of type ServletConfig sc. This is called when a request comes to Servlet. This is used to initialize the config object.

There is a destroy() which is used to mark the ending of the Servlet 

The getServletInfo() is used to return the name of Servlet

The getServletConfig returns the config object on being called.

In last, after a request has come, two objects of type ServletRequest and ServletResponse are created to mark their connection with the client and passed to the service().Here we set the response type of our ServletResponse object to HTML type. Then we obtain the PrintWriter object pw from the response object res by calling getWriter(). Lastly, we write what we have to print in response to the client using println() of pw object.

Thus we have come to an end of this article on ‘Web Pages in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.