我正在尝试实现一个简单的servlet,它在嵌入式jetty(7.3.0 v20110203)容器中使用HTTP会话.要启动jetty,我使用以下代码:
Server server = new Server(12043);
ServletContextHandler handler = new
ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
server.setHandler(handler);
ServletHolder holder = new ServletHolder(new BaseServlet());
handler.addServlet(holder, "/*");
server.start();
server.join();
Run Code Online (Sandbox Code Playgroud)
servlet获取会话
HttpSession session = request.getSession(true);
Run Code Online (Sandbox Code Playgroud)
并在其中存储一些数据.在下一个请求时,它会使用以下代码获取会话:
HttpSession session = request.getSession(false);
Run Code Online (Sandbox Code Playgroud)
会话始终为空.
我没有在互联网上找到有关此特定问题的任何信息.我还尝试过设置SessionManager或SessionIdManager,但这似乎没有改变任何东西.我怀疑我在这里遗漏了一些关于SessionManager或SessionIdManager或SessionHandler的内容,但这只是一个疯狂的猜测.
我想用gradle构建运行Jetty 7+,但不幸的是看起来没有办法用jettyRun来做这件事.因此,实现我想要的最简单的想法是使用自定义目标:
task runJetty << {
def server = new Server()
// more code here
server.start()
server.join()
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我刚从gradle开始,我也不知道groovy,所以我很难创建合适的目标.我正在寻找互联网,但我找不到任何解决方案.任何人都可以用一些样本groovy代码打我,可以用jetty运行现有的jar吗?
查看嵌入式Jetty示例的以下示例:http: //musingsofaprogrammingaddict.blogspot.com.au/2009/12/running-jsf-2-on-embedded-jetty.html
下面给出了代码示例(如下).
然后,作者继续举例说明在web.xml文件中引用上下文参数.例如
...
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
...
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 如果我想在Java类中做所有事情 - 有没有办法以编程方式设置context-params?
public class JettyRunner {
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(8080);
connector.setHost("127.0.0.1");
server.addConnector(connector);
WebAppContext wac = new AliasEnhancedWebAppContext();
wac.setContextPath("/myapp");
wac.setBaseResource(
new ResourceCollection(
new String[] {"./src/main/webapp", "./target"}));
wac.setResourceAlias("/WEB-INF/classes/", "/classes/");
server.setHandler(wac);
server.setStopAtShutdown(true);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud) 我按照提示创建一个"标准Web应用程序与码头和Maven" 正是作为Eclipse的维基描述:http://wiki.eclipse.org/Jetty/Tutorial/Jetty_and_Maven_HelloWorld#Developing_a_Standard_WebApp_with_Jetty_and_Maven
但是,当我运行webapp(mvn jetty:run)并转到localhost:8080/hello-world/hello时,我最终会遇到"HTTP ERROR 404问题访问/ hello-world/hello.原因:未找到".我已阅读文档,查看了wiki页面的历史记录,探讨了其他论坛和stackoverflow线程,但无法找到这个看似简单的问题的答案.我将发布我的源代码,但它与教程完全相同.
任何帮助,将不胜感激.我真的很想开始玩这种技术,但它仍然沮丧地继续抨击同样的死胡同.
(请注意:创建"JettyMavenHelloWorld"教程的第一部分工作正常.我的问题是第二部分,"JettyMavenHelloWarApp".这一部分标题为"使用Jetty和Maven开发标准WebApp")
JettyMavenHelloWarApp/pom.xml的
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hello-world</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Jetty HelloWorld</name>
<properties>
<jettyVersion>7.2.0.v20101020</jettyVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- This plugin is needed for the servlet example -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<mainClass>org.example.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
JettyMavenHelloWarApp/SRC /主/ JAVA /组织/示例/ HelloServlet.java
package org.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; …Run Code Online (Sandbox Code Playgroud) 是否可以使用嵌入式Jetty从目录X提供静态文件但映射到URL Y?我有静态文件存储在目录"web"下,但我希望URL类似于http://host/myapp.
我已经成功运行了ResourceHandler以下列方式配置的服务器:
ResourceHandler ctx = new ResourceHandler();
ctx.setResourceBase("path-to-web");
HandlerList list = new HandlerList();
list.addHandler(ctx);
...
server.setHandler(list);
Run Code Online (Sandbox Code Playgroud)
但结果是提供/web所需URL映射下的文件,而不是它们.
我正在尝试对上传调用进行单元测试,但是我收到以下代码的错误:
@MultipartConfig(maxFileSize = 3145728)
class WebServlet extends ScalatraServlet with FileUploadSupport {
override def isSizeConstraintException(e: Exception) = e match {
case se: ServletException if se.getMessage.contains("exceeds max filesize") ||
se.getMessage.startsWith("Request exceeds maxRequestSize") => true
case _ => false
}
error {
case e: SizeConstraintExceededException => RequestEntityTooLarge("too much!")
}
post("/uploadscript") {
val privateParam = try {params("private") != null && params("private").equals("true") } catch { case _ => false }
println("privateParam = " + privateParam)
val file = fileParams("file")
println(s"The size of the file is …Run Code Online (Sandbox Code Playgroud) 我正在使用嵌入式Jetty,我想添加一个servlet过滤器来检查每个请求之前的身份验证.我尝试了这个例子,但看起来签名已经改变了.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.0.4.v20130625</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的Jetty起动器看起来像这样:
public class JettyStarter {
public static void main( final String[] args ) throws Exception {
Server server = new Server(8080);
final ServletHolder servletHolder = new ServletHolder(new CXFServlet());
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
// context.addFilter(AuthenticationFilter.class, "/*", FilterMapping.REQUEST);
context.addServlet(servletHolder, "/platform/*");
context.addEventListener(new ContextLoaderListener());
context.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
context.setInitParameter("contextConfigLocation", Config.class.getName());
server.setHandler(context);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
当我取消注释该行
// context.addFilter(AuthenticationFilter.class, "/*", FilterMapping.REQUEST);
Run Code Online (Sandbox Code Playgroud)
我发现签名已经改变了.所以我想稍微退后一步,用嵌入式Jetty询问如何添加在请求开始时运行的过滤器,并且只有在满足某些条件时才允许请求继续?
AuthenticationFilter类的开头如下所示:
import javax.servlet.*;
import java.io.IOException;
public class AuthenticationFilter implements Filter {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我正在使用Jetty 9.1.0.RC2和Spring 4.有一个AbstractAnnotationConfigDispatcherServletInitializer并尝试启动初始化:
Server server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setConfigurations(new Configuration[] { new AnnotationConfiguration() });
webAppContext.setParentLoaderPriority(true);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/target/classes/.*");
server.setHandler(webAppContext);
server.start();
server.join();
Run Code Online (Sandbox Code Playgroud)
但未能发现:
No Spring WebApplicationInitializer types detected on classpath
Run Code Online (Sandbox Code Playgroud) 我试图在应用程序中嵌入Jetty服务器,并看到一个非常奇怪的问题:
根据文档,可以使用以下代码启动一个简单的服务器(我正在构建测试):
import org.eclipse.jetty.server.Server;
public class SimpleServer throws Exception
{
public static void main(String[] args)
{
Server server = new Server(8080);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
我相信我从下载的Jetty中获得了正确的Jar文件:
码头 - 服务器9.3.7.v20160115.jar
不幸的是,我发现我使用的Server类没有public start()方法.它有一个受保护的start()方法,它有一个LifeCycle参数,但就是这样.文档中引用的public start()方法(以及Stack Overflow中的几个答案)不存在!
我使用正确的服务器类吗?如果没有,我在哪里得到一个合适的???
有人请指教......
我创建了一个maven jersey starter webapp.此外,我使用jetty插件在我的应用程序中嵌入了jetty服务器.
当我使用mvn jetty:run命令运行我的项目时,我的项目工作正常.
但是当我使用mvn clean package命令打包我的项目并运行名为jar-with-dependencies的jar文件时,项目会在从jersey资源返回json响应时抛出此异常.
严重:找不到媒体类型= application/json的MessageBodyWriter,类型=类com.nitish.freecharge.model.Count,genericType = class com.nitish.freecharge.model.Count.
这是我的pom.xml文件
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nitish.freecharge</groupId>
<artifactId>wordcount</artifactId>
<packaging>war</packaging>
<version>2.0</version>
<name>wordcount</name>
<build>
<finalName>wordcount</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.v20150612</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<webApp>
<contextPath>/wordcount</contextPath>
</webApp>
<httpConnector>
<!--host>localhost</host -->
<port>9999</port>
</httpConnector>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>package-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version> …Run Code Online (Sandbox Code Playgroud) jersey embedded-jetty maven jersey-2.0 jar-with-dependencies
embedded-jetty ×10
jetty ×5
java ×4
maven ×2
spring ×2
gradle ×1
groovy ×1
httpsession ×1
jersey ×1
jersey-2.0 ×1
jetty-9 ×1
maven-2 ×1
rest ×1
scala-2.10 ×1
scalatra ×1
servlets ×1
spring-mvc ×1
unit-testing ×1
war ×1
web-testing ×1