Servlet usage code for example

1. return session instance,

public HttpSession getSession() {
try {
return request.getSession();
} catch (Exception e) {
return null;
}
}


2. return absolute path,

public String getRealPath() {
return request.getSession().getServletContext().getRealPath("/");
}


3.get  remote server's IP

public String getRemoteAddr() {
if ("127.0.0.1"==(request.getRemoteAddr()) 
|| "127.0.0.1".equals(request.getRemoteAddr())){
return CommonFun.fetchLocIp();
}else{
return request.getRemoteAddr();
}

}


4. get remote server's port

public Integer getRemotePort() {
return request.getRemotePort();
}


5. Get sessionId

public String getSessionId() {
// 如果sessionId不为空则直接返回
if (null!=sessionId && (sessionId.length()) > 0){
return sessionId;
}
try {
// 根据用户cookie取sessionID
java.util.Map<String, String> cookieMap = CookieUtil.parseCookie(request, "CmsCookie");
if (null==cookieMap){
return null;
}
sessionId = cookieMap.get("sessionId");
log.debug("now sessionId is&&&&&&&&&&&&&&&=" + sessionId);
} catch (RuntimeException e) {
log.error("BaseAction::getLoginUserInfo::获取当前会话ID出现异常", e);
throw e;
}
return sessionId;// 返回
}

你可能感兴趣的:(code,servlet,f,usage)