Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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
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:-
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
There are two packages by which servlets can build
Advantages of Servlets
Let us move on to the next topic of this Web Pages In Java article,
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:-
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=”Servlet”> <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:-
Import javax,servlet. *; Import java.io.*; Public class OurFirstServlet implements Servlet{ ServletConfig config=null; Public void init(ServletConfig sc) { Config = sc; System.out.println(“in init”); } 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 Certification Course 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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co