What is a Servlet and what is it for?

Asked

Viewed 31,029 times

10

I’ve heard a lot about the term servlet, however, I still can’t understand. What really is a servlet? What is the point? What is its practical applicability?

  • Have you ever used any other web language besides java?

  • I started studying java for the web a little while ago, I’m still adapting to this new way of programming for the web using java. I have more experience with java for desktop, now for the web platform is a new environment that I’m getting to know

  • 4

    Web systems work very different from desktop systems. Basically what happens is an exchange of messages between the client (a browser for example) and the server, this conversation occurs through the http protocol, and yes this is done with plain text! so the language is java, c#, php reads this information (header) and populates some objects/offers functions for manipulation. request is a request from the customer, it can be the sending of a login or the click on link and response is given name the server response. Had to be brief the xD explanation.

  • 1

    Got it, thank you so much for the @rray explanation! If it’s not too much to ask, you could give me a hint where I should start studying about this kind of platform?

  • Regardless of the technology know how the http protocol works, life cycle, the main methods are get and post there are others, see the cabbages, how to maintain status between the client and server. I forgot to comment http is a stateless protocol. Knowing this you can work with any language because the concepts are the same.

  • 1
Show 1 more comment

3 answers

10


Servlets are Java classes, developed according to a well-defined structure that when installed and configured on a Server that implements a Servlet Container, can handle requests received from Web clients, such as Browsers (Internet Explorer® and Mozilla Firefox®).

Upon receiving a request, a Servlet can capture the parameters of this request, perform any processing inherent in a Java class, and return an HTML page. - Taken from Devmedia.

They are basically software modules that run on a web server to meet client application requests and provide them some kind of service.

That is, when you receive your request in the view, you need to receive that request, process it in some way and send a reply. Servlet receives your request, processes or sends it to someone to process and then returns the answer to where you need it.

  • I had a question, what would be a container @guiandmag?

  • 2

    It is an abstraction, a component that encompasses several components to give functionality, https://pt.wikipedia.org/wiki/Container_(type_de_data_abstract), as an example in the Java world, we have Tomcat, a container(component) that encompasses other components (Servlet) to give real functionality to everything.

  • Got it, thanks a lot @guiandmag

7

Servlet is a java class for working with web development although it is not specially developed for this.

In this class, the manipulations of the requisitions are made, two important members are the request(input normally) and the response(exit).

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Teste extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Teste() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

1

Servlet is a technology that the Java platform provides to web developers as a simple and consistent mechanism to extend the functionality of a web server and to access existing business systems. Java Servlets is what makes many web applications possible, they are responsible for generating the pages dynamically as per user request. In short, the goal is to receive HTTP calls, process them and return a response to the client.

You can read a great explanation here.

Sources:

http://www.oracle.com/technetwork/java/index-jsp-135475.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.