Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Name: AMAN ANAND

Roll- 2101330100033
-------------------------------------------------------------------------------------------------

11) Create a project showing the implementation of Spring MVC using model
and view class for finding the area of rectangle.

Rectangle.java(Model class):
package com.springmvc.model;

public class Rectangle {


public int length;
public int width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int area() {
return this.length*this.width;
}
}

RectangleController.java(Controller class)
import ch.qos.logback.core.model.Model;

@Controller
@RequestMapping("/rectangle")
public class RectangleController {
// Show the form to input rectangle dimensions
@GetMapping("/form")
public String showForm() {
return "rectangleForm";
}
// Handle the form submission and calculate the area
@PostMapping("/calculate")
public String calculateArea(@RequestParam int length, @RequestParam int width, Model
model) {
Rectangle rectangle = new Rectangle();
rectangle.setLength(length);
rectangle.setWidth(width);

int area = rectangle.area();

model.addAttribute("rectangle", rectangle);
model.addAttribute("area", area);

return "result";
}
}

View Class (JSP file) (User input):


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rectangle Area Calculator</title>
</head>
<body>

<h2>Rectangle Area Calculator</h2>

<form action=”rectangleController.java” method=”post”>


<label for="length">Length:</label>
<input type="text" id="length" name="length" required><br>

<label for="width">Width:</label>
<input type="text" id="width" name="width" required><br>

<input type="submit" value="Calculate Area">


</form>

</body>
</html>

View Class(JSP file)(result)


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rectangle Area Result</title>
</head>
<body>

<h2>Result</h2>

<p>Rectangle with length ${rectangle.length} and width ${rectangle.width} has an area of


${area} square units.</p>

</body>
</html>

OUTPUT:
12) Create a rest service based web application using Spring boot and fetch data from
internal or external databases with JPA and send API request to POSTMAN client or
browser

Application.java
package com.springboot.jpa;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.springboot.jpa.dao.UserRepository;
import com.springboot.jpa.dao.User;

@SpringBootApplication
public class SpringbootjpaexampleApplication {

public static void main(String[] args) {


ApplicationContext context =
SpringApplication.run(SpringbootjpaexampleApplication.class, args);
UserRepository userRepository =
context.getBean(UserRepository.class);
Iterable<User> itr= userRepository.findAll();
Iterator<User> iterator= itr.iterator();

while(iterator.hasNext()) {
User user= iterator.next();
System.out.println("Name: "+user.getName() + " City:
"+user.getCity()+" Status: "+user.getStatus());

}
User.java
package com.springboot.jpa.dao;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User(int id, String name, String city, String status) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
UserRepository.java

package com.springboot.jpa.dao;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {

Application.properties

spring.datasource.name=springbootjdbc

spring.datasource.url=jdbc:mysql://localhost:3306/springbootjdbc

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

server.port=8008

Output:
13) WAP to create simple web application in Spring Boot to build a
registration form with the help of JSP and show the data on view page.

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring.ques</groupId>
<artifactId>springquestion4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springquestion4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

User.java

package com.spring.ques.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;


private String email;
public User(Long id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

// Getters and setters


}

UserRepository.java

package com.spring.ques.services;

import org.apache.catalina.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

UserController.java

package com.spring.ques.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.spring.ques.entity.User;
import com.spring.ques.services.UserService;

import java.util.List;

@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String register() {
return "register";
}

@PostMapping("/register")
public String register(@RequestParam("name") String name,
@RequestParam("email") String email,
Model model) {
User user = new User();
user.setName(name);
user.setEmail(email);
userService.save(user);
List<User> users = userService.findAll();
model.addAttribute("users", users);
return "view";
}
}

UserService.java

package com.spring.ques.services;

import java.util.List;

import org.apache.catalina.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

public class UserService {

@Autowired

private UserRepository userRepository;

public User save(User user) {

return userRepository.save(user);

}
public List<User> findAll() {

return userRepository.findAll();

public User findById(Long id) {

return userRepository.findById(id).orElse(null);

public void deleteById(Long id) {

userRepository.deleteById(id);

Register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<form action="/register" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>

Output:
14) WAP to perform insert delete and update query into a database with the help of
creating entity class and using its controller repository and services
SpringbootjpaApplication.java:
package com.springboot.jpa;
import java.util.List;
import org.apache.el.stream.Optional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.springboot.jpa.dao.UserRepository;
import com.springboot.jpa.dao.User;

@SpringBootApplication
public class SpringbootjpaApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(SpringbootjpaApplication.class, args);
UserRepository userRepository =
context.getBean(UserRepository.class);

// insert
User user1=new User();
user1.setName("qwerty");
user1.setCity("Up");
user1.setStatus("kcgsdg");
User resultUser=userRepository.save(user1);
System.out.println(resultUser);

User user2=new User();


user2.setName("asdfg");
user2.setCity("Abdsdsc");
user2.setStatus("Web Developer");
User resultUser2=userRepository.save(user2);
System.out.println(resultUser2);

//Update
java.util.Optional<User> optional= userRepository.findById(202);
User user =optional.get();
user.setName("whjdwedw");
User result22 =userRepository.save(user);

//delete
userRepository.deleteById(202);
System.out.print("Deleted");
Iterable<User> result = userRepository.findAll();
result.forEach(user ->{
System.out.println(user);
userRepository.deleteAll(result);
});
}
}
User.java:
package com.springboot.jpa.dao;
mport org.springframework.data.repository.CrudRepository;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User(int id, String name, String city, String statUserReposius) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

}
UserRepository:

package com.springboot.jpa.dao;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer>{

}
Application.properties:

spring.datasource.name=test

spring.datasource.url=jdbc:mysql://localhost:3306/springbootjdbc

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

OUTPUT:
Insertion:

Update:

Deletion:

You might also like