This section uses a simple Javaweb project to realize the use of Gradle
Demand
Build a Javaweb project and build a Jsp+servlet development environment
You can break down your requirements into two steps:
- Building a Java project using Gradle
- Build the Web view layer for the project
Tools
Experimental process
First, create an empty Gradle project in idea.
Create the following directory-file structure
Write the following in the build script
group ‘com.shy‘version ‘1.0-SNAPSHOT‘apply plugin:‘java‘apply plugin:‘war‘apply from: ‘https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin‘jar{ manifest{ attributes ‘Main-Class‘:‘com.shy.todo.ToDoApp‘ }}repositories{ mavenCentral();//对maven central 2仓库访问的快捷方式}dependencies{ providedCompile ‘javax.servlet:servlet-api:2.5‘ runtime ‘javax.servlet:jstl:1.2‘}task wrapper(type:Wrapper ){ gradleVersion = ‘4.3‘}
- The Web. XML is configured as
Use a todoservlet to accept all requests
<?xmlVersion= "1.0" encoding= "UTF-8"?><web-appxmlns="Http://java.sun.com/xml/ns/javaee"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version="2.5"> <servlet> <servlet-name>Todoservlet</servlet-name> <servlet-class>Com.shy.todo.web.ToDoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Todoservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
- Todoservlet responsible for forwarding requests
Public classTodoservletextendshttpservlet{PrivateTodorepository todorepository =New inmemorytodorepository();@Override protected void Service(HttpServletRequest req, HttpServletResponse resp)throwsServletexception, IOException {String Servletpath = req.Getservletpath(); String view =ProcessRequest(Servletpath,req); RequestDispatcher Dispatcher = req.Getrequestdispatcher(view); Dispatcher.forward(REQ,RESP); System. out.println("Hello"); }PrivateStringProcessRequest(String servletpath,httpservletrequest request) {if(Servletpath.equals("/all")){return "/jsp/todo-list.jsp"; }Else{return "not-found.jsp"; } }}
The above is the project source
Gradle Learning Journey (iv) building a simple Java Web project using Gradle