本文介绍了Jetty:将对象从main方法传递给servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个类服务器
(使用main方法,启动服务器)和 StartPageServlet
使用Servlet。
I have two classes Server
(with the main method, starting the server) and StartPageServlet
with a Servlet.
代码最重要的部分是:
public class Server {
public static void main(String[] args) throws Exception {
// some code
// I want to pass "anObject" to every Servlet.
Object anObject = new Object();
Server server = new Server(4000);
ServletContextHandler context =
new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(StartPageServlet.class, "/");
// more code
}
和StartPageServlet:
And the StartPageServlet:
public class StartPageServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Here I want to access "anObject"
}
我该怎么做?
推荐答案
嵌入式Jetty是这样的很棒。
Embedded Jetty is so wonderful here.
您有几个常见的选择:
- 直接实例化servlet,使用构造函数或setter,然后通过
ServletHolder
将其交给Jetty(可以是任何值或对象类型) - 将它添加到main中的
ServletContext
,然后通过应用程序中的ServletContext
访问它(可以是任何值或对象类型)。
- Direct instantiation of the servlet, use constructors or setters, then hand it off to Jetty via the
ServletHolder
(can be any value or object type) - Add it to the
ServletContext
in your main, and then access it via theServletContext
in your application (can be any value or object type).
示例:
package jetty;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class ObjectPassingExample
{
public static void main(String args[]) throws Exception
{
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
// Option 1: Direct servlet instantiation and ServletHolder
HelloServlet hello = new HelloServlet("everyone");
ServletHolder helloHolder = new ServletHolder(hello);
context.addServlet(helloHolder, "/hello/*");
// Option 2: Using ServletContext attribute
context.setAttribute("my.greeting", "you");
context.addServlet(GreetingServlet.class, "/greetings/*");
server.setHandler(context);
server.start();
server.join();
}
public static class HelloServlet extends HttpServlet
{
private final String hello;
public HelloServlet(String greeting)
{
this.hello = greeting;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
resp.getWriter().println("Hello " + this.hello);
}
}
public static class GreetingServlet extends HttpServlet
{
private String greeting;
@Override
public void init() throws ServletException
{
this.greeting = (String) getServletContext().getAttribute("my.greeting");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
resp.getWriter().println("Greetings to " + this.greeting);
}
}
}
这篇关于Jetty:将对象从main方法传递给servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!