java代码怎么判断当前服务器是tomcat还是weblogic啊,就是获取服务器类型。。。。
package comrogerqueryutil;
import orgapachelog4jLogger;
/
@ 服务器类型探测
@Date 2011/04/13
/
public class ServerUtil {
public static final String GERONIMO_CLASS = "/org/apache/geronimo/system/main/Daemonclass";
public static final String JBOSS_CLASS = "/org/jboss/Mainclass";
public static final String JETTY_CLASS = "/org/mortbay/jetty/Serverclass";
public static final String JONAS_CLASS = "/org/objectweb/jonas/server/Serverclass";
public static final String OC4J_CLASS = "/oracle/jsp/oc4jutil/Oc4jUtilclass";
public static final String ORION_CLASS = "/com/evermind/server/ApplicationServerclass";
public static final String PRAMATI_CLASS = "/com/pramati/Serverclass";
public static final String RESIN_CLASS = "/com/caucho/server/resin/Resinclass";
public static final String REXIP_CLASS = "/com/tcc/Mainclass";
public static final String SUN7_CLASS = "/com/iplanet/ias/tools/cli/IasAdminMainclass";
public static final String SUN8_CLASS = "/com/sun/enterprise/cli/framework/CLIMainclass";
public static final String TOMCAT_CLASS = "/org/apache/catalina/startup/Bootstrapclass";
public static final String WEBLOGIC_CLASS = "/weblogic/Serverclass";
public static final String WEBSPHERE_CLASS = "/com/ibm/websphere/product/VersionInfoclass";
public static String getServerId() {
ServerUtil sd = _instance;
if (sd_serverId == null) {
if (ServerUtilisGeronimo()) {
sd_serverId = "geronimo";
} else if (ServerUtilisJBoss()) {
sd_serverId = "jboss";
} else if (ServerUtilisJOnAS()) {
sd_serverId = "jonas";
} else if (ServerUtilisOC4J()) {
sd_serverId = "oc4j";
} else if (ServerUtilisOrion()) {
sd_serverId = "orion";
} else if (ServerUtilisResin()) {
sd_serverId = "resin";
} else if (ServerUtilisWebLogic()) {
sd_serverId = "weblogic";
} else if (ServerUtilisWebSphere()) {
sd_serverId = "websphere";
}
if (ServerUtilisJetty()) {
if (sd_serverId == null) {
sd_serverId = "jetty";
} else {
sd_serverId += "-jetty";
}
} else if (ServerUtilisTomcat()) {
if (sd_serverId == null) {
sd_serverId = "tomcat";
} else {
sd_serverId += "-tomcat";
}
}
if (_logisInfoEnabled()) {
_loginfo("Detected server " + sd_serverId);
}
if (sd_serverId == null) {
throw new RuntimeException("Server is not supported");
}
}
return sd_serverId;
}
public static boolean isGeronimo() {
ServerUtil sd = _instance;
if (sd_geronimo == null) {
Class c = sdgetClass();
if (cgetResource(GERONIMO_CLASS) != null) {
sd_geronimo = BooleanTRUE;
} else {
sd_geronimo = BooleanFALSE;
}
}
return sd_geronimobooleanValue();
}
public static boolean isJBoss() {
ServerUtil sd = _instance;
if (sd_jBoss == null) {
Class c = sdgetClass();
if (cgetResource(JBOSS_CLASS) != null) {
sd_jBoss = BooleanTRUE;
} else {
sd_jBoss = BooleanFALSE;
}
}
return sd_jBossbooleanValue();
}
public static boolean isJetty() {
ServerUtil sd = _instance;
if (sd_jetty == null) {
Class c = sdgetClass();
if (cgetResource(JETTY_CLASS) != null) {
sd_jetty = BooleanTRUE;
} else {
sd_jetty = BooleanFALSE;
}
}
return sd_jettybooleanValue();
}
public static boolean isJOnAS() {
ServerUtil sd = _instance;
if (sd_jonas == null) {
Class c = sdgetClass();
if (cgetResource(JONAS_CLASS) != null) {
sd_jonas = BooleanTRUE;
} else {
sd_jonas = BooleanFALSE;
}
}
return sd_jonasbooleanValue();
}
public static boolean isOC4J() {
ServerUtil sd = _instance;
if (sd_oc4j == null) {
Class c = sdgetClass();
if (cgetResource(OC4J_CLASS) != null) {
sd_oc4j = BooleanTRUE;
} else {
sd_oc4j = BooleanFALSE;
}
}
return sd_oc4jbooleanValue();
}
public static boolean isOrion() {
ServerUtil sd = _instance;
if (sd_orion == null) {
Class c = sdgetClass();
if (cgetResource(ORION_CLASS) != null) {
sd_orion = BooleanTRUE;
} else {
sd_orion = BooleanFALSE;
}
}
return sd_orionbooleanValue();
}
public static boolean isPramati() {
ServerUtil sd = _instance;
if (sd_pramati == null) {
Class c = sdgetClass();
if (cgetResource(PRAMATI_CLASS) != null) {
sd_pramati = BooleanTRUE;
} else {
sd_pramati = BooleanFALSE;
}
}
return sd_pramatibooleanValue();
}
public static boolean isResin() {
ServerUtil sd = _instance;
if (sd_resin == null) {
Class c = sdgetClass();
if (cgetResource(RESIN_CLASS) != null) {
sd_resin = BooleanTRUE;
} else {
sd_resin = BooleanFALSE;
}
}
return sd_resinbooleanValue();
}
public static boolean isRexIP() {
ServerUtil sd = _instance;
if (sd_rexIP == null) {
Class c = sdgetClass();
if (cgetResource(REXIP_CLASS) != null) {
sd_rexIP = BooleanTRUE;
} else {
sd_rexIP = BooleanFALSE;
}
}
return sd_rexIPbooleanValue();
}
public static boolean isSun() {
if (isSun7() || isSun8()) {
return true;
} else {
return false;
}
}
public static boolean isSun7() {
ServerUtil sd = _instance;
if (sd_sun7 == null) {
Class c = sdgetClass();
if (cgetResource(SUN7_CLASS) != null) {
sd_sun7 = BooleanTRUE;
} else {
sd_sun7 = BooleanFALSE;
}
}
return sd_sun7booleanValue();
}
public static boolean isSun8() {
ServerUtil sd = _instance;
if (sd_sun8 == null) {
Class c = sdgetClass();
if (cgetResource(SUN8_CLASS) != null) {
sd_sun8 = BooleanTRUE;
} else {
sd_sun8 = BooleanFALSE;
}
}
return sd_sun8booleanValue();
}
public static boolean isTomcat() {
ServerUtil sd = _instance;
if (sd_tomcat == null) {
Class c = sdgetClass();
if (cgetResource(TOMCAT_CLASS) != null) {
sd_tomcat = BooleanTRUE;
} else {
sd_tomcat = BooleanFALSE;
}
}
return sd_tomcatbooleanValue();
}
public static boolean isWebLogic() {
ServerUtil sd = _instance;
if (sd_webLogic == null) {
Class c = sdgetClass();
if (cgetResource(WEBLOGIC_CLASS) != null) {
sd_webLogic = BooleanTRUE;
} else {
sd_webLogic = BooleanFALSE;
}
}
return sd_webLogicbooleanValue();
}
public static boolean isWebSphere() {
ServerUtil sd = _instance;
if (sd_webSphere == null) {
Class c = sdgetClass();
if (cgetResource(WEBSPHERE_CLASS) != null) {
sd_webSphere = BooleanTRUE;
} else {
sd_webSphere = BooleanFALSE;
}
}
return sd_webSpherebooleanValue();
}
private ServerUtil() {
}
private static Logger _log = LoggergetLogger(ServerUtilclass);
private static ServerUtil _instance = new ServerUtil();
private String _serverId;
private Boolean _geronimo;
private Boolean _jBoss;
private Boolean _jetty;
private Boolean _jonas;
private Boolean _oc4j;
private Boolean _orion;
private Boolean _pramati;
private Boolean _resin;
private Boolean _rexIP;
private Boolean _sun7;
private Boolean _sun8;
private Boolean _tomcat;
private Boolean _webLogic;
private Boolean _webSphere;
}
代码来自 javaRoger 的博客,求采纳
出现该问题,主要是因为tomcat版本高的原因。我使用tomcat 8024,便没有此问题,当我使用tomcat 85+ 时,就会出现该问题,需要修改我下面书写的配置,才能生效!
一、检查linux
linux文件访问默认权限与umask有关,查看系统默认配置(/etc/profile)是否正常,如果是002或022就正常没有问题。
[umask 002 对应文件权限664,文件夹权限775;
umask 022对应文件权限644,文件夹权限755]
二、检查tomcat
打开bin/catalinash文件,检查大概在263行左右。
将0027改改为0022,重启tomcat,上传访问的问题解决。
原代码
# Set UMASK unless it has been overriddenif [ -z "$UMASK" ]; then
UMASK="0027"fi
改为
# Set UMASK unless it has been overriddenif [ -z "$UMASK" ]; then
UMASK="0022"fi
三、重启tomcat
sh shutdownsh(关闭)
sh startupsh 或者 /startupsh(启动)
JAVA中通过Hibernate-Validation进行参数验证
在开发JAVA服务器端代码时,我们会遇到对外部传来的参数合法性进行验证,而hibernate-validator提供了一些常用的参数校验注解,我们可以拿来使用。
1maven中引入hibernate-validator对应的jar:
<dependency>
<groupId>orghibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>431Final</version>
</dependency>
2在Model中定义要校验的字段(即该字段不能为空,并且最大长度为14):
import javaxvalidationconstraintsSize;
import orghibernatevalidatorconstraintsNotEmpty;
public class PayRequestDto {
/
支付完成时间
/
@NotEmpty(message="支付完成时间不能空")
@Size(max=14,message="支付完成时间长度不能超过{max}位")
private String payTime;
public String getPayTime() {
return payTime;
}
public void setPayTime(String payTime) {
thispayTime = payTime;
}
}
3定义Validation工具类:
import javautilSet;
import javaxvalidationConstraintViolation;
import javaxvalidationValidation;
import javaxvalidationValidator;
import orghibernatevalidatorHibernateValidator;
import comataiframeworklangAppException;
public class ValidationUtils {
/
使用hibernate的注解来进行验证
/
private static Validator validator = Validation
byProvider(HibernateValidatorclass)configure()failFast(true)buildValidatorFactory()getValidator();
/
功能描述: <br>
〈注解验证参数〉
@param obj
@see [相关类/方法](可选)
@since [产品/模块版本](可选)
/
public static <T> void validate(T obj) {
Set<ConstraintViolation<T>> constraintViolations = validatorvalidate(obj);
// 抛出检验异常
if (constraintViolationssize() > 0) {
throw new AppException("0001", Stringformat("参数校验失败:%s", constraintViolationsiterator()next()getMessage()));
}
}
}
4在代码中调用工具类进行参数校验:
ValidationUtilsvalidate(requestDto);
以下是对hibernate-validator中部分注解进行描述:
@AssertTrue 用于boolean字段,该字段只能为true
@AssertFalse 该字段的值只能为false
@CreditCardNumber 对信用卡号进行一个大致的验证
@DecimalMax 只能小于或等于该值
@DecimalMin 只能大于或等于该值
@Digits(integer=,fraction=) 检查是否是一种数字的整数、分数,小数位数的数字
@Email 检查是否是一个有效的email地址
@Future 检查该字段的日期是否是属于将来的日期
@Length(min=,max=) 检查所属的字段的长度是否在min和max之间,只能用于字符串
@Max 该字段的值只能小于或等于该值
@Min 该字段的值只能大于或等于该值
@NotNull 不能为null
@NotBlank 不能为空,检查时会将空格忽略
@NotEmpty 不能为空,这里的空是指空字符串
@Null 检查该字段为空
@Past 检查该字段的日期是在过去
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
@Size(min=, max=) 检查该字段的size是否在min和max之间,可以是字符串、数组、集合、Map等
@URL(protocol=,host,port) 检查是否是一个有效的URL,如果提供了protocol,host等,则该URL还需满足提供的条件
@Valid 该注解主要用于字段为一个包含其他对象的集合或map或数组的字段,或该字段直接为一个其他对象的引用,这样在检查当前对象的同时也会检查该字段所引用的对象
通过心跳包来确保客户端是否正常连接,比如定时发心跳包给客户端,然后接收回应包,如果没有收到该回应包则可以认为客户端已经断开连接,这个算是常规做法,
至少在我所有SOCKET编程中都使用这种方式来维护连接状态。
0条评论