Sunday, November 2, 2014

Database


JPA setup - Controller Module -> persistence.xml contains all JPA entities JTA DS, class, properties, property Hibernate has a JPA implementaion + JPA is just a specification + it contains annotations

Hibernate - ORM framework for transaction management

Hibernate Life cycle - Transient Objects -> Persistence Object -> Detached Object

Hibernate Setup - hibernate.cfg.xml + mapping files *.hbm.xml

Hibernate Advantages - We can enable 2nd level cache + indexing + Denormalization + HQL

Hibernate Advantages Session Factory - thread safe immutable singleton objects supply

Hibernate Advantages session closing and exception handling are automated by hibernate template

Connection Pooling Connecting to database through pre-created set of resuable software objects. (It is a pattern)

JNDI Use In distributed enviroinments we can use JNDI to locate the resource by abstract resource-independent way.

Ways of creating a datasource 1. Manually call the JDBC driver manager class and create the DS
2. Connection Pooling
3. Distributed transaction

Transaction management using JDBC autocommit(), commit(), rollback()

Isolation Levels in JDBC TRANSACTION_SERIALIZABLE, TRANSACTION_READ_COMMITED, TRANSACTION_READ_UNCOMMITED, TRANSACTION_REPEATABLE_READ

Statement VS Prepared Statement Statement is used in static DB connection - Prepared Statement is used when multiple SQL statements run at the same time Statement does not accept parameters - Prepared Statement accepts parameters at runtime

Outer Join VS Inner Join Outer Join gives union of two sets - Inner join gives intersection of two sets

Triggers executes a set of operations when a particular operation is performed in DB

Saturday, November 1, 2014

Web Services

  • REST API: This refers to any API that adheres to the principles of REST (Representational State Transfer). REST is an architectural style with constraints like statelessness, uniform interface, and resource-based interactions. Technically, an API can claim to be a REST API even if it doesn’t fully comply with all REST principles.

  • RESTful API: This term is used to describe APIs that strictly follow REST principles and are more closely aligned with RESTful design. Essentially, a RESTful API is a "better-behaved" REST API. It emphasizes proper implementation of REST constraints, including statelessness, resource representation via URLs, and the use of standard HTTP methods.

To simplify, all RESTful APIs are REST APIs, but not all REST APIs are RESTful.

REST - Point to point HTTP communication, not reliable, URL directly references the resource being accessed and message can be of any formats - only HTTP

JAX-RS - uses annotations like @Path, @GET, @POST, @PUT, @Consumes, @Produces and @Provider

SOAP - distributed communication, reliable, WSDL-XSD - contract based where WSDL will be registered in UDDI and standard XML message communication between WS client and server. - HTTP and SMTP

JAXB - API to marshal JAVA objects to XML and unmarshal it + Java web services development pack

Testing WS - Add the WS component in web.xml -> WS will be added to WS servlet. Now, we can test the WS using localhost.
WASAxis2Servlet is the servlet class name for WS.

WSDL Basic Parts - Definition, Type, Interface, Service, Endpoint, Message

WSDL minor parts - wsdl:definitions, types, message, part, portType, operation, input, output, binding, body, service, port

XSD parts - xsd:types, schema, element, complexType, sequence, element

UDDI - Universal Definition Discovery and Integration

. Set Up Your Project

  • Create a Spring Boot project using .

  • Include the dependency spring-boot-starter-web to enable web and REST API functionality.

2. Create a REST Controller

Use the @RestController annotation to define a RESTful controller. Here's an example:

java
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        // Logic to fetch a user by ID
        return userService.getUserById(id);
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        // Logic to create a new user
        return userService.saveUser(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        // Logic to update a user
        return userService.updateUser(id, user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        // Logic to delete a user
        userService.deleteUser(id);
    }
}

3. Key Annotations

  • @RestController: Combines @Controller and @ResponseBody to return JSON/XML responses.

  • @RequestMapping: Maps a base URL for the controller.

  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping: Handle specific HTTP methods.

  • @RequestBody: Binds the body of a request to an object.

  • @PathVariable: Binds a variable in the URL to a method parameter.

4. Handle Data

Spring Boot uses Jackson to handle JSON serialization and deserialization, ensuring that objects are converted seamlessly.

5. Database Integration (Optional)

  • Add spring-boot-starter-data-jpa for working with databases.

  • Configure the application.properties file for database connection details.

  • Use Spring Data JPA for CRUD operations.

6. Run and Test

  • Run your application using mvn spring-boot:run or directly from your IDE.

  • Test your APIs with tools like Postman or cURL.

7. Advanced Features

  • Validation: Use @Valid and validation annotations like @NotNull or @Size.

  • Exception Handling: Use @ControllerAdvice and @ExceptionHandler to provide meaningful error responses.

  • Security: Add spring-boot-starter-security for authentication and authorization

STRUTS


MVC in struts Model – handlers – encapsulates business logic View – Response from handler – JSP, DB content and other presentation components. Controller – delegates request to appropriate handlers

Struts Components Servlets + Custom tags + JSP + message resources

Components of a web application


EAR - enterprise archive - it consists of application.xml (META-INF folder) which is the deployment descriptor, it is used to pack and deploy WAR and EJB in to web application servers

WAR - it consists of servlet (front controller) + JSPs + Business Delegate Classes + Web.xml + Anxillary jars (WEB-INF/lib folder)

EJB - it consists of web service clients + MDBs + Data base logic + session beans + JPA + DAOs

ANT + Maven - can be used to build ear files

Monday, August 11, 2014

J2EE



Advantages of using entity beans Types – Container and bean managed entity beans. It represents data from the database.

EJB invocation Client < EJB object <- RetrieveHome object -> Naming service -> JNDI

HTTP session sharing between JSP and EJB We can pass the fat session values between JSP and EJB, but not efficient. It is better to form value object as an abstraction layer where data can be modified and passes back and forth.

Primary key cannot be primitive datatype

Message driven bean VS Stateless Session bean MDB’s process multiple JMS messages simultaneously – stateless session bean process messages serially one by one. MDB’s are maintained by containers through whole lifecycle – Client requests or other API calls may invoke a stateless session bean

JNDI can be used to call one EJB inside other EJB

Only inner class can be declared as private

Deployment Descriptor in J2EE .ear File must in called in web.xml in WEB-INF directory and application.xml in MET-INF directory.

ANT VS Maven ANT is procedural where we need to define each and every step - Maven has a lifecycle it automatically created Jar when we configure POM.XML with source code