In this blog we are going to learn about servlets and jsp and what is the requirement of them.
Need of Servlet and JSP
We are aware that in today’s world we need a dynamic website that displays content based on the request received from the client-side. So if you are using Java as your back-end language you can implement this using servlets and JSP. Servlet gets a request from the server, processes it, produces a response, and sends it back to the server.
Servlet is a java program that receives a request from the web client and processes it and sends a response back across a web protocol.
You can create a servlet by extending GenericServlet or HttpServlet. This interface characterizes techniques to introduce a servlet, to support demands, and to eliminate a servlet from the worker.
Life Cycle Of Servlet
- A servlet is created, then initialized.
- Calls from the client are handled and sent back a response.
- Servlet is taken out of service, then destroyed and garbage collected.
Methods Of Servlet
The init method is called only once if the instance of the servlet required does not exist already. It is done through a web container by loading the servlet class, creating an instance, and initializing it.
The init method must be completed successfully before the servlet starts receiving any requests.
It is called after the servlet init method has been completed successfully. It handles the HTTP request types like do, post, put, delete, and then calls the appropriate methods.
It is called by the container to take the servlet out of service. It is only called after all the threads executing the service method have exited or a timeout period has occurred. After this container will not call the service method again.
Example Of Servlet
Here we are creating a Servlet named as ContactUsFormServlet which is intercepting an HTTP request which in this case is a post request.
So the data of the contact us form is stored in the request object. So first we create an object of the ContactUsDao which is a Data Object Class which do the storing of the data in our database. Then after completing the required task we send a response back to the client. Here we are redirecting to the index.JSP.
Now for the web-mapping is important so we can target our servlet at a specific URL.
We can achieve this either by a @WebServlet annotation or by adding the mapping in the web.xml file.
JSP stands for the Java Server Pages. It is a server-side technology and used for the creation of web appa]s and dynamic content pages.
JSP is different from the servlet as here we embed the Java code using scripting tags like
- <% — comment — %> To add comments
- <%@ directive %> To import classes and include pages
- <%! declarations %> declare variables in class outside service method
- <% scriplet %> To add Java code
- <%= expression %>To print Java expressions
LifeCycle Of JSP
- Converting of JSP page into the servlet .
- Loading the servlet class and creating a servlet instance.
- Initializing the servlet, processing the request and destroying the servlet.
Example Of JSP
Here we are creating a JSP page named login.JSP which basically is a login page for the user and will send the request to a Servlet named as AuthenticatorServlet which checks the login credentials and sends a response basis on which the page is redirected to show the data page or refreshed to the current page.
Since it is a basic page so here we didn’t have a scriptlet but based on your need you can add it. Here we have added the contentType and language for the page using <%@ page %> and comment using the <% — — %> tag.
Then we have created a login form of method post and specified the URL in action.