gpt4 book ai didi

java.lang.IllegalStateException : No SessionManager 错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:02 26 4
gpt4 key购买 nike

请给我建议如何正确设置 Jetty。我使用的是最新版本的 jetty (9.0.6.v20130930)。我订购了服务器实现具体的 servlet,但它不起作用! 我已经与 Jetty 合作过一些,但没有看到类似的东西。

-- 主类

 public static void main(String[] args) throws Exception
{
MessageSystem ms = new MessageSystem();
Frontend frontend = new Frontend(ms);

(new Thread(frontend)).start();

Server server = new Server(8080);
server.setHandler(frontend);

server.start();
server.join();

}

-- 前端类

public class Frontend extends AbstractHandler implements Runnable, Abonent {
... constructor and others methods...

public void handle(String target, Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
setResponseSettings(baseRequest,response);
PrintWriter out = response.getWriter();

try {
int id;
HttpSession session = request.getSession();
if( session.isNew()){
id = setSessionId(session);
pageContent = "Hello new user! Your id: "+id;
out.println(new Page().enterName());
}else{
id = getSessionId(session);
pageContent = "Hi! I remember you. You are: " +id;
out.println(new Page().normalContent());
}
}
catch (IllegalStateException ex){
System.out.println("Exception!"+ex);
}
out.println("<H1>" + pageContent + "</H1>");
Iterator iterator = listStrings.iterator();
while (iterator.hasNext()){
out.println("<br>");
out.println("Your text from TextField: "+iterator.next());
}
out.close();
}

-- 控制台

2013-11-01 04:06:24.273:INFO:oejs.Server:main: jetty-9.0.6.v20130930
2013-11-01 04:06:24.309:INFO:oejs.ServerConnector:main: Started ServerConnector@358e0f79{HTTP/1.1}{0.0.0.0:8080}
Exception!java.lang.IllegalStateException: No SessionManager
Exception!java.lang.IllegalStateException: No SessionManager

最佳答案

你错过了 3 件事。

主.java

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.session.HashSessionIdManager;
import org.eclipse.jetty.server.session.HashSessionManager;
import org.eclipse.jetty.server.session.SessionHandler;

public class Main
{
public static void main(String[] args)
{
MyDumpHandler dump = new MyDumpHandler();

Server server = new Server(8080);

// Specify the Session ID Manager
HashSessionIdManager idmanager = new HashSessionIdManager();
server.setSessionIdManager(idmanager);

// Sessions are bound to a context.
ContextHandler context = new ContextHandler("/");
server.setHandler(context);

// Create the SessionHandler (wrapper) to handle the sessions
HashSessionManager manager = new HashSessionManager();
SessionHandler sessions = new SessionHandler(manager);
context.setHandler(sessions);

// Put dump inside of SessionHandler
sessions.setHandler(dump);

// Tree is now
// Server
// + ContextHandler("/")
// + SessionHandler(Hash)
// + MyDumpHandler

try
{
server.start();
server.join();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}

MyDumpHandler.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class MyDumpHandler extends AbstractHandler
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/plain");

try
{
HttpSession session = request.getSession();
if (session.isNew())
{
out.printf("New Session: %s%n", session.getId());
}
else
{
out.printf("Old Session: %s%n", session.getId());
}
}
catch (IllegalStateException ex)
{
out.println("Exception!" + ex);
ex.printStackTrace(out);
}
out.close();
}
}

关于java.lang.IllegalStateException : No SessionManager 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19718159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com