🔖 javaapp

pull/2/head
Zhang Peng 2018-07-04 17:23:59 +08:00
parent c1e15aec6d
commit 2d79471cb2
13 changed files with 5094 additions and 0 deletions

View File

@ -0,0 +1,71 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.dunwu</groupId>
<artifactId>JavaWebApp</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>${project.artifactId}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<tomcat.version>8.5.24</tomcat.version>
</properties>
<dependencies>
<!-- javaee begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- javaee end -->
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- spring end -->
<!-- tomcat begin -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<!-- tomcat end -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>

View File

@ -0,0 +1,2 @@
java -Dtomcat.context.path=/app -Dtomcat.connector.port=8091 -cp "./../target/JavaWebApp/WEB-INF/classes;./../target/JavaWebApp/WEB-INF/lib/*" io.github.dunwu.app.Main
pause

View File

@ -0,0 +1 @@
nohup java -Dtomcat.context.path=/app -Dtomcat.connector.port=8091 -cp "./../target/JavaWebApp/WEB-INF/classes;./../target/JavaWebApp/WEB-INF/lib/*" io.github.dunwu.app.Main > 'console.log' 2>&1 &

View File

@ -0,0 +1,118 @@
package io.github.dunwu.app;
import java.io.File;
import org.apache.catalina.Server;
import org.apache.catalina.startup.Catalina;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.digester.Digester;
import org.apache.tomcat.util.scan.Constants;
public class Main {
private static final String CONNECTOR_PORT = "8080";
// 以下设置轻易不要改动
private static final String RELATIVE_DEV_BASE_DIR = "src/main/resources/tomcat/";
private static final String RELATIVE_BASE_DIR = "WEB-INF/classes/tomcat/";
private static final String RELATIVE_DEV_DOCBASE_DIR = "src/main/webapp";
private static final String RELATIVE_DOCBASE_DIR = "";
private static final String CONTEXT_PATH = "/";
public static void main(String[] args) throws Exception {
System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "{}|");
System.setProperty("tomcat.host.appBase", getAbsolutePath());
File checkFile = new File(System.getProperty("tomcat.host.appBase") + "/WEB-INF");
if (!checkFile.exists()) {
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_DEV_BASE_DIR);
System.setProperty("tomcat.context.docBase", RELATIVE_DEV_DOCBASE_DIR);
} else {
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR);
System.setProperty("tomcat.context.docBase", RELATIVE_DOCBASE_DIR);
}
if (isBlank(System.getProperty("tomcat.connector.port"))) {
System.setProperty("tomcat.connector.port", CONNECTOR_PORT);
}
if (isBlank(System.getProperty("tomcat.server.shutdownPort"))) {
System.setProperty("tomcat.server.shutdownPort",
String.valueOf(Integer.valueOf(System.getProperty("tomcat.connector.port")) + 10000));
}
if (isBlank(System.getProperty("tomcat.context.path"))) {
System.setProperty("tomcat.context.path", CONTEXT_PATH);
}
System.out.println("====================ENV setting====================");
System.out.println("spring.profiles.active:" + System.getProperty("spring.profiles.active"));
System.out.println("catalina.base:" + System.getProperty("catalina.base"));
System.out.println("tomcat.host.appBase:" + System.getProperty("tomcat.host.appBase"));
System.out.println("tomcat.context.docBase:" + System.getProperty("tomcat.context.docBase"));
System.out.println("tomcat.context.path:" + System.getProperty("tomcat.context.path"));
System.out.println("tomcat.connector.port:" + System.getProperty("tomcat.connector.port"));
System.out.println("tomcat.server.shutdownPort:" + System.getProperty("tomcat.server.shutdownPort"));
ExtendedTomcat tomcat = new ExtendedTomcat();
tomcat.start();
tomcat.getServer().await();
}
private static String getAbsolutePath() {
String path = null;
String folderPath = Main.class.getProtectionDomain().getCodeSource().getLocation()
.getPath();
if (folderPath.indexOf("WEB-INF") > 0) {
path = folderPath.substring(0, folderPath.indexOf("WEB-INF"));
} else if (folderPath.indexOf("target") > 0) {
path = folderPath.substring(0, folderPath.indexOf("target"));
}
return path;
}
private static boolean isBlank(String str) {
if (str == null || str.isEmpty()) {
return true;
}
return false;
}
static class ExtendedTomcat extends Tomcat {
private static final String RELATIVE_SERVERXML_PATH = "/conf/server.xml";
private class ExtendedCatalina extends Catalina {
@Override
public Digester createStartDigester() {
return super.createStartDigester();
}
}
@Override
public Server getServer() {
if (server != null) {
return server;
}
// 默认不开启JNDI. 开启时, 注意maven必须添加tomcat-dbcp依赖
System.setProperty("catalina.useNaming", "false");
ExtendedCatalina extendedCatalina = new ExtendedCatalina();
//覆盖默认的skip和scan jar包配置
System.setProperty(Constants.SKIP_JARS_PROPERTY, "");
System.setProperty(Constants.SCAN_JARS_PROPERTY, "");
Digester digester = extendedCatalina.createStartDigester();
digester.push(extendedCatalina);
try {
server = ((ExtendedCatalina) digester
.parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer();
// 设置catalina.base和catalna.home
this.initBaseDir();
return server;
} catch (Exception e) {
System.err.println("Error while parsing server.xml" + e.getMessage());
throw new RuntimeException("server未创建,请检查server.xml(路径:" + System.getProperty("catalina.base")
+ RELATIVE_SERVERXML_PATH + ")配置是否正确");
}
}
}
}

