Skip to content

spring vs servlets

Yash edited this page Oct 22, 2018 · 2 revisions

json - converstion servlet has to create json object and send it with header.

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

PrintWriter out = response.getWriter();

Employee employee = new Employee(1, "Karan", "IT", 5000);
String employeeJsonString = this.gson.toJson(employee);
out.print(employeeJsonString);
out.flush();

// Spring uses Servlet concept @RequestMapping(value = "/path", method = RequestMethod.POST )
// SB : {"no":"1","name":"yash","domain":"github"}
JSONObject jObj = new JSONObject(sb.toString());
out.print(jObj);

spring responsed back direclty json from pojo class

@GetMapping(value="/person/{id}/", 
	    params="format=json",
	    produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> getPerson(@PathVariable Integer id){
	Person person = personMapRepository.findPerson(id);
	return ResponseEntity.ok(person);
}
security module in frame work is inbuilt
servlets we have to implement

DI - object creation

DB - JDBC Template Connection pool inbuilt (Boiler plate code)
Servlet - use any coonection form outside to get connection from DB

stand alone and webbased application, quick development

How many types of IOC containers are there in spring?

  • BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients.
  • ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface. It provides some extra functionality on top BeanFactory.
BeanFactory ApplicationContext
org.springframework.beans.factory.BeanFactory org.springframework.context.ApplicationContext
It uses Lazy initialization It uses Eager / Aggressive initialization
It explicitly provides a resource object using the syntax It creates and manages resource objects on its own
It doesn’t supports internationalization (i18n) It supports internationalization (i18n)
It doesn’t supports annotation based dependency It supports annotation based dependency

Clone this wiki locally