Gradle Web App
Gradle Web App
1|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
2. CONFIGURING GRADLE:-
You need to add the configuration for your application to become "WEB
Application". And can be run directly on Eclipse + Tomcat Plugin.
repositories {
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile 'com.google.guava:guava:23.0'
testCompile 'junit:junit:4.12'
providedCompile "javax.servlet:javax.servlet-api:3.1.0"
dependencies {
def tomcatVersion = '8.0.53'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-
juli:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}
buildscript {
repositories {
jcenter()
}
2|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
}
}
Note that each time there is a change in build.gradle you need to update the
project, using the tool of Gradle.
src/main/resources: This folder can hold property files and other resources
src/main/webapp: This folder holds jsp and other web application content.
3|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
Servlet class:
package com.app;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/home")
public class Message extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();
4|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
out.println("Hello App");
}
}
index.jsp file
5|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
http://localhost:8080/MyWebApp/home
6|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
7|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
Folder System:-
8|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
}
dependencies {
9|P ag e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.springframework:spring-webmvc:5.1.8.RELEASE'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.5'
runtime 'javax.servlet:jstl:1.2'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-
juli:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
}
}
AppInit.java
package com.app.init;
import
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcher
ServletInitializer;
import com.app.config.AppConfig;
@Override
protected Class<?>[] getRootConfigClasses() {
10 | P a g e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/*"};
}
AppConfig.java
package com.app.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan("com.app")
@EnableWebMvc
public class AppConfig {
RestController
package com.app.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
11 | P a g e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
@RestController
@RequestMapping("/emp")
public class EmpController {
@GetMapping("/msg")
public String show() {
return "Hello R-APP";
}
}
12 | P a g e
- by RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
FB:
https://www.facebook.com/groups/thejavatemple/
13 | P a g e