一、在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 4.如下填写完成后,点击完成即可 5.创建完成后的maven项目结构如下 其中index.jsp报错,错误信息:Multiple annotations found at this line: – The superclass "javax.servlet.http.HttpServlet" was not found on the Java 意思是缺少servlet包,我们可以导入javax.servlet-api-3.1.0.jar包,我们可以用两种方式来处理: 1> 在pom.xml中的dependencies中加入依赖包
1 2 3 4 5 |
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> |
2> 可以在build path中添加tomcat 库,如下 点击next出现下面界面,如下操作 至此,一个正常的maven web项目已经建好,如下: 二、配置springMVC 1.在pom.xml中添加对spring的依赖 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>HelloSpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HelloSpringMVC Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.1.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <build> <finalName>HelloSpringMVC</finalName> </build> </project> |
保存后会下载对应的jar文件 2.编辑web.xml文件 web.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<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>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
3.创建springContext.xml文件,在src/main/resources包中创建springContext.xml文件,如图: springContxt.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 搜索spring控件 --> <context:component-scan base-package="com.test"></context:component-scan> <!-- 视图页面配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
在springContext.xml中,base-package是指定spring控制器控件的包,前缀指定的是视图目录,被设置为/WEB-INF/views,即视图目录被放到WEB-INF下。后缀指定的是视图的扩展名。例如,"hellospring"视图,将被放到/WEB-INF/views/hellospring.jsp。 4. 创建Spring控制器和视图 创建HelloSpringController.java类,在src/main/java包中,如下图: HelloSpringController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloSpringController { String message = "Welcome to Spring MVC!"; @RequestMapping("/hello") public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "Spring") String name) { ModelAndView mv = new ModelAndView("hellospring");//指定视图 //向视图中添加所要展示或使用的内容,将在页面中使用 mv.addObject("message", message); mv.addObject("name", name); return mv; } } |
在上面的代码中,@Controller注解为Spring标注前置控制器的方式,@RequestMapping注解映射web请求到具体要操作的类或者方法上面,@RequestMapping注解既可以用到类上,也可以用到方法上,在此不再详述,如有疑问,可以百度。@RequestParam注解为请求指定参数。这种方式提供了一个一致 的编程风格。 另外上述代码中ModelAndView类指定具体的视图,这里是"hellospring",由于我们在springContext.xml配置了视图的前后缀,所以在这里只需要写出视图的具体名称即可,其具体指定的就是:前缀+视图名称+后缀,即完整的视图路径/WEB-INF/views/hellospring.jsp,也就是所要展示的视图的位置。 项目首页index.jsp内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring 4 MVC - HelloWorld Index Page</title> </head> <body> <center> <h2>Hello World</h2> <h3> <a href="hello?name=zhangsan">点击跳转</a> </h3> </center> </body> </html> |
上述代码中,点击跳转 的链接其实就是我们HelloSpringController.java中定义的控制器的一个@RequestMapping注解方法,name=zhangsan为showMessage接受的参数。 在/WEB-INF/views/下创建hellospring.jsp视图,如图: hellospring.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring 4 MVC -HelloWorld</title> </head> <body> <center> <h2>Hello World</h2> <h2> ${message} ${name} </h2> </center> </body> </html> |
上述代码中显示我们在HelloSpringController.java的showMessage方法中添加的两个参数message和name,如图: 好,至此我们使用maven搭建springMVC的操作已经全部完成,完成后整个项目的结构如下图: 三、将项目部署到tomcat服务器运行 首页 http://localhost:8080/HelloSpringMVC/ ,其中也可以使用 http://localhost:8080/HelloSpringMVC/index.jsp 两个效果是一样的 点击跳转 页 注意:若是跳转后页面直接显示${message} ${name}说明jstl表达式不起作用,我们可以在pom.xml中添加如下依赖:
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> |
from:http://www.cnblogs.com/qixing/p/qixing.html
View Detailssettings.xml存在于两个地方: 1.安装的地方:$M2_HOME/conf/settings.xml 2.用户的目录:${user.home}/.m2/settings.xml 前者又被叫做全局配置,后者被称为用户配置。如果两者都存在,它们的内容将被合并,并且用户范围的settings.xml优先。 如果你偶尔需要创建用户范围的settings,你可以简单的copy Maven安装路径下的settings到目录${user.home}/.m2。Maven默认的settings.xml是一个包含了注释和例子的模板,你可以快速的修改它来达到你的要求。 发现很多第三方的项目默认的setting配置都是用户目录/.m2/settings.xml 所以为了方便,需要自己创建.m2文件夹,并在其中配置settings.xml 网上的教程就是使用命令
1 |
mvn help:system |
使用之后,发现并没有生成.m2文件夹 查找很多之后发现,必须把默认的maven里面的本地存储设置为默认的,就是不要设置
1 |
<localRepository>D:\maven\repository</localRepository> |
这一行注释或取消掉,再执行mvn help:system命令就OK了。 http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html
View Details一、使用yum命令搜索支持jdk版本
1 |
yum search java|grep jdk |
二、使用yum安装jdk8
1 |
yum install -y java-1.8.0-openjdk |
三、检查是否成功
1 |
java -version |
首先是下载,当然是官方网址 http://tomcat.apache.org/ 进入官网后了我们看到从tomcat6-tomcat9可供我们选择,因为tomcat9还是alpha版本,我们就选择最新的稳定版8.5.14.当然你也可以选择最常用的tomcat7.0.7. 具体的大家可以看这篇博客里面有各个版本的介绍tomcat版本介绍 选择完成之后我们就可以下载了。这里我给出两个版本的下载地址,如果大家需要其他版本的Tomcat可以自行去官网上面下载。
1 2 |
wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.14/bin/apache-tomcat-8.5.14.tar.gz wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.77/bin/apache-tomcat-7.0.77.tar.gz |
下载完成后我们解压
1 |
tar -xvf apache-tomcat-8.5.14.tar.gz |
解压完毕之后我们进入tomcat目录下的bin文件,当然你也可以使用mv命令重命名文件夹,如果不太了解mv命令可以查看我的linux常用命令。 执行./catalina.sh 或者./startup.sh启动tomcat。 如果出现上图,并且在浏览器的8080端口中可以看到tomcat的猫就说明tomcat启动成功。 如果启动tomcat时,出现以以下错误说明jdk的环境变量没有配置。参考我的jdk安装指南配置。 如果tomcat 启动成功但是外网机访问不到请检查linux的防火墙是不是没有关闭,或者外网机是不是设置了代理。
1 |
--停止防火墙 |
1 2 |
systemctl stop firewalld service service iptables stop |
1 |
--永久关闭防火墙 |
1 2 |
systemctl disable firewalld service chkconfig iptables off |
怎么看tomcat服务是否启动成功呢,使用命令:
1 |
ps -ef | grep tomcat --查看正在运行的进程 |
如果看到tomcat的进程就说明tomcat启动成功了。如果没有出现就说明tomcat有问题。更换活着重新解压缩tomcat试试。 启动成功之后我们可以先看看在本机上是否可以访问。我们使用wget命令:
1 |
wget http://localhost:8080/ |
出现下图返回 200 ok 说明在本机上是没有问题的。 确认本机没有问题后,我们互相ping linux系统和windows系统的ip地址,看是否可以相互ping通,如果不能以相互ping通的话。请检查检查windows和linux服务器连接问题。比如防火墙,或者代理问题。 如果本机上也没有返回 200 就检查tomcat的配置文件,看他得端口号是不是默认的8080,如果端口号也没错的话还是看不见tomcat的猫的话可以留言和我讨论。
1 |
more conf/server.xml |
最后一点,如何设置tomcat开机自启动。将tomcat配置在 /etc/rc.local 下。
1 |
vim /etc/rc.local |
启动tomcat之前必须将java环境变量设置好。
1 2 3 |
JAVA_HOME=/usr/java/jdk1.8.0_91 export JAVA_HOME /usr/local/tomcat-8.5.14/bin/startup.sh |
重启reboot验证,tomcat 是否启动了,执行。
1 |
ps -ef | grep tomcat |
出现下图就说明tomcat已经启动了。 最后也就是见证奇迹的时刻,我们把我们的网站发布至/usr/local/tomcat-8.5.14/webapps/ROOT文件夹下; 我们就可以直接访问我们的网站了! from: http://www.cnblogs.com/Jxiaobai/p/6840480.html?utm_source=itdadao&utm_medium=referral
View Detailsvar result = "平年"; var year = DateTime.Now.Year; //不是整百年,且可以被4整除 if (year % 100 != 0 && year % 4 == 0) { result = "闰年"; } //能被400整除 if (year % 400 == 0) { result = "闰年"; } //对于数值很大的年份,这年如果能被3200整除,并且能被172800整除则是闰年 if (year > 3200 && year % 3200 == 0 && year % 172800 == 0) { result = "闰年"; }
View Details(PHP 4 >= 4.0.4, PHP 5, PHP 7) call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 说明 mixed call_user_func_array ( callable $callback , array $param_arr ) 把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。 参数 callback 被调用的回调函数。 param_arr 要被传入回调函数的数组,这个数组得是索引数组。 返回值 返回回调函数的结果。如果出错的话就返回FALSE 更新日志 版本 说明 5.3.0 对面向对象里面的关键字的解析有所增强。在此之前,使用两个冒号来连接一个类和里面的一个方法,把它作为参数来作为回调函数的话,将会发出一个E_STRICT的警告,因为这个传入的参数被视为静态方法。 范例 Example #1 call_user_func_array()例子 <?php function foobar($arg, $arg2) { echo __FUNCTION__, " got $arg and $arg2\n"; } class foo { function bar($arg, $arg2) { echo __METHOD__, " got $arg and $arg2\n"; } } // Call the foobar() function with 2 arguments call_user_func_array("foobar", array("one", "two")); // Call the $foo->bar() method with 2 arguments $foo = new foo; call_user_func_array(array($foo, "bar"), array("three", "four")); ?> 以上例程的输出类似于:
1 2 |
foobar got one and two foo::bar got three and four |
Example #2 call_user_func_array()使用命名空间的情况 <?php namespace Foobar; class Foo { static public function test($name) { print "Hello {$name}!\n"; } } // As of PHP 5.3.0 call_user_func_array(__NAMESPACE__ .’\Foo::test', array('Hannes')); // As of PHP 5.3.0 call_user_func_array(array(__NAMESPACE__ .’\Foo', 'test'), array('Philip')); ?> 以上例程的输出类似于:
1 2 |
Hello Hannes! Hello Philip! |
Example #3 把完整的函数作为回调传入call_user_func_array() <?php $func = function($arg1, $arg2) { return $arg1 * $arg2; }; var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */ ?> 以上例程会输出:
1 |
int(8) |
Example #4 传引用 <?php function mega(&$a){ […]
View Detailsjava对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题。 建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 1 2 3 4 HttpServletRequest request HttpServletResponse response Cookie cookie = new Cookie("cookiename","cookievalue"); response.addCookie(cookie); 下面建立一个有生命周期的cookie,可以设置他的生命周期 1 2 3 4 5 6 7 8 cookie = new Cookie("cookiename","cookievalue"); cookie.setMaxAge(3600); //设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问 cookie.setPath("/"); response.addCookie(cookie); 下面介绍如何读取cookie,读取cookie代码如下 1 2 3 4 5 Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组 for(Cookie cookie : cookies){ cookie.getName();// get the cookie name cookie.getValue(); // get the cookie value } 上面就是基本的读写cookie的操作。我们在实际中最好进行一下封装,比如增加一个cookie,我们关注的是cookie的name,value,生命周期,所以进行封装一个函数,当然还要传入一个response对象,addCookie()代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 /** * 设置cookie * @param response * @param name cookie名字 […]
View Details这样写就是错的,编译通不过 #include <string> void main() { string x; } 改成这样就 OK: #include <string> using namespace std; void main() { string x; } 或者这样: #include <string> void main() { std::string x; } 原因 就是 string 是标准库,C++标准引入了命名空间namespace,标准库的空间是std from:http://blog.csdn.net/sergery/article/details/8144731
View Details一、针对Microsoft #include <stdlib.h> (1)第一种方式 system( "PAUSE "); ——————-- (2)第二种方式 getchar(); // 这招对QT程序也有用 ——————— (3)第三种方式 Sleep(); 二、针对Linux (1)第一种方式 getchar(); 参考: http://www.cppblog.com/lauer3912/archive/2011/06/18/148936.html
View Details//WebRequest request = WebRequest.Create(uri); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); //request.Timeout = Timeout.Infinite; request.Timeout = 20000; //request.AllowAutoRedirect = true; //request.AllowWriteStreamBuffering = true; //request.Referer = ""; response = request.GetResponse(); 转载自:http://blog.sina.com.cn/s/blog_6fe1bc2e0101emc5.html 【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (HttpWebResponse)req.GetResponse(); 之前的多次调试,一直都是可以正常获得对应的response,然后读取html页面的。 但是后来几次的调试,在没有改变代码的前提下,结果GetResponse却始终会超时死掉。 【解决过程】 1.默认request的timeout是1000000毫秒=100秒,都会超时,手动改为10秒,因此就更容易超时了,无法解决问题。 2.将http的request的keepAlive设置为false,问题依旧。 3.去参考:c# request.GetResponse();超时问题的解决,和HttpWebRequest多线程性能问题,请求超时的错误, 去把前面共4次的httprequest,每次都增加对应的: resp = null; 。。。 if (resp != null) { resp.Close(); } if (req != null) { req.Abort(); } 结果还是没解决问题。 4. 同样参考:HttpWebRequest多线程性能问题,请求超时的错误, 去尝试关于DefaultConnectionLimit的设置,改为为10: System.Net.ServicePointManager.DefaultConnectionLimit = 10; 问题依旧。 5.又去测试了下,关于response.Close() 也是没解决问题。 6. 最后无意间,索性不抱希望的,再次DefaultConnectionLimit设置为更大的值50: System.Net.ServicePointManager.DefaultConnectionLimit = 50; 试了试,结果就解决超时的问题了。 然后才搞懂原因。 之前默认设置为2,后来改为10,都没有解决问题的原因在于,当前有很多个http的连接,没有被关闭掉, 而这些keepalive的连接,都是 由于代码中,对于前面多个request。其都是keepalive为true,以及多个response也没有close, 而之前调试了很多次了,所以,此时已经存在了很多个alive的http连接了,已经超过了10个了,所以前面设置了DefaultConnectionLimit 为10,也还是没用的。 而改为50,才够用。 【总结】 此处GetResponse超过的原因是,当前存在太多数目的alive的http连接(大于10个),所以再次提交同样的http的request,再去GetResponse,就会超时死掉。 解决办法就是,把DefaultConnectionLimit 设置为一个比较大一点的数值,此数值保证大于你当前已经存在的alive的http连接数即可。 【经验总结】 […]
View Details