セッションを見ているサーブレットでhello world Jettyプロジェクト(Jetty 9.3.6.v20151106を使用)を実装しようとしています。しかし、私のコードが実行されると、私はセッションを見たり、クッキーを保存することができません。私はthis stackoverflowの質問でトラブルシューティングを支援し、シンプルなBaseServletを実装することをお勧めしましたが、BaseServletを実行するたびに「セッションなし」を出力します。私はいくつかの設定が欠けていますか?ここでJetty ServerのセッションがNullです
は私のHelloWorld.javaクラスである:ここで
package com.my.company;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.session.HashSessionIdManager;
import org.eclipse.jetty.server.session.HashSessionManager;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class HelloWorld {
public static void main(String[] args) throws Exception {
//Create the server
Server server = new Server();
// Configure for Http
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768);
ServerConnector http = new ServerConnector(server,
new HttpConnectionFactory(http_config));
int http_port = 12043;
http.setPort(http_port);
http.setIdleTimeout(30000);
server.addConnector(http);
//Add ServletContextHandler
ServletContextHandler servletContextHandler = new ServletContextHandler(
ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
SessionManager sessionManager = new HashSessionManager();
sessionManager.setMaxInactiveInterval(60 * 15); //session time out of 15 minutes
HashSessionIdManager idManager = new HashSessionIdManager();
sessionManager.getSessionCookieConfig().setName("JSESSIONID_" + Integer.toString(http_port));
sessionManager.setSessionIdManager(idManager);
server.setSessionIdManager(idManager);
SessionHandler sessionHandler = new SessionHandler(sessionManager);
servletContextHandler.setSessionHandler(sessionHandler);
server.setHandler(servletContextHandler);
//Add BaseServlet
ServletHolder holder = new ServletHolder(new BaseServlet());
servletContextHandler.addServlet(holder, "/*");
//Start the server
server.start();
System.out.println("SERVER DUMP AFTER START" + server.dump());
// The use of server.join() the will make the current thread join and
// wait until the server is done executing.
// See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join();
}
}
は私のBaseServlet.javaクラスです:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EmbeddJettyHelloWorld</groupId>
<artifactId>EmbeddJettyHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EmbeddJettyHelloWorld</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.my.company.HelloWorld</mainClass>
<classpathScope>test</classpathScope>
<systemProperties>
<property>
<key>basedir</key>
<value>${basedir}</value>
</property>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty-version}</version>
</dependency>
</dependencies>
<properties>
<jetty-version>9.3.6.v20151106</jetty-version>
</properties>
:ここ
package com.my.company;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class BaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
boolean create = "true".equals(req.getParameter("create"));
HttpSession session = req.getSession(create);
if (create) {
session.setAttribute("created", new Date());
}
PrintWriter pw = new PrintWriter(resp.getOutputStream());
pw.println("Create = " + create);
if (session == null) {
pw.println("no session");
} else {
pw.println("Session = " + session.getId());
pw.println("Created = " + session.getAttribute("created"));
}
pw.flush();
}
}
は私のpom.xmlファイルであります
ここにouがありますサーバー・ダンプのTPUT:
SERVER DUMP AFTER [email protected] - STARTED
+= qtp1149319664{STARTED,8<=8<=200,i=5,q=0} - STARTED
| +- 15 qtp1149319664-15 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
| +- 13 [email protected]@326de728 {HTTP/1.1,[http/1.1]}{0.0.0.0:12043} RUNNABLE @ sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) prio=3
| +- 14 qtp1149319664-14 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
| +- 12 qtp1149319664-12 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
| +- 18 qtp1149319664-18 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
| +- 16 qtp1149319664-16 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
| +- 11 qtp1149319664-11 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
| +- 17 qtp1149319664-17 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
+= [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:12043} - STARTED
| +~ [email protected] - STARTED
| +~ qtp1149319664{STARTED,8<=8<=200,i=5,q=0} - STARTED
| += [email protected]e - STARTED
| +- [email protected]
| += [email protected][HTTP/1.1] - STARTED
| | +- [email protected]{32768/8192,8192/8192,https://:8443,[]}
| += [email protected]4b6995df - STARTED
| | +- [email protected] id=0 keys=0 selected=0 id=0
| | | +- [email protected] keys=0
| | +- [email protected] id=1 keys=0 selected=0 id=1
| | +- [email protected] keys=0
| +- sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:12043]
| +- [email protected]@326de728{HTTP/1.1,[http/1.1]}{0.0.0.0:12043}
+= [email protected] - STARTED
+= [email protected]{/,null,AVAILABLE} - STARTED
| += [email protected] - STARTED
| | += [email protected] - STARTED
| | | +~ [email protected] - STARTED
| | | += [email protected]b - STARTED
| | | +- sun.misc.Unsafe.park(Native Method)
| | | +- java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
| | | +- java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
| | | +- java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
| | | +- java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
| | | +- java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
| | | +- java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
| | | +- java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
| | | +- java.lang.Thread.run(Thread.java:745)
| | += [email protected] - STARTED
| | += [email protected]==com.rtn.iaf.BaseServlet,-1,true - STARTED
| | +- [/*]=>com.rtn.iaf.BaseServlet-484b61fc
| | += org.[email protected]1d1bf782==org.eclipse.jetty.servlet.ServletHandler$Default404Servlet,-1,false - STARTED
| | +- [/]=>org.eclipse.jetty.servlet.ServletHandler$Default404Servlet-7a7b0070
| |
| +> No ClassLoader
| +> Handler attributes [email protected]{/,null,AVAILABLE}
| | +- org.eclipse.jetty.server.Executor=qtp1149319664{STARTED,8<=8<=200,i=5,q=0}
| +> Context attributes [email protected]{/,null,AVAILABLE}
| | +- org.eclipse.jetty.util.DecoratedObjectFactory=org.eclipse.jetty.util.DecoratedObjectFactory[decorators=0]
| +> Initparams [email protected]{/,null,AVAILABLE}
|
+> [email protected]
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/EmbeddJettyHelloWorld/target/classes/
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-server/9.3.6.v20151106/jetty-server-9.3.6.v20151106.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-http/9.3.6.v20151106/jetty-http-9.3.6.v20151106.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-util/9.3.6.v20151106/jetty-util-9.3.6.v20151106.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-io/9.3.6.v20151106/jetty-io-9.3.6.v20151106.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-servlet/9.3.6.v20151106/jetty-servlet-9.3.6.v20151106.jar
+- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-security/9.3.6.v20151106/jetty-security-9.3.6.v20151106.jar
+- [email protected]