View File

@ -0,0 +1,28 @@
package io.github.dunwu.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
* spring mvc
* @author Zhang Peng
* @since 2016.07.29
*/
@Controller
@RequestMapping(value = "/hello")
public class HelloController {
/**
* <p>Spring hello.jsp
* <p>访http://localhost:8080/hello?name=张三
*/
@RequestMapping(value = "/name", method = RequestMethod.GET)
public ModelAndView hello(@RequestParam("name") String name) {
ModelAndView mav = new ModelAndView();
mav.addObject("message", "你好," + name);
mav.setViewName("hello");
return mav;
}
}

View File

@ -0,0 +1,27 @@
/**
* The Apache License 2.0 Copyright (c) 2016 Zhang Peng
*/
package io.github.dunwu.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Zhang Peng
* @date 2017/4/12.
*/
@Controller
public class IndexController {
/**
* <p> ModelAndView index index.jsp
* <p>访http://localhost:8080/
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启动注解服务 -->
<mvc:annotation-driven/>
<context:component-scan base-package="io.github.dunwu.app"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<Server port="${tomcat.server.shutdownPort}" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<Service name="Catalina">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="300" minSpareThreads="25"/>
<Connector port="${tomcat.connector.port}" protocol="HTTP/1.1" connectionTimeout="30000" acceptCount="500"
executor="tomcatThreadPool"
processorCache="300" bindOnInit="false"/>
<Engine name="localhost" defaultHost="localhost">
<Host name="localhost" createDirs="false" appBase="${tomcat.host.appBase}" unpackWARs="false"
autoDeploy="false"
deployOnStartup="false" failCtxIfServletStartFails="true">
<Context path="${tomcat.context.path}" docBase="${tomcat.context.docBase}" reloadable="false"
logEffectiveWebXml="false"
privileged="false"
swallowOutput="false" workDir="${catalina.base}/work">
<JarScanner>
<JarScanFilter pluggabilitySkip="*.jar" pluggabilityScan=""
defaultPluggabilityScan="true" tldSkip="*.jar"
tldScan="" defaultTldScan="true"/>
</JarScanner>
</Context>
</Host>
</Engine>
</Service>
</Server>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: io.github.dunwu.app.Main

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>spring-embed-tomcat-demo-helloworld</display-name>
<!-- SERVLET BEGIN -->
<servlet>
<servlet-name>spring-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- SERVLET END -->
<!-- FILTER BEGIN -->
<!-- 转换字符编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- FILTER END -->
<welcome-file-list>
<welcome-file>/views/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

View File

@ -0,0 +1,17 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath =
request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HelloController</title>
</head>
<body>
<h1>${message}</h1>
<a href="<%=basePath%>">回到首页</a><br>
</body>
</html>

View File

@ -0,0 +1,23 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
String domain = request.getScheme() + "://" + request.getServerName() + request.getContextPath();
String host = request.getRemoteHost();
int port = request.getServerPort();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>spring-embed-tomcat-demo</title>
</head>
<body>
<h1>spring-embed-tomcat-demo</h1>
<h2><%out.print("当前服务器信息:");%></h2>
<ul>
<li><%out.print("domain" + domain);%></li>
<li><%out.print("host" + host);%></li>
<li><%out.print("port" + port);%></li>
</ul>
</body>
</html>