Servlet –
Java Servlets are helpful for developing Web applications. Servlets are server-side Java applications that can be loaded dynamically.
You can have data from end users through web pages that present records from a database or another source, and create web pages dynamically.
The technology that was used prior to servlet was CGI. So lets first understand the history of servlet using CGI and the disadvantages of it with the help of Java Classes In Pune.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
History of CGI –
A Web Server generally has to handle a large amount of requests concurrently and thus is not able to handle all transactions that keep coming in. To achieve this some applications are used to extend the Web Servers. The oldest among this category are CGI(Common Gateway Interface). This first technology revolutionized the way requests were handled by Web servers since dynamic contents could be sent now instead of a static file.
CGI has following drawbacks –
CGI scripts cannot interact with Web Servers or cannot take advantage of its capabilities. It is running as a separate process.
For each request that a server receives for a CGI program it starts a separate process.
Then the server passes all the data that the CGI program might need to generate the response with the help of environmental variables and standard input.
This limits the number of requests a server can handle.
Introduction to Servlets –
A Servlet is very similar to CGI or server extension in functionality with the difference being that it runs inside JVM on the Server and therefore is safe and portable. Servlets, unlike applets, do not require a browser to run, they run within the domain of the server. The major drawback in CGI technique is that they run multiple processes to handle different requests. A Servlet on the other hand handles each request using Java’s multithreading capability. A separate thread is created to handle each request . Thats how servlets become more efficient and scalable. One more advantage of Servlets is that they are portable across operating systems and web browsers. Most of the web browsers support Servlets. Though Servlets are basically used in place of CGI scripts, they can be used to extend any type of server like FTP server, Mail server etc. Learn more about it through Java Course In Pune,
A Servlet works via the request , response cycle. A request is sent to the Servlet by the client, the Servlet processes it., generates a response and sends the response back to the client. In a protocol independent Servlet , the request is handled by its service() method. The service method accepts two parameters Http Servlet Request and Http Servlet Response type of objects. The Http Servlet Request objects specify the client’s request and Http Servlet Response objects specify the Servlet’s response to that client’s request. An Http Servlet on the other hand does not usually override this service() method. This is because it has a specific method called doGet() and doPost() to handle specific requests.
The service() method thus dispatches requests to these methods. Both doPost() and doGet() methods accept two arguments, the Http Servlet Request and Http Servlet Response type of objects, which again specifies request and response respectively.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
The doPost() method supports the POST method of HTTP protocol and doGet() method supports the GET method of HTTP protocol. The HTTPservlets service() method by default also calls the doOptions() method when it receives the OPTIONS request, and doTrace() method when it receives the TRACE request. The default implementation of doOptions() automatically determines what HTTP options are supported and returns that information. The default implementation of the method doTrace() causes a response with a message containing all of the headers sent in the trace request. This method are not typically overridden. The doHead() method is used to access header information of a request. The doPut() method is used to put some information from the client to the server and the doDelete() method is used to delete some information from the server.
When a call to the servlet is made its init() method is invoked and when the servlet is being destroyed just before the garbage collection happens, its destroy() method is called. Understand all parameters within Java Training In Pune.
This is the basic example of servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html”);
String host=request.getRemoteHost();
String addr=request.getRemoteAddr();
PrintWriter pw=response.getWriter();
pw.println(“<h1>”Welcome to MyServlet”</h1>”);
pw.println(“<h2>”Your Host is “+host”+” Your address is “+addr+”</h2>”);
}
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Author:-
Jyostna Binjwe
Call the Trainer and Book your free demo Class Call now!!!
| SevenMentor Pvt Ltd.
© Copyright 2021 | Sevenmentor Pvt Ltd.