Lesson 3 (JAX-RS and Servlets) Flashcards
What is a servlet?
A servlet is some kind of progam running in another program (broker). The broker sends the requests to the server. The broker is responsible to send data back to the client. The servlet program can handle custom steps from a request.
What is a servlet engine? Give an example.
For example Tomcat. Tomcat is a Servlet Engine that handles servlet requests for apache. Its like a helper application, its like a servlet container.
Which servlet-method is called in response to a http GET-command?
doGet method is called automatically.
Which annotation is used in JAVA-RS to specify the relative path of a resource?
@Path
The doGet method of the httpServlet class contains two parameters. The first parameter is of the class HttpServletRequest. Describe the second parameter.
The HttpServletResponse. This is used for preparing the response in the code. It also provides a writer to write the output of the response object as a response to the original request.
What are some JAX-RS annotations and what are they used for?
@Path url pattern
@GET as http method
@Procudes as mime type for the response data.
@Consumes content type for the request data
@FormParam / @HeaderParam/ @CookieParam to bind those values to the request or response. Those kind of things.
What are some JAX-RS implementations in existing solutions?
CFX (Apache)
Jersey (from Sun/Oracle)
RESTeasy from Jboss
What do you need to create a JAX-RS project?
A maven project with the jersey as a library included in the POM (kind of manifest) file.
What are some webservers and how do they work?
Apache or Nginx, they are very popular and they listen on various ports using the HTTP protocol to serve requests and responses.
How does a servlet work?
1 Client sends a requets to the server
2 Server starts the right servlet
3 Servlets computes a result for the server and does not quit
4 server returns the response to the client
5 Another client sends a request
6 Server calls the same servlet .
A servlet is instantiated like a singleton.
How is a servlet instantiated?
The init(config) method is called first and only once. Servlets order can be defined with params, like when you want to start a server as first, or always want to start it BEFORE anything else gets called. The servlet must be thread safe. Finally when its shutdown it calles destroy()
What are the methods called int the servlets after executing a request?
doGet, doPost. doPut, doDelete.
How can a servlet retrieve data from the POST?
by calling request.getParam if it was a form/url parameter. If there is just a JSON object, the request reader has to be used to read the input stream. Then be parsed to a JSON object.
How does the url encoding work?
Special characters and spaces are replaces, for example a space is %20.
How are request paramters retrieved by different methods? How can you find out which ones are available?
getParameterNames() > all that is available by key
getParameter(name) > value for one specific parameter
getParameterValues(name) > used for array values.