Gradle Learning Journey (iv) building a simple Java Web project using Gradle

Source: Internet
Author: User
Tags java web

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
    • gradle4.3
    • Idea
    • JDK 1.8
Experimental process
    1. First, create an empty Gradle project in idea.

    2. Create the following directory-file structure

    3. 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‘}
    1. 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>
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.