我正在准备OCPJP考试,我遇到了以下示例:
class Test {
public static void main(String args[]) {
String test = "I am preparing for OCPJP";
String[] tokens = test.split("\\S");
System.out.println(tokens.length);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码打印16.我期待像no_of_characters + 1这样的东西.有人可以解释一下,split()方法在这种情况下实际上做了什么?我只是不明白......
我正在为大量 JSF 1.x 和 2.x 应用程序创建一个自定义框架(类似于门户)。为此,我创建了一个 servlet 过滤器,用框架菜单、面包屑、注销等“丰富”应用程序 HTML。在该过滤器中,我读取应用程序的 HTML,修改它并将其写入输出流。到目前为止,一切都很好,但现在我在创建自定义错误页面时遇到了问题。
我试图读取响应状态代码,并基于该代码,我正在创建输出 HTML:
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) req;
HttpServletResponse res = (HttpServletResponse) resp;
StringServletResponseWrapper responseWrapper = new StringServletResponseWrapper(res);
// Invoke resource, accumulating output in the wrapper.
chain.doFilter(req, responseWrapper);
String contentType = res.getContentType();
byte[] data;
if (contentType.contains("text/html")) {
String html = null;
int statusCode = res.getStatus();
LOG.debug("status: {}, committed: {}", statusCode, res.isCommitted());
if (statusCode != 200) {
html = "<!DOCTYPE …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个本地化的 JSF Web 应用程序,它允许用户通过下拉菜单选择一种语言。选择语言后,我模拟重定向到同一页面,但带有 URL 参数:
window.location.replace(urlToMyApp + '?locale=DE');
Run Code Online (Sandbox Code Playgroud)
接下来,我在应用程序的 Web 过滤器中读取“locale”参数并将其写入同名的 cookie 中:
String localeValue = httpRequest.getParameter("locale");
Cookie cookie = new Cookie("locale", localeValue);
cookie.setMaxAge(-1);
cookie.setDomain(cookieDomain);
cookie.setPath(cookiePath);
httpResponse.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试在请求 bean init 方法中读取该 cookie 时,cookie 不可用。如果我通过下拉菜单(例如 EN)选择另一种语言,则在 init 方法中读取先前选择的语言 (DE)。
我假设在下一个“请求 - 响应”周期之前写入过滤器的 cookie 不可用,有人可以确认吗?
如果这是真的,我想在选择另一种语言后立即翻译我的应用程序。
我认为我需要提及一件事 - 语言下拉列表不是我的应用程序的一部分。它是包含多个应用程序的某种框架的一部分(如门户)。
cookies ×1
java ×1
jsf ×1
localization ×1
ocpjp ×1
regex ×1
requestscope ×1
servlets ×1
split ×